sreepathi-ravikumar commited on
Commit
d82ef8a
·
verified ·
1 Parent(s): 62d54c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -80
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": "prompts be must"}), 400
35
- image_folder = "/tmp/images"
36
- #line=prompts.splitlines()
37
- #asyncio.run(main(line))
38
- raw_lines = prompts.splitlines(keepends=False)
39
- lines = []
40
-
41
- i = 0
42
- while i < len(raw_lines):
43
- line = raw_lines[i].strip()
44
-
45
- # Check if current line is a heading
46
- if line.strip().startswith("#") and (line.endswith('?') or line.endswith(':')):
47
- block = line # Start block with heading
48
- i += 1
49
-
50
- # Accumulate body lines until next heading or 5+ lines
51
- paragraph_lines = []
52
- while i < len(raw_lines):
53
- next_line = raw_lines[i].strip()
54
-
55
- # Stop if next line is a heading
56
- if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
57
- break
58
-
59
- paragraph_lines.append(next_line)
60
- i += 1
61
-
62
- # If we've gathered enough lines for a slide, break to next
63
- if len(paragraph_lines) >= 5:
64
- break
65
-
66
- # Combine heading + paragraph
67
- if paragraph_lines:
68
- block += '\n' + '\n'.join(paragraph_lines)
69
-
70
- lines.append(block)
71
-
72
- else:
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