Update app.py
Browse files
app.py
CHANGED
|
@@ -27,88 +27,49 @@ def home():
|
|
| 27 |
def generate_video():
|
| 28 |
try:
|
| 29 |
data = request.get_json()
|
| 30 |
-
prompt = data.get("duration", '').strip()
|
| 31 |
-
prompts=prompt.replace("**","")
|
| 32 |
print(prompts)
|
| 33 |
if prompts == '':
|
| 34 |
-
return jsonify({"error": "
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Group normal lines (not part of any heading)
|
| 74 |
-
block_lines = []
|
| 75 |
-
count = 0
|
| 76 |
-
|
| 77 |
-
while i < len(raw_lines) and count < 5:
|
| 78 |
-
next_line = raw_lines[i].strip()
|
| 79 |
-
|
| 80 |
-
# If this is a heading, break to handle it separately
|
| 81 |
-
if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
|
| 82 |
-
break
|
| 83 |
-
|
| 84 |
-
block_lines.append(next_line)
|
| 85 |
-
i += 1
|
| 86 |
-
count += 1
|
| 87 |
-
|
| 88 |
-
if block_lines:
|
| 89 |
-
lines.append('\n'.join(block_lines))
|
| 90 |
-
|
| 91 |
-
# Print or use lines as slides
|
| 92 |
-
if len(lines)==1:
|
| 93 |
-
try:
|
| 94 |
-
os.remove("/app/data/video/clip0.mp4")
|
| 95 |
-
os.remove("/app/data/sound/audio0.mp3")
|
| 96 |
-
except Exception as e:
|
| 97 |
-
print(e)
|
| 98 |
-
#image_folder="/tmp/images"
|
| 99 |
-
#image_olst=[]
|
| 100 |
-
#create_text_image(lines[0],0,image_olst)
|
| 101 |
-
#image_files = sorted(glob.glob(os.path.join(image_folder, "*.png")))
|
| 102 |
-
#if not image_files:
|
| 103 |
-
#raise ValueError("No images found in folder!")
|
| 104 |
-
video_link = "/app/data/video/clip0.mp4"
|
| 105 |
-
video_func(0,lines)
|
| 106 |
-
#for img in image_files:
|
| 107 |
-
#os.remove(img)
|
| 108 |
-
return send_file(video_link, mimetype='video/mp4')
|
| 109 |
-
|
| 110 |
-
else:
|
| 111 |
-
print("learn deeper")
|
| 112 |
except Exception as e:
|
| 113 |
traceback.print_exc()
|
| 114 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 27 |
def generate_video():
|
| 28 |
try:
|
| 29 |
data = request.get_json()
|
| 30 |
+
prompt = data.get("duration", '').strip() # Note: This key is "duration" but seems like a typo; consider changing to "prompt" if possible
|
| 31 |
+
prompts = prompt.replace("**", "")
|
| 32 |
print(prompts)
|
| 33 |
if prompts == '':
|
| 34 |
+
return jsonify({"error": "prompt must be provided"}), 400
|
| 35 |
+
|
| 36 |
+
# Helper to extract only the last paragraph (assumes paragraphs separated by blank lines)
|
| 37 |
+
def get_last_paragraph(text):
|
| 38 |
+
paragraphs = []
|
| 39 |
+
current = []
|
| 40 |
+
for line in text.splitlines():
|
| 41 |
+
if line.strip():
|
| 42 |
+
current.append(line)
|
| 43 |
+
else:
|
| 44 |
+
if current:
|
| 45 |
+
paragraphs.append('\n'.join(current))
|
| 46 |
+
current = []
|
| 47 |
+
if current:
|
| 48 |
+
paragraphs.append('\n'.join(current))
|
| 49 |
+
return paragraphs[-1] if paragraphs else '' # Return the last one, or empty if none
|
| 50 |
+
|
| 51 |
+
last_text = get_last_paragraph(prompts)
|
| 52 |
+
if not last_text:
|
| 53 |
+
return jsonify({"error": "No valid text found in prompt"}), 400
|
| 54 |
+
|
| 55 |
+
lines = [last_text] # Force to single block: only the latest text
|
| 56 |
+
|
| 57 |
+
# Generate unique id for this generation to avoid file conflicts
|
| 58 |
+
unique_id = str(uuid.uuid4())[:8] # Short unique string
|
| 59 |
+
|
| 60 |
+
# Remove any existing files for this unique_id (optional, but safer)
|
| 61 |
+
try:
|
| 62 |
+
os.remove(os.path.join(CLIPS_DIR, f"clip{unique_id}.mp4"))
|
| 63 |
+
os.remove(os.path.join(AUDIO_DIR, f"audio{unique_id}.mp3"))
|
| 64 |
+
except Exception:
|
| 65 |
+
pass # Ignore if files don't exist
|
| 66 |
+
|
| 67 |
+
video_link = os.path.join(CLIPS_DIR, f"clip{unique_id}.mp4")
|
| 68 |
+
video_func(unique_id, lines) # Pass unique_id instead of 0
|
| 69 |
+
|
| 70 |
+
# For now, assuming single clip; if you add multi-clip support later, concatenate here
|
| 71 |
+
return send_file(video_link, mimetype='video/mp4')
|
| 72 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
traceback.print_exc()
|
| 75 |
return jsonify({"error": str(e)}), 500
|