Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
| 2 |
-
from moviepy.editor import
|
| 3 |
import traceback
|
| 4 |
import uuid
|
| 5 |
import glob
|
| 6 |
import os
|
| 7 |
-
import asyncio
|
| 8 |
from image_fetcher import main
|
| 9 |
from video import create_text_image
|
| 10 |
from video2 import video_func
|
|
@@ -20,86 +19,81 @@ def generate_video():
|
|
| 20 |
try:
|
| 21 |
data = request.get_json()
|
| 22 |
prompt = data.get("duration", '').strip()
|
| 23 |
-
prompts=prompt.replace("**","")
|
| 24 |
print(prompts)
|
| 25 |
if prompts == '':
|
| 26 |
-
return jsonify({"error": "prompts be
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
lines = []
|
| 32 |
-
|
| 33 |
i = 0
|
| 34 |
while i < len(raw_lines):
|
| 35 |
line = raw_lines[i].strip()
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if line.strip().startswith("#") and (line.endswith('?') or line.endswith(':')):
|
| 39 |
-
block = line # Start block with heading
|
| 40 |
i += 1
|
| 41 |
-
|
| 42 |
-
# Accumulate body lines until next heading or 5+ lines
|
| 43 |
paragraph_lines = []
|
| 44 |
while i < len(raw_lines):
|
| 45 |
next_line = raw_lines[i].strip()
|
| 46 |
-
|
| 47 |
-
# Stop if next line is a heading
|
| 48 |
-
if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
|
| 49 |
break
|
| 50 |
-
|
| 51 |
paragraph_lines.append(next_line)
|
| 52 |
i += 1
|
| 53 |
-
|
| 54 |
-
# If we've gathered enough lines for a slide, break to next
|
| 55 |
if len(paragraph_lines) >= 5:
|
| 56 |
break
|
| 57 |
-
|
| 58 |
-
# Combine heading + paragraph
|
| 59 |
if paragraph_lines:
|
| 60 |
block += '\n' + '\n'.join(paragraph_lines)
|
| 61 |
-
|
| 62 |
lines.append(block)
|
| 63 |
-
|
| 64 |
else:
|
| 65 |
-
# Group normal lines (not part of any heading)
|
| 66 |
block_lines = []
|
| 67 |
count = 0
|
| 68 |
-
|
| 69 |
while i < len(raw_lines) and count < 5:
|
| 70 |
next_line = raw_lines[i].strip()
|
| 71 |
-
|
| 72 |
-
# If this is a heading, break to handle it separately
|
| 73 |
-
if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
|
| 74 |
break
|
| 75 |
-
|
| 76 |
block_lines.append(next_line)
|
| 77 |
i += 1
|
| 78 |
count += 1
|
| 79 |
-
|
| 80 |
if block_lines:
|
| 81 |
lines.append('\n'.join(block_lines))
|
| 82 |
-
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
image_olst=[]
|
| 86 |
for id in range(len(lines)):
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
if not image_files:
|
| 90 |
raise ValueError("No images found in folder!")
|
| 91 |
-
|
| 92 |
-
video_location = video_func(0,lines)
|
| 93 |
-
for img in image_files:
|
| 94 |
-
os.remove(img)
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
traceback.print_exc()
|
| 100 |
return jsonify({"error": str(e)}), 500
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
app.run(host="0.0.0.0", port=7860)
|
| 104 |
-
|
| 105 |
-
# Example call (remove or change in your actual app)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
| 2 |
+
from moviepy.editor import VideoFileClip, concatenate_videoclips
|
| 3 |
import traceback
|
| 4 |
import uuid
|
| 5 |
import glob
|
| 6 |
import os
|
|
|
|
| 7 |
from image_fetcher import main
|
| 8 |
from video import create_text_image
|
| 9 |
from video2 import video_func
|
|
|
|
| 19 |
try:
|
| 20 |
data = request.get_json()
|
| 21 |
prompt = data.get("duration", '').strip()
|
| 22 |
+
prompts = prompt.replace("**", "")
|
| 23 |
print(prompts)
|
| 24 |
if prompts == '':
|
| 25 |
+
return jsonify({"error": "prompts must not be empty"}), 400
|
| 26 |
+
|
| 27 |
+
# Ensure all temp dirs exist
|
| 28 |
+
for d in ["/tmp/images", "/tmp/sound", "/tmp/video"]:
|
| 29 |
+
os.makedirs(d, exist_ok=True)
|
| 30 |
+
|
| 31 |
+
raw_lines = prompts.splitlines()
|
| 32 |
lines = []
|
| 33 |
+
|
| 34 |
i = 0
|
| 35 |
while i < len(raw_lines):
|
| 36 |
line = raw_lines[i].strip()
|
| 37 |
+
if line.startswith("#") and (line.endswith('?') or line.endswith(':')):
|
| 38 |
+
block = line
|
|
|
|
|
|
|
| 39 |
i += 1
|
|
|
|
|
|
|
| 40 |
paragraph_lines = []
|
| 41 |
while i < len(raw_lines):
|
| 42 |
next_line = raw_lines[i].strip()
|
| 43 |
+
if next_line.startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
|
|
|
|
|
|
|
| 44 |
break
|
|
|
|
| 45 |
paragraph_lines.append(next_line)
|
| 46 |
i += 1
|
|
|
|
|
|
|
| 47 |
if len(paragraph_lines) >= 5:
|
| 48 |
break
|
|
|
|
|
|
|
| 49 |
if paragraph_lines:
|
| 50 |
block += '\n' + '\n'.join(paragraph_lines)
|
|
|
|
| 51 |
lines.append(block)
|
|
|
|
| 52 |
else:
|
|
|
|
| 53 |
block_lines = []
|
| 54 |
count = 0
|
|
|
|
| 55 |
while i < len(raw_lines) and count < 5:
|
| 56 |
next_line = raw_lines[i].strip()
|
| 57 |
+
if next_line.startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
|
|
|
|
|
|
|
| 58 |
break
|
|
|
|
| 59 |
block_lines.append(next_line)
|
| 60 |
i += 1
|
| 61 |
count += 1
|
|
|
|
| 62 |
if block_lines:
|
| 63 |
lines.append('\n'.join(block_lines))
|
| 64 |
+
|
| 65 |
+
# Generate images
|
| 66 |
+
image_olst = []
|
|
|
|
| 67 |
for id in range(len(lines)):
|
| 68 |
+
create_text_image(lines[id], id, image_olst)
|
| 69 |
+
|
| 70 |
+
image_files = sorted(glob.glob("/tmp/images/*.png"))
|
| 71 |
if not image_files:
|
| 72 |
raise ValueError("No images found in folder!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Generate video clips
|
| 75 |
+
for id in range(len(lines)):
|
| 76 |
+
video_func(id, lines)
|
| 77 |
+
|
| 78 |
+
clips = []
|
| 79 |
+
for id in range(len(lines)):
|
| 80 |
+
clip_path = f"/tmp/video/clip{id}.mp4"
|
| 81 |
+
clip = VideoFileClip(clip_path)
|
| 82 |
+
clips.append(clip)
|
| 83 |
+
|
| 84 |
+
final_video = concatenate_videoclips(clips)
|
| 85 |
+
output_path = "/tmp/final_output.mp4"
|
| 86 |
+
final_video.write_videofile(output_path, fps=24)
|
| 87 |
+
|
| 88 |
+
# Clean up
|
| 89 |
+
for f in image_files:
|
| 90 |
+
os.remove(f)
|
| 91 |
+
|
| 92 |
+
return send_file(output_path, mimetype='video/mp4')
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
traceback.print_exc()
|
| 96 |
return jsonify({"error": str(e)}), 500
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|