adnaniqbal001 commited on
Commit
def6c22
·
verified ·
1 Parent(s): eb12d79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -61
app.py CHANGED
@@ -1,70 +1,34 @@
1
- import os
2
- from flask import Flask, request, jsonify, send_file
3
- from gTTS import gTTS
4
- from moviepy.editor import *
5
- from PIL import Image, ImageDraw, ImageFont
6
- from flask_ngrok import run_with_ngrok # Import flask_ngrok
7
 
8
  app = Flask(__name__)
9
- run_with_ngrok(app) # This will automatically start the ngrok tunnel
10
 
11
- # Function to create TTS audio
12
- def create_tts_audio(text, filename="audio.mp3"):
13
- tts = gTTS(text=text, lang='en')
14
- tts.save(filename)
15
- return filename
16
-
17
- # Function to create an image with text
18
- def create_image_with_text(text, filename="image.png"):
19
- img = Image.new('RGB', (1920, 1080), color=(255, 255, 255))
20
- d = ImageDraw.Draw(img)
21
-
22
- try:
23
- font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 100)
24
- except IOError:
25
- font = ImageFont.load_default()
26
-
27
- bbox = d.textbbox((0, 0), text, font=font)
28
- text_width, text_height = bbox[2] - bbox[0], bbox[3] - bbox[1]
29
-
30
- position = ((1920 - text_width) // 2, (1080 - text_height) // 2)
31
- d.text(position, text, fill=(0, 0, 0), font=font)
32
-
33
- img.save(filename)
34
- return filename
35
 
36
- # Function to create video from text
37
- def create_video_from_text(text, video_filename="output_video.mp4"):
38
- audio_file = create_tts_audio(text)
39
- image_file = create_image_with_text(text)
40
-
41
- clip = ImageClip(image_file, duration=AudioFileClip(audio_file).duration)
42
- audio = AudioFileClip(audio_file)
43
-
44
- clip = clip.set_audio(audio)
45
- clip.write_videofile(video_filename, fps=24)
46
 
47
- # Clean up temporary files
48
- os.remove(image_file)
49
- os.remove(audio_file)
50
 
51
- return video_filename
 
52
 
53
- @app.route('/generate_video', methods=['POST'])
54
- def generate_video():
55
- text = request.json.get('text')
56
- if not text:
57
- return jsonify({"error": "No text provided"}), 400
58
-
59
- try:
60
- # Generate video from text
61
- video_file = create_video_from_text(text)
62
 
63
- # Send the video file back
64
- return send_file(video_file, as_attachment=True, download_name="output_video.mp4", mimetype="video/mp4")
65
-
66
- except Exception as e:
67
- return jsonify({"error": str(e)}), 500
68
 
69
- if __name__ == '__main__':
70
- app.run()
 
 
 
1
+ import requests
2
+ from flask import Flask, request, jsonify
3
+ from flask_ngrok import run_with_ngrok
 
 
 
4
 
5
  app = Flask(__name__)
6
+ run_with_ngrok(app) # Start ngrok when the app is run
7
 
8
+ @app.route("/generate_video", methods=["POST"])
9
+ def generate_video():
10
+ # This is a placeholder for your video generation logic
11
+ # You would need to replace this with your actual implementation
12
+ text = request.json.get("text", "")
13
+ # ... your video generation logic here ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # For demonstration, we'll return a simple message
16
+ return jsonify({"message": f"Video generated for text: {text}"})
 
 
 
 
 
 
 
 
17
 
18
+ if __name__ == "__main__":
19
+ app.run()
 
20
 
21
+ # In a separate cell or script, make the request:
22
+ import requests
23
 
24
+ # Replace with the actual ngrok URL you get after running the Flask app
25
+ ngrok_url = "http://<your_ngrok_url>.ngrok.io"
26
+ url = f"{ngrok_url}/generate_video"
27
+ text = {"text": "Hello, this is a sample text-to-video conversion!"}
 
 
 
 
 
28
 
29
+ response = requests.post(url, json=text)
 
 
 
 
30
 
31
+ if response.status_code == 200:
32
+ print("Success:", response.json())
33
+ else:
34
+ print("Error:", response.status_code, response.text)