sreepathi-ravikumar commited on
Commit
ca42371
·
verified ·
1 Parent(s): 5b103a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -41
app.py CHANGED
@@ -10,6 +10,7 @@ import asyncio
10
  from image_fetcher import main
11
  from video import create_text_image
12
  from video2 import video_func
 
13
 
14
  app = Flask(__name__)
15
 
@@ -19,6 +20,35 @@ CLIPS_DIR= "/app/data/video/"
19
  os.makedirs(SAVE_DIR, exist_ok=True)
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  @app.route("/")
23
  def home():
24
  return "Flask Video Generator is Running"
@@ -29,53 +59,94 @@ def home():
29
  def generate_video():
30
  try:
31
  data = request.get_json()
32
- prompt = data.get("duration", '').strip() # Note: This key is "duration" but seems like a typo; consider changing to "prompt" if possible
33
- prompts = prompt.replace("**", "")
 
34
  print(prompts)
35
  if prompts == '':
36
- return jsonify({"error": "prompt must be provided"}), 400
37
-
38
- # Helper to extract only the last paragraph (assumes paragraphs separated by blank lines)
39
- def get_last_paragraph(text):
40
- paragraphs = []
41
- current = []
42
- for line in text.splitlines():
43
- if line.strip():
44
- current.append(line)
45
- else:
46
- if current:
47
- paragraphs.append('\n'.join(current))
48
- current = []
49
- if current:
50
- paragraphs.append('\n'.join(current))
51
- return paragraphs[-1] if paragraphs else '' # Return the last one, or empty if none
52
-
53
- last_text = get_last_paragraph(prompts)
54
- if not last_text:
55
- return jsonify({"error": "No valid text found in prompt"}), 400
56
-
57
- lines = [last_text] # Force to single block: only the latest text
58
-
59
- # Generate unique id for this generation to avoid file conflicts
60
- unique_id = str(uuid.uuid4())[:8] # Short unique string
61
-
62
- # Remove any existing files for this unique_id (optional, but safer)
63
- try:
64
- os.remove(os.path.join(CLIPS_DIR, f"clip{unique_id}.mp4"))
65
- os.remove(os.path.join(AUDIO_DIR, f"audio{unique_id}.mp3"))
66
- except Exception:
67
- pass # Ignore if files don't exist
68
-
69
- video_link = os.path.join(CLIPS_DIR, f"clip{unique_id}.mp4")
70
- video_func(unique_id, lines) # Pass unique_id instead of 0
71
-
72
- # For now, assuming single clip; if you add multi-clip support later, concatenate here
73
- return send_file(video_link, mimetype='video/mp4')
74
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  except Exception as e:
76
  traceback.print_exc()
77
  return jsonify({"error": str(e)}), 500
78
 
 
79
  if __name__ == "__main__":
80
  app.run(host="0.0.0.0", port=7860)
81
 
 
10
  from image_fetcher import main
11
  from video import create_text_image
12
  from video2 import video_func
13
+ import re
14
 
15
  app = Flask(__name__)
16
 
 
20
  os.makedirs(SAVE_DIR, exist_ok=True)
21
 
22
 
23
+
24
+ def extract_english_paragraphs(text):
25
+ """
26
+ Extract paragraphs that contain only English text
27
+ """
28
+ paragraphs = text.split('\n\n')
29
+ english_paragraphs = []
30
+
31
+ for para in paragraphs:
32
+ # Check if the paragraph contains only English characters
33
+ if not re.search(r'[^\x00-\x7F]', para):
34
+ english_paragraphs.append(para.strip())
35
+
36
+ return '\n\n'.join(english_paragraphs)
37
+
38
+ def extract_native_text(text):
39
+ pattern = r'[^\x00-\x7F]'
40
+
41
+ # Search for the first non-English character
42
+ match = re.search(pattern, text)
43
+
44
+ if match:
45
+ # Return everything from the first non-English character
46
+ return text[match.start():]
47
+ else:
48
+ # If no non-English characters found, return empty string
49
+ return ""
50
+
51
+
52
  @app.route("/")
53
  def home():
54
  return "Flask Video Generator is Running"
 
59
  def generate_video():
60
  try:
61
  data = request.get_json()
62
+ userprompt = data.get("duration", '').strip()
63
+ promt=extract_english_paragraphs(userprompt)
64
+ prompts=prompt.replace("**","")
65
  print(prompts)
66
  if prompts == '':
67
+ return jsonify({"error": "prompts be must"}), 400
68
+ image_folder = "/tmp/images"
69
+ #line=prompts.splitlines()
70
+ #asyncio.run(main(line))
71
+ raw_lines = prompts.splitlines(keepends=False)
72
+ lines = []
73
+
74
+ i = 0
75
+ while i < len(raw_lines):
76
+ line = raw_lines[i].strip()
77
+
78
+ # Check if current line is a heading
79
+ if line.strip().startswith("#") and (line.endswith('?') or line.endswith(':')):
80
+ block = line # Start block with heading
81
+ i += 1
82
+
83
+ # Accumulate body lines until next heading or 5+ lines
84
+ paragraph_lines = []
85
+ while i < len(raw_lines):
86
+ next_line = raw_lines[i].strip()
87
+
88
+ # Stop if next line is a heading
89
+ if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
90
+ break
91
+
92
+ paragraph_lines.append(next_line)
93
+ i += 1
94
+
95
+ # If we've gathered enough lines for a slide, break to next
96
+ if len(paragraph_lines) >= 5:
97
+ break
98
+
99
+ # Combine heading + paragraph
100
+ if paragraph_lines:
101
+ block += '\n' + '\n'.join(paragraph_lines)
102
+
103
+ lines.append(block)
104
+
105
+ else:
106
+ # Group normal lines (not part of any heading)
107
+ block_lines = []
108
+ count = 0
109
+
110
+ while i < len(raw_lines) and count < 5:
111
+ next_line = raw_lines[i].strip()
112
+
113
+ # If this is a heading, break to handle it separately
114
+ if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
115
+ break
116
+
117
+ block_lines.append(next_line)
118
+ i += 1
119
+ count += 1
120
+
121
+ if block_lines:
122
+ lines.append('\n'.join(block_lines))
123
+
124
+ # Print or use lines as slides
125
+ if len(lines)==1:
126
+ try:
127
+ os.remove("/app/data/video/clip0.mp4")
128
+ os.remove("/app/data/sound/audio0.mp3")
129
+ except Exception as e:
130
+ print(e)
131
+ #image_folder="/tmp/images"
132
+ #image_olst=[]
133
+ #create_text_image(lines[0],0,image_olst)
134
+ #image_files = sorted(glob.glob(os.path.join(image_folder, "*.png")))
135
+ #if not image_files:
136
+ #raise ValueError("No images found in folder!")
137
+ video_link = "/app/data/video/clip0.mp4"
138
+ video_func(0,lines)
139
+ #for img in image_files:
140
+ #os.remove(img)
141
+ return send_file(video_link, mimetype='video/mp4')
142
+
143
+ else:
144
+ print("learn deeper")
145
  except Exception as e:
146
  traceback.print_exc()
147
  return jsonify({"error": str(e)}), 500
148
 
149
+
150
  if __name__ == "__main__":
151
  app.run(host="0.0.0.0", port=7860)
152