|
|
from flask import Flask, request, jsonify, send_file |
|
|
from moviepy.editor import ColorClip,ImageClip, concatenate_videoclips,VideoFileClip |
|
|
import traceback |
|
|
import uuid |
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed |
|
|
import glob |
|
|
import os |
|
|
import requests |
|
|
import asyncio |
|
|
from image_fetcher import main |
|
|
from video import create_text_image |
|
|
from video2 import video_func |
|
|
import re |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
SAVE_DIR = "/app/data/video/" |
|
|
os.makedirs(SAVE_DIR, exist_ok=True) |
|
|
CLIPS_DIR= "/app/data/video/" |
|
|
os.makedirs(SAVE_DIR, exist_ok=True) |
|
|
|
|
|
|
|
|
|
|
|
def extract_english_paragraphs(text): |
|
|
""" |
|
|
Extract paragraphs that contain only English text |
|
|
""" |
|
|
paragraphs = text.split('\n\n') |
|
|
english_paragraphs = [paragraphs[0]] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return '\n\n'.join(english_paragraphs) |
|
|
|
|
|
def extract_native_text(text): |
|
|
paragraphs = text.split('\n\n') |
|
|
nativelang_paragraphs = paragraphs[1] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nativelang_paragraphs |
|
|
|
|
|
|
|
|
@app.route("/") |
|
|
def home(): |
|
|
return "Flask Video Generator is Running" |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/generate", methods=["POST"]) |
|
|
def generate_video(): |
|
|
try: |
|
|
data = request.get_json() |
|
|
userprompt = data.get("duration", '').strip() |
|
|
prompt=extract_english_paragraphs(userprompt) |
|
|
lang=extract_native_text(userprompt) |
|
|
prompts=prompt.replace("**","") |
|
|
print(prompts) |
|
|
if prompts == '': |
|
|
return jsonify({"error": "prompts be must"}), 400 |
|
|
image_folder = "/tmp/images" |
|
|
|
|
|
|
|
|
raw_lines = prompts.splitlines(keepends=False) |
|
|
lines = [] |
|
|
|
|
|
i = 0 |
|
|
while i < len(raw_lines): |
|
|
line = raw_lines[i].strip() |
|
|
|
|
|
|
|
|
if line.strip().startswith("#") and (line.endswith('?') or line.endswith(':')): |
|
|
block = line |
|
|
i += 1 |
|
|
|
|
|
|
|
|
paragraph_lines = [] |
|
|
while i < len(raw_lines): |
|
|
next_line = raw_lines[i].strip() |
|
|
|
|
|
|
|
|
if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')): |
|
|
break |
|
|
|
|
|
paragraph_lines.append(next_line) |
|
|
i += 1 |
|
|
|
|
|
|
|
|
if len(paragraph_lines) >= 5: |
|
|
break |
|
|
|
|
|
|
|
|
if paragraph_lines: |
|
|
block += '\n' + '\n'.join(paragraph_lines) |
|
|
|
|
|
lines.append(block) |
|
|
|
|
|
else: |
|
|
|
|
|
block_lines = [] |
|
|
count = 0 |
|
|
|
|
|
while i < len(raw_lines) and count < 5: |
|
|
next_line = raw_lines[i].strip() |
|
|
|
|
|
|
|
|
if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')): |
|
|
break |
|
|
|
|
|
block_lines.append(next_line) |
|
|
i += 1 |
|
|
count += 1 |
|
|
|
|
|
if block_lines: |
|
|
lines.append('\n'.join(block_lines)) |
|
|
|
|
|
|
|
|
if len(lines)==1: |
|
|
try: |
|
|
os.remove("/app/data/video/clip0.mp4") |
|
|
os.remove("/app/data/sound/audio0.mp3") |
|
|
except Exception as e: |
|
|
print(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
video_link = "/app/data/video/clip0.mp4" |
|
|
video_func(0,lines,lang) |
|
|
|
|
|
|
|
|
return send_file(video_link, mimetype='video/mp4') |
|
|
|
|
|
else: |
|
|
print("learn deeper") |
|
|
except Exception as e: |
|
|
traceback.print_exc() |
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.run(host="0.0.0.0", port=7860) |
|
|
|
|
|
|