Update app.py
Browse files
app.py
CHANGED
|
@@ -57,94 +57,53 @@ def home():
|
|
| 57 |
|
| 58 |
|
| 59 |
|
|
|
|
| 60 |
@app.route("/generate", methods=["POST"])
|
| 61 |
def generate_video():
|
| 62 |
try:
|
| 63 |
data = request.get_json()
|
| 64 |
userprompt = data.get("duration", '').strip()
|
| 65 |
-
prompt=extract_english_paragraphs(userprompt)
|
| 66 |
-
lang=extract_native_text(userprompt)
|
| 67 |
-
prompts=prompt.replace("**","")
|
| 68 |
print(prompts)
|
|
|
|
| 69 |
if prompts == '':
|
| 70 |
return jsonify({"error": "prompts be must"}), 400
|
| 71 |
-
|
| 72 |
-
#line
|
| 73 |
-
#asyncio.run(main(line))
|
| 74 |
-
raw_lines = prompts.splitlines(keepends=False)
|
| 75 |
-
lines = []
|
| 76 |
-
|
| 77 |
-
i = 0
|
| 78 |
-
while i < len(raw_lines):
|
| 79 |
-
line = raw_lines[i].strip()
|
| 80 |
-
|
| 81 |
-
# Check if current line is a heading
|
| 82 |
-
if line.strip().startswith("#") and (line.endswith('?') or line.endswith(':')):
|
| 83 |
-
block = line # Start block with heading
|
| 84 |
-
i += 1
|
| 85 |
-
|
| 86 |
-
# Accumulate body lines until next heading or 5+ lines
|
| 87 |
-
paragraph_lines = []
|
| 88 |
-
while i < len(raw_lines):
|
| 89 |
-
next_line = raw_lines[i].strip()
|
| 90 |
-
|
| 91 |
-
# Stop if next line is a heading
|
| 92 |
-
if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
|
| 93 |
-
break
|
| 94 |
-
|
| 95 |
-
paragraph_lines.append(next_line)
|
| 96 |
-
i += 1
|
| 97 |
-
|
| 98 |
-
# If we've gathered enough lines for a slide, break to next
|
| 99 |
-
if len(paragraph_lines) >= 5:
|
| 100 |
-
break
|
| 101 |
-
|
| 102 |
-
# Combine heading + paragraph
|
| 103 |
-
if paragraph_lines:
|
| 104 |
-
block += '\n' + '\n'.join(paragraph_lines)
|
| 105 |
-
|
| 106 |
-
lines.append(block)
|
| 107 |
-
|
| 108 |
-
else:
|
| 109 |
-
# Group normal lines (not part of any heading)
|
| 110 |
-
block_lines = []
|
| 111 |
-
count = 0
|
| 112 |
-
|
| 113 |
-
while i < len(raw_lines) and count < 5:
|
| 114 |
-
next_line = raw_lines[i].strip()
|
| 115 |
-
|
| 116 |
-
# If this is a heading, break to handle it separately
|
| 117 |
-
if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
|
| 118 |
-
break
|
| 119 |
-
|
| 120 |
-
block_lines.append(next_line)
|
| 121 |
-
i += 1
|
| 122 |
-
count += 1
|
| 123 |
-
|
| 124 |
-
if block_lines:
|
| 125 |
-
lines.append('\n'.join(block_lines))
|
| 126 |
|
| 127 |
-
|
| 128 |
-
if len(lines)==1:
|
| 129 |
-
try:
|
| 130 |
-
os.remove("/app/data/video/clip0.mp4")
|
| 131 |
-
os.remove("/app/data/sound/audio0.mp3")
|
| 132 |
-
except Exception as e:
|
| 133 |
-
print(e)
|
| 134 |
-
#image_folder="/tmp/images"
|
| 135 |
-
#image_olst=[]
|
| 136 |
-
#create_text_image(lines[0],0,image_olst)
|
| 137 |
-
#image_files = sorted(glob.glob(os.path.join(image_folder, "*.png")))
|
| 138 |
-
#if not image_files:
|
| 139 |
-
#raise ValueError("No images found in folder!")
|
| 140 |
video_link = "/app/data/video/clip0.mp4"
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
return send_file(video_link, mimetype='video/mp4')
|
| 145 |
|
| 146 |
else:
|
| 147 |
-
|
|
|
|
| 148 |
except Exception as e:
|
| 149 |
traceback.print_exc()
|
| 150 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
+
@app.route("/generate", methods=["POST"])
|
| 61 |
@app.route("/generate", methods=["POST"])
|
| 62 |
def generate_video():
|
| 63 |
try:
|
| 64 |
data = request.get_json()
|
| 65 |
userprompt = data.get("duration", '').strip()
|
| 66 |
+
prompt = extract_english_paragraphs(userprompt)
|
| 67 |
+
lang = extract_native_text(userprompt)
|
| 68 |
+
prompts = prompt.replace("**", "")
|
| 69 |
print(prompts)
|
| 70 |
+
|
| 71 |
if prompts == '':
|
| 72 |
return jsonify({"error": "prompts be must"}), 400
|
| 73 |
+
|
| 74 |
+
# ... [your existing line processing code] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
if len(lines) == 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
video_link = "/app/data/video/clip0.mp4"
|
| 78 |
+
audio_link = "/app/data/sound/audio0.mp3"
|
| 79 |
+
|
| 80 |
+
# Generate the video
|
| 81 |
+
try:
|
| 82 |
+
video_func(0, lines, lang)
|
| 83 |
+
except Exception as video_error:
|
| 84 |
+
print(f"Video generation failed: {video_error}")
|
| 85 |
+
traceback.print_exc()
|
| 86 |
+
return jsonify({"error": f"Video generation failed: {str(video_error)}"}), 500
|
| 87 |
+
|
| 88 |
+
# Verify the file was created and has content
|
| 89 |
+
if not os.path.exists(video_link):
|
| 90 |
+
return jsonify({"error": "Video file was not created"}), 500
|
| 91 |
+
|
| 92 |
+
if os.path.getsize(video_link) == 0:
|
| 93 |
+
return jsonify({"error": "Video file is empty"}), 500
|
| 94 |
+
|
| 95 |
+
# Clean up old files after successful generation
|
| 96 |
+
try:
|
| 97 |
+
if os.path.exists(audio_link):
|
| 98 |
+
os.remove(audio_link)
|
| 99 |
+
except Exception as cleanup_error:
|
| 100 |
+
print(f"Cleanup error: {cleanup_error}")
|
| 101 |
+
|
| 102 |
return send_file(video_link, mimetype='video/mp4')
|
| 103 |
|
| 104 |
else:
|
| 105 |
+
return jsonify({"error": "Multiple lines not yet implemented"}), 501
|
| 106 |
+
|
| 107 |
except Exception as e:
|
| 108 |
traceback.print_exc()
|
| 109 |
return jsonify({"error": str(e)}), 500
|