Update app.py
Browse files
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 |
-
|
| 33 |
-
|
|
|
|
| 34 |
print(prompts)
|
| 35 |
if prompts == '':
|
| 36 |
-
return jsonify({"error": "
|
| 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 |
-
|
| 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 |
|