Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,34 @@
|
|
1 |
-
import
|
2 |
-
from flask import Flask, request, jsonify
|
3 |
-
from
|
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) #
|
10 |
|
11 |
-
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
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 |
-
#
|
37 |
-
|
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 |
-
|
48 |
-
|
49 |
-
os.remove(audio_file)
|
50 |
|
51 |
-
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
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 |
-
|
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
|
70 |
-
|
|
|
|
|
|
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)
|