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