from flask import Flask, request, jsonify, send_file from moviepy.editor import ColorClip,ImageClip, concatenate_videoclips,VideoFileClip import traceback import uuid import glob import os import asyncio from image_fetcher import main from video import create_text_image from video2 import video_com app = Flask(__name__) SUBSPACE_URL = "https://sreepathi-ravikumar-subprocess1.hf.space/generate_video" SAVE_DIR = "/app/data/video/" os.makedirs(SAVE_DIR, exist_ok=True) @app.route("/") def home(): return "Flask Video Generator is Running" @app.route("/generate", methods=["POST"]) def generate_video(): try: data = request.get_json() prompt = data.get("duration", '').strip() prompts=prompt.replace("**","") print(prompts) if prompts == '': return jsonify({"error": "prompts be must"}), 400 image_folder = "/tmp/images" #line=prompts.splitlines() #asyncio.run(main(line)) raw_lines = prompts.splitlines(keepends=False) lines = [] i = 0 while i < len(raw_lines): line = raw_lines[i].strip() # Check if current line is a heading if line.strip().startswith("#") and (line.endswith('?') or line.endswith(':')): block = line # Start block with heading i += 1 # Accumulate body lines until next heading or 5+ lines paragraph_lines = [] while i < len(raw_lines): next_line = raw_lines[i].strip() # Stop if next line is a heading if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')): break paragraph_lines.append(next_line) i += 1 # If we've gathered enough lines for a slide, break to next if len(paragraph_lines) >= 5: break # Combine heading + paragraph if paragraph_lines: block += '\n' + '\n'.join(paragraph_lines) lines.append(block) else: # Group normal lines (not part of any heading) block_lines = [] count = 0 while i < len(raw_lines) and count < 5: next_line = raw_lines[i].strip() # If this is a heading, break to handle it separately 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)) # Print or use lines as slides id=0 try: # Send list + int to subspace res = requests.post(SUBSPACE_URL, json={"lines": lines, "id": id}) if res.status_code == 200: video_content = res.content filename = f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4" filepath = os.path.join(SAVE_DIR, filename) with open(filepath, "wb") as f: f.write(video_content) return jsonify({ "status": "success", "saved_to": filepath }) else: return jsonify({ "status": "error", "code": res.status_code, "message": res.text }) except Exception as e: return jsonify({"status": "failed", "error": str(e)}) video_path = video_com(lines) for img in image_files: os.remove(img) return send_file(video_path, mimetype='video/mp4') 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) # Example call (remove or change in your actual app)