testdeep123 commited on
Commit
23fb8e6
·
verified ·
1 Parent(s): 57899c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -0
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ print("✅ Dependencies installed.")
3
+
4
+ # @title Import Libraries and Define Globals
5
+ import gradio as gr
6
+ import os
7
+ import shutil
8
+ import requests
9
+ import io
10
+ import time
11
+ import re
12
+ import random
13
+ import math
14
+ import tempfile
15
+ import traceback
16
+ import numpy as np
17
+ import soundfile as sf
18
+ import pysrt
19
+ import cv2
20
+ from kokoro import KPipeline
21
+ from moviepy.editor import (
22
+ VideoFileClip, AudioFileClip, ImageClip, concatenate_videoclips,
23
+ CompositeVideoClip, TextClip, CompositeAudioClip
24
+ )
25
+ import moviepy.video.fx.all as vfx
26
+ import moviepy.config as mpy_config
27
+ from pydub import AudioSegment
28
+ from PIL import Image, ImageDraw, ImageFont
29
+ from bs4 import BeautifulSoup
30
+ from urllib.parse import quote
31
+ from gtts import gTTS
32
+
33
+ # --- Configuration ---
34
+ PEXELS_API_KEY = 'BhJqbcdm9Vi90KqzXKAhnEHGsuFNv4irXuOjWtT761U49lRzo03qBGna'
35
+ OPENROUTER_API_KEY = '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'
36
+ OPENROUTER_MODEL = "mistralai/mistral-small-3.1-24b-instruct:free"
37
+ TEMP_FOLDER = "/tmp/temp_video_processing"
38
+ OUTPUT_VIDEO_FILENAME = "final_video.mp4"
39
+ USER_AGENT = "Mozilla/5.0"
40
+
41
+ # Initialize Kokoro TTS
42
+ try:
43
+ pipeline = KPipeline(lang_code='a', device='cpu')
44
+ print("✅ Kokoro TTS Initialized.")
45
+ except Exception as e:
46
+ print(f"⚠️ Error initializing Kokoro TTS: {e}. Using gTTS fallback.")
47
+ pipeline = None
48
+
49
+ # --- Helper Functions ---
50
+ def fix_imagemagick_policy():
51
+ try:
52
+ policy_paths = [
53
+ "/etc/ImageMagick-6/policy.xml",
54
+ "/etc/ImageMagick-7/policy.xml",
55
+ "/etc/ImageMagick/policy.xml",
56
+ "/usr/local/etc/ImageMagick-7/policy.xml"
57
+ ]
58
+ for path in policy_paths:
59
+ if os.path.exists(path):
60
+ os.system(f"sudo sed -i 's/rights=\"none\" pattern=\"PS\"/rights=\"read|write\" pattern=\"PS\"/' {path}")
61
+ os.system(f"sudo sed -i 's/rights=\"none\" pattern=\"LABEL\"/rights=\"read|write\" pattern=\"LABEL\"/' {path}")
62
+ return True
63
+ except:
64
+ return False
65
+
66
+ fix_imagemagick_policy()
67
+
68
+ # [Include all other helper functions from the original code here:
69
+ # generate_script, parse_script, search_pexels_videos, search_pexels_images,
70
+ # search_google_images, download_image, download_video, generate_media,
71
+ # generate_tts, apply_kenburns_effect, resize_to_fill, add_background_music,
72
+ # create_clip]
73
+
74
+ # --- Gradio Interface ---
75
+ def generate_video(topic, resolution, captions, bg_music):
76
+ status = []
77
+ temp_folder = f"{TEMP_FOLDER}_{int(time.time())}"
78
+ os.makedirs(temp_folder, exist_ok=True)
79
+
80
+ try:
81
+ status.append("Generating script...")
82
+ script = generate_script(topic)
83
+ if not script:
84
+ return "❌ Script generation failed", None
85
+
86
+ status.append("Parsing script...")
87
+ elements = parse_script(script)
88
+ if not elements:
89
+ return "❌ Script parsing failed", None
90
+
91
+ clips = []
92
+ for i in range(0, len(elements), 2):
93
+ media_elem = elements[i]
94
+ tts_elem = elements[i+1]
95
+
96
+ status.append(f"Processing: {media_elem['prompt']}")
97
+ media = generate_media(media_elem['prompt'])
98
+ if not media:
99
+ status.append(f"⚠️ Skipping {media_elem['prompt']} - No media found")
100
+ continue
101
+
102
+ tts_path = generate_tts(tts_elem['text'], 'en')
103
+ if not tts_path:
104
+ status.append(f"⚠️ Skipping {tts_elem['text']} - TTS failed")
105
+ continue
106
+
107
+ clip = create_clip(
108
+ media['path'],
109
+ media['asset_type'],
110
+ tts_path,
111
+ (1080, 1920) if resolution == "Short" else (1920, 1080),
112
+ duration=tts_elem['duration'],
113
+ narration_text=tts_elem['text'],
114
+ segment_index=i//2
115
+ )
116
+ if clip:
117
+ clips.append(clip)
118
+
119
+ if not clips:
120
+ return "❌ No valid clips created", None
121
+
122
+ final = concatenate_videoclips(clips)
123
+ final = add_background_music(final)
124
+
125
+ output_path = os.path.join(temp_folder, OUTPUT_VIDEO_FILENAME)
126
+ final.write_videofile(output_path, codec='libx264', fps=24, preset='ultrafast')
127
+
128
+ return "\n".join(status), output_path
129
+ except Exception as e:
130
+ return f"❌ Error: {str(e)}", None
131
+ finally:
132
+ shutil.rmtree(temp_folder, ignore_errors=True)
133
+
134
+ # Gradio Interface
135
+ iface = gr.Interface(
136
+ fn=generate_video,
137
+ inputs=[
138
+ gr.Textbox(label="Video Topic", placeholder="Enter your documentary topic here..."),
139
+ gr.Radio(label="Resolution", choices=["Short", "Full HD"], value="Short"),
140
+ gr.Checkbox(label="Add Captions", value=True),
141
+
142
+ ],
143
+ outputs=[
144
+ gr.Textbox(label="Status Log"),
145
+ gr.Video(label="Generated Video")
146
+ ],
147
+ title="AI Documentary Generator",
148
+ description="Create short documentaries with AI-generated scripts and media"
149
+ )
150
+
151
+ iface.launch(debug=True)