Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify, send_file | |
| import traceback | |
| import uuid | |
| import glob | |
| import asyncio | |
| from image_fetcher import main | |
| from video import create_text_image | |
| from moviepy.editor import * | |
| from PIL import Image | |
| import pytesseract | |
| import numpy as np | |
| from gtts import gTTS | |
| from mutagen.mp3 import MP3 | |
| from gtts import gTTS | |
| import os | |
| def video_func(id,lines): | |
| tts = gTTS(text=lines[id], lang='ta', slow=False) | |
| filename = "/tmp/audio"+str(id)+".mp3" | |
| tts.save(filename) | |
| if os.path.exists(filename): | |
| audio = MP3(filename) | |
| duration = audio.info.length | |
| IMAGE_PATH = "/tmp/images/slide"+str(id)+".png" # Ensure this path is correct | |
| VIDEO_DURATION = duration # seconds | |
| HIGHLIGHT_COLOR = (255, 255, 0) # Yellow highlight | |
| HIGHLIGHT_OPACITY = 0.5 # Semi-transparent | |
| # --- OCR STEP --- | |
| img = Image.open(IMAGE_PATH) | |
| data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT) | |
| # Extract words and their positions | |
| words = [] | |
| for i in range(len(data['text'])): | |
| word = data['text'][i].strip() | |
| if word and int(data['conf'][i]) > 60: | |
| x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i] | |
| words.append({'text': word, 'box': (x, y, w, h)}) | |
| # --- BASE IMAGE CLIP --- | |
| image_clip = ImageClip(IMAGE_PATH).set_duration(VIDEO_DURATION) | |
| # --- HIGHLIGHT WORDS ONE BY ONE --- | |
| n_words = len(words) | |
| highlight_duration = VIDEO_DURATION / n_words | |
| highlight_clips = [] | |
| for i, word in enumerate(words): | |
| x, y, w, h = word['box'] | |
| start = i * highlight_duration | |
| end = start + highlight_duration | |
| # Create highlight rectangle | |
| rect = ColorClip(size=(w, h), color=HIGHLIGHT_COLOR) | |
| rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end) | |
| highlight_clips.append(rect) | |
| # --- FINAL VIDEO -- | |
| final_clip = CompositeVideoClip([image_clip] + highlight_clips) | |
| audio = AudioFileClip(filename) | |
| final_clip = final_clip.set_audio(audio) | |
| final_clip.write_videofile("/tmp/clip"+str(id)+".mp4", fps=24) | |
| app = Flask(__name__) | |
| def home(): | |
| return "Flask Video Generator is Running" | |
| 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 | |
| image_olst=[] | |
| for id in range(len(lines)): | |
| create_text_image(lines[id],id,image_olst) | |
| for i in range(len(lines)): | |
| video_func(i,lines) | |
| clips = [] | |
| for id in range(len(lines)): | |
| clip = VideoFileClip(f"clip{id}.mp4") | |
| clips.append(clip) | |
| final_video = concatenate_videoclips(clips) | |
| final_video.write_videofile("/tmp/final_output.mp4", fps=24) | |
| for img in image_olst: | |
| os.remove(img) | |
| return send_file("/tmp/final_output.mp4", 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) | |