Update app.py
Browse files
app.py
CHANGED
|
@@ -14,22 +14,302 @@ from deep_translator import GoogleTranslator
|
|
| 14 |
import wikipediaapi
|
| 15 |
from groq import Groq
|
| 16 |
|
| 17 |
-
# Import functions from your Python files
|
| 18 |
-
from script_generator import script_gen_and_translate
|
| 19 |
-
from urdu_tts_video import urdu_tts_and_video
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
import wikipediaapi
|
| 15 |
from groq import Groq
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
|
| 19 |
+
# β
Set API Key
|
| 20 |
+
os.environ["GROQ_API_KEY"] = "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" # Replace with your API key
|
| 21 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def fetch_wikipedia_summary(topic):
|
| 25 |
+
wiki_wiki = wikipediaapi.Wikipedia(
|
| 26 |
+
user_agent="EducationalScriptApp/1.0",
|
| 27 |
+
language="en"
|
| 28 |
+
)
|
| 29 |
+
page = wiki_wiki.page(topic)
|
| 30 |
+
return page.summary if page.exists() else "No Wikipedia summary available."
|
| 31 |
+
|
| 32 |
+
def generate_script(topic, duration):
|
| 33 |
+
try:
|
| 34 |
+
factual_content = fetch_wikipedia_summary(topic)
|
| 35 |
+
words_per_minute = 130
|
| 36 |
+
target_words = duration * words_per_minute
|
| 37 |
+
|
| 38 |
+
response = client.chat.completions.create(
|
| 39 |
+
messages=[{"role": "user", "content": f"Format the following factual content into a well-structured educational script in English with approximately {target_words} words: \n{factual_content}"}],
|
| 40 |
+
model="llama-3.3-70b-versatile"
|
| 41 |
+
)
|
| 42 |
+
return response.choices[0].message.content
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"β Error in script generation: {str(e)}"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# β
Function to Extract Keywords Using YAKE
|
| 48 |
+
def extract_keywords(script):
|
| 49 |
+
try:
|
| 50 |
+
kw_extractor = yake.KeywordExtractor(
|
| 51 |
+
lan="en", # Language
|
| 52 |
+
n=3, # Max number of words in a keyword phrase (trigrams)
|
| 53 |
+
dedupLim=0.9, # Reduce redundant phrases
|
| 54 |
+
# top=10 # Extract top 10 keywords
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
keywords = kw_extractor.extract_keywords(script)
|
| 58 |
+
return ", ".join([kw[0] for kw in keywords]) # β
Extract only the keyword text
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"β Error extracting keywords: {str(e)}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def save_keywords_file(keywords, topic):
|
| 64 |
+
today = datetime.today().strftime('%Y_%b_%d')
|
| 65 |
+
filename = f"Keywords/{topic}_Keyword_{today}.txt"
|
| 66 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
| 67 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 68 |
+
f.write(keywords)
|
| 69 |
+
return filename
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def translate_to_urdu(english_script):
|
| 74 |
+
try:
|
| 75 |
+
# β
Define a max chunk size (Google Translator has a limit)
|
| 76 |
+
max_chunk_size = 4500 # Stay below 5000 to be safe
|
| 77 |
+
chunks = [english_script[i:i + max_chunk_size] for i in range(0, len(english_script), max_chunk_size)]
|
| 78 |
+
|
| 79 |
+
translated_chunks = []
|
| 80 |
+
for chunk in chunks:
|
| 81 |
+
translated_chunk = GoogleTranslator(source='en', target='ur').translate(chunk)
|
| 82 |
+
translated_chunks.append(translated_chunk)
|
| 83 |
+
|
| 84 |
+
return " ".join(translated_chunks) # β
Join all translated chunks
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"β Error in translation: {str(e)}"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def save_english_file(content, topic):
|
| 91 |
+
today = datetime.today().strftime('%Y_%b_%d') # Format: 2025_Feb_21
|
| 92 |
+
filename = f"English_Scripts/{topic}_Eng_{today}.txt"
|
| 93 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True) # Ensure directory exists
|
| 94 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 95 |
+
f.write(content)
|
| 96 |
+
return filename
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def save_urdu_file(content, topic):
|
| 100 |
+
today = datetime.today().strftime('%Y_%b_%d')
|
| 101 |
+
filename = f"Urdu_Scripts/{topic}_Urdu_{today}.txt"
|
| 102 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
| 103 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 104 |
+
f.write(content)
|
| 105 |
+
return filename
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def save_final_urdu_file(topic, content):
|
| 109 |
+
date_str = datetime.now().strftime("%Y_%b_%d")
|
| 110 |
+
filename = f"Urdu_Final/{topic}_Urdu_Final_{date_str}.txt" # β
Corrected file path
|
| 111 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True) # β
Ensure the directory exists
|
| 112 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 113 |
+
f.write(content)
|
| 114 |
+
return filename
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def finalize_process():
|
| 118 |
+
return "β
Script Generation Completed Successfully!"
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def clear_old_files():
|
| 122 |
+
# β
Define all directories where files are stored
|
| 123 |
+
directories = ["English_Scripts", "Urdu_Scripts", "Urdu_Final", "Keywords"]
|
| 124 |
+
|
| 125 |
+
for directory in directories:
|
| 126 |
+
if os.path.exists(directory): # β
Check if directory exists
|
| 127 |
+
files = glob.glob(f"{directory}/*") # β
Get all files in the directory
|
| 128 |
+
for file in files:
|
| 129 |
+
try:
|
| 130 |
+
os.remove(file) # β
Delete each file
|
| 131 |
+
except Exception as e:
|
| 132 |
+
print(f"β Error deleting {file}: {e}")
|
| 133 |
+
|
| 134 |
+
return "", "", "", "", "" # β
Clear all textboxes in UI
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
#######################################################################################
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# Ensure required folders exist
|
| 143 |
+
os.makedirs("generated_images", exist_ok=True)
|
| 144 |
+
os.makedirs("output", exist_ok=True)
|
| 145 |
+
|
| 146 |
+
# Load Stable Diffusion for image generation
|
| 147 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 148 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 149 |
+
pipe.safety_checker = None # Disable safety checker
|
| 150 |
+
|
| 151 |
+
# Global variable to store generated TTS audio path
|
| 152 |
+
global_audio_path = None
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
### π£οΈ TEXT-TO-SPEECH FUNCTION ###
|
| 156 |
+
def text_to_speech(script_file):
|
| 157 |
+
if script_file is None:
|
| 158 |
+
return None, "β οΈ Please upload an Urdu script file!"
|
| 159 |
+
|
| 160 |
+
with open(script_file.name, "r", encoding="utf-8") as f:
|
| 161 |
+
urdu_text = f.read().strip()
|
| 162 |
+
|
| 163 |
+
audio_path = "output/urdu_audio.mp3"
|
| 164 |
+
tts = gTTS(text=urdu_text, lang="ur")
|
| 165 |
+
tts.save(audio_path)
|
| 166 |
+
|
| 167 |
+
global global_audio_path
|
| 168 |
+
global_audio_path = audio_path
|
| 169 |
+
|
| 170 |
+
return audio_path, "β
Audio generated successfully!"
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
### ποΈ IMAGE GENERATION FUNCTION ###
|
| 174 |
+
def generate_images(script_file, num_images):
|
| 175 |
+
if script_file is None:
|
| 176 |
+
return None, "β οΈ Please upload a script file!"
|
| 177 |
+
|
| 178 |
+
num_images = int(num_images)
|
| 179 |
+
|
| 180 |
+
with open(script_file.name, "r", encoding="utf-8") as f:
|
| 181 |
+
text_lines = f.read().split("\n\n") # Splitting scenes by double newlines
|
| 182 |
+
|
| 183 |
+
image_paths = []
|
| 184 |
+
for i, scene in enumerate(text_lines[:num_images]):
|
| 185 |
+
prompt = f"Scene {i+1}: {scene.strip()}"
|
| 186 |
+
image = pipe(prompt).images[0]
|
| 187 |
+
image_path = f"generated_images/image_{i+1}.png"
|
| 188 |
+
image.save(image_path)
|
| 189 |
+
image_paths.append(image_path)
|
| 190 |
+
|
| 191 |
+
return image_paths, "β
Images generated successfully!"
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
### π₯ VIDEO CREATION FUNCTION ###
|
| 195 |
+
def images_to_video(image_paths, fps=1):
|
| 196 |
+
if not image_paths:
|
| 197 |
+
return None
|
| 198 |
+
|
| 199 |
+
frame = cv2.imread(image_paths[0])
|
| 200 |
+
height, width, layers = frame.shape
|
| 201 |
+
|
| 202 |
+
video_path = "output/generated_video.mp4"
|
| 203 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 204 |
+
video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
| 205 |
+
|
| 206 |
+
for image in image_paths:
|
| 207 |
+
frame = cv2.imread(image)
|
| 208 |
+
video.write(frame)
|
| 209 |
+
|
| 210 |
+
video.release()
|
| 211 |
+
return video_path
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
### π AUDIO-VIDEO MERGE FUNCTION ###
|
| 215 |
+
def merge_audio_video(video_path):
|
| 216 |
+
if global_audio_path is None:
|
| 217 |
+
return None, "β οΈ No audio found! Please generate Urdu TTS first."
|
| 218 |
+
|
| 219 |
+
final_video_path = "output/final_video.mp4"
|
| 220 |
+
|
| 221 |
+
video = ffmpeg.input(video_path)
|
| 222 |
+
audio = ffmpeg.input(global_audio_path)
|
| 223 |
+
|
| 224 |
+
ffmpeg.output(video, audio, final_video_path, vcodec="libx264", acodec="aac").run(overwrite_output=True)
|
| 225 |
+
|
| 226 |
+
return final_video_path, "β
Video with Urdu voice-over generated successfully!"
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
### π¬ FINAL VIDEO GENERATION PIPELINE ###
|
| 230 |
+
def generate_final_video(script_file, num_images):
|
| 231 |
+
if script_file is None:
|
| 232 |
+
return None, "β οΈ Please upload a script file for image generation!"
|
| 233 |
+
|
| 234 |
+
image_paths, img_msg = generate_images(script_file, num_images)
|
| 235 |
+
if not image_paths:
|
| 236 |
+
return None, img_msg
|
| 237 |
+
|
| 238 |
+
video_path = images_to_video(image_paths, fps=1)
|
| 239 |
+
final_video_path, vid_msg = merge_audio_video(video_path)
|
| 240 |
+
|
| 241 |
+
return final_video_path, vid_msg
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
### π GRADIO UI ###
|
| 246 |
+
with gr.Blocks() as app:
|
| 247 |
+
|
| 248 |
+
gr.Markdown("## # π¬ AI-Powered Educational Video Generator")
|
| 249 |
+
|
| 250 |
+
# TTS Section
|
| 251 |
+
with gr.Tab("Script Generator"):
|
| 252 |
+
topic_input = gr.Textbox(label="Enter Topic")
|
| 253 |
+
duration_input = gr.Slider(minimum=1, maximum=30, step=1, label="Duration (minutes)")
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
generate_button = gr.Button("Generate English Script")
|
| 257 |
+
eng_output = gr.Textbox(label="Generated English Script", interactive=False)
|
| 258 |
+
download_english_button = gr.Button("Download English Script")
|
| 259 |
+
download_english_button.click(save_english_file, inputs=[eng_output, topic_input], outputs=[gr.File()])
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
# β
Keyword Extraction Section
|
| 263 |
+
extract_keywords_btn = gr.Button("π Extract Keywords")
|
| 264 |
+
keyword_output = gr.Textbox(label="π Extracted Keywords", interactive=True)
|
| 265 |
+
download_keywords_btn = gr.Button("β¬οΈ Download Keywords")
|
| 266 |
+
download_keywords_btn.click(save_keywords_file, inputs=[keyword_output, topic_input], outputs=[gr.File()])
|
| 267 |
+
|
| 268 |
+
translate_button = gr.Button("Generate Urdu Script")
|
| 269 |
+
urdu_output = gr.Textbox(label="Translated Urdu Script", interactive=False, rtl=True)
|
| 270 |
+
download_urdu_button = gr.Button("Download Urdu Script")
|
| 271 |
+
download_urdu_button.click(save_urdu_file, inputs=[urdu_output, topic_input], outputs=[gr.File()])
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
final_edited_urdu_output = gr.Textbox(label="Edited Urdu Script", interactive=True, rtl=True)
|
| 275 |
+
download_final_urdu_button = gr.Button("Download Final Urdu Script")
|
| 276 |
+
download_final_urdu_button.click(save_final_urdu_file, inputs=[topic_input, final_edited_urdu_output], outputs=[gr.File()])
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
# β
Button Actions
|
| 280 |
+
# generate_button.click(generate_script, inputs=[topic_input, duration_input], outputs=[eng_output])
|
| 281 |
+
generate_button.click(generate_script, inputs=[topic_input, duration_input], outputs=[eng_output])
|
| 282 |
+
extract_keywords_btn.click(extract_keywords, inputs=[eng_output], outputs=[keyword_output])
|
| 283 |
+
translate_button.click(translate_to_urdu, inputs=[eng_output], outputs=[urdu_output])
|
| 284 |
+
|
| 285 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 286 |
+
finalize_button = gr.Button("Finalize Process")
|
| 287 |
+
finalize_button.click(finalize_process, outputs=[status_output])
|
| 288 |
+
|
| 289 |
+
generate_button.click(
|
| 290 |
+
lambda topic, duration: (*clear_old_files(), generate_script(topic, duration)),
|
| 291 |
+
inputs=[topic_input, duration_input],
|
| 292 |
+
outputs=[keyword_output, urdu_output, final_edited_urdu_output, status_output] )
|
| 293 |
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
# TTS Section
|
| 297 |
+
with gr.Tab("π£οΈ Urdu Text-to-Speech"):
|
| 298 |
+
script_file_tts = gr.File(label="π Upload Urdu Script for Audio", type="filepath")
|
| 299 |
+
generate_audio_btn = gr.Button("ποΈ Generate Audio", variant="primary")
|
| 300 |
+
audio_output = gr.Audio(label="π Urdu Speech Output", interactive=False)
|
| 301 |
+
audio_status = gr.Textbox(label="βΉοΈ Status", interactive=False)
|
| 302 |
+
|
| 303 |
+
generate_audio_btn.click(text_to_speech, inputs=[script_file_tts], outputs=[audio_output, audio_status])
|
| 304 |
+
|
| 305 |
+
# Video Generation Section
|
| 306 |
+
with gr.Tab("π₯ AI Video Generator"):
|
| 307 |
+
script_file_video = gr.File(label="π Upload Urdu Script for Images", type="filepath")
|
| 308 |
+
num_images = gr.Number(label="πΈ Number of Scenes", value=3, minimum=1, maximum=10, step=1)
|
| 309 |
+
generate_video_btn = gr.Button("π¬ Generate Video", variant="primary")
|
| 310 |
+
video_output = gr.Video(label="ποΈ Generated Video")
|
| 311 |
+
video_status = gr.Textbox(label="βΉοΈ Status", interactive=False)
|
| 312 |
|
| 313 |
+
generate_video_btn.click(generate_final_video, inputs=[script_file_video, num_images], outputs=[video_output, video_status])
|
| 314 |
|
| 315 |
+
app.launch()
|
|
|