File size: 11,745 Bytes
b918062
00e7c14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b918062
bc75df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664915e
bc75df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b918062
bc75df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b918062
bc75df6
b918062
bc75df6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import gradio as gr
import os
import torch
import yake
import shutil
import glob
import ffmpeg
import cv2
import numpy as np
from datetime import datetime
from gtts import gTTS
from diffusers import StableDiffusionPipeline
from deep_translator import GoogleTranslator
import wikipediaapi
from groq import Groq



# βœ… Set API Key
os.environ["GROQ_API_KEY"] = "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"    # Replace with your API key
client = Groq(api_key=os.environ["GROQ_API_KEY"])


def fetch_wikipedia_summary(topic):
    wiki_wiki = wikipediaapi.Wikipedia(
        user_agent="EducationalScriptApp/1.0",
        language="en"
    )
    page = wiki_wiki.page(topic)
    return page.summary if page.exists() else "No Wikipedia summary available."

def generate_script(topic, duration):
    try:
        factual_content = fetch_wikipedia_summary(topic)
        words_per_minute = 130
        target_words = duration * words_per_minute

        response = client.chat.completions.create(
            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}"}],
            model="llama-3.3-70b-versatile"
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"❌ Error in script generation: {str(e)}"


# βœ… Function to Extract Keywords Using YAKE
def extract_keywords(script):
    try:
        kw_extractor = yake.KeywordExtractor(
            lan="en",  # Language
            n=3,  # Max number of words in a keyword phrase (trigrams)
            dedupLim=0.9,  # Reduce redundant phrases
            # top=10  # Extract top 10 keywords
        )

        keywords = kw_extractor.extract_keywords(script)
        return ", ".join([kw[0] for kw in keywords])  # βœ… Extract only the keyword text
    except Exception as e:
        return f"❌ Error extracting keywords: {str(e)}"


def save_keywords_file(keywords, topic):
    today = datetime.today().strftime('%Y_%b_%d')
    filename = f"Keywords/{topic}_Keyword_{today}.txt"
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    with open(filename, "w", encoding="utf-8") as f:
        f.write(keywords)
    return filename



def translate_to_urdu(english_script):
    try:
        # βœ… Define a max chunk size (Google Translator has a limit)
        max_chunk_size = 4500  # Stay below 5000 to be safe
        chunks = [english_script[i:i + max_chunk_size] for i in range(0, len(english_script), max_chunk_size)]

        translated_chunks = []
        for chunk in chunks:
            translated_chunk = GoogleTranslator(source='en', target='ur').translate(chunk)
            translated_chunks.append(translated_chunk)

        return " ".join(translated_chunks)  # βœ… Join all translated chunks
    except Exception as e:
        return f"❌ Error in translation: {str(e)}"



def save_english_file(content, topic):
    today = datetime.today().strftime('%Y_%b_%d')  # Format: 2025_Feb_21
    filename = f"English_Scripts/{topic}_Eng_{today}.txt"
    os.makedirs(os.path.dirname(filename), exist_ok=True)  # Ensure directory exists
    with open(filename, "w", encoding="utf-8") as f:
        f.write(content)
    return filename


def save_urdu_file(content, topic):
    today = datetime.today().strftime('%Y_%b_%d')
    filename = f"Urdu_Scripts/{topic}_Urdu_{today}.txt"
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    with open(filename, "w", encoding="utf-8") as f:
        f.write(content)
    return filename


def save_final_urdu_file(topic, content):
    date_str = datetime.now().strftime("%Y_%b_%d")
    filename = f"Urdu_Final/{topic}_Urdu_Final_{date_str}.txt"  # βœ… Corrected file path
    os.makedirs(os.path.dirname(filename), exist_ok=True)  # βœ… Ensure the directory exists
    with open(filename, "w", encoding="utf-8") as f:
        f.write(content)
    return filename


def finalize_process():
    return "βœ… Script Generation Completed Successfully!"


def clear_old_files():
    # βœ… Define all directories where files are stored
    directories = ["English_Scripts", "Urdu_Scripts", "Urdu_Final", "Keywords"]

    for directory in directories:
        if os.path.exists(directory):  # βœ… Check if directory exists
            files = glob.glob(f"{directory}/*")  # βœ… Get all files in the directory
            for file in files:
                try:
                    os.remove(file)  # βœ… Delete each file
                except Exception as e:
                    print(f"❌ Error deleting {file}: {e}")

    return "", "", "", "", ""  # βœ… Clear all textboxes in UI



#######################################################################################

    

# Ensure required folders exist
os.makedirs("generated_images", exist_ok=True)
os.makedirs("output", exist_ok=True)

# Load Stable Diffusion for image generation
model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
pipe.safety_checker = None  # Disable safety checker

# Global variable to store generated TTS audio path
global_audio_path = None


### πŸ—£οΈ TEXT-TO-SPEECH FUNCTION ###
def text_to_speech(script_file):
    if script_file is None:
        return None, "⚠️ Please upload an Urdu script file!"
    
    with open(script_file.name, "r", encoding="utf-8") as f:
        urdu_text = f.read().strip()

    audio_path = "output/urdu_audio.mp3"
    tts = gTTS(text=urdu_text, lang="ur")
    tts.save(audio_path)

    global global_audio_path
    global_audio_path = audio_path

    return audio_path, "βœ… Audio generated successfully!"


### 🏞️ IMAGE GENERATION FUNCTION ###
def generate_images(script_file, num_images):
    if script_file is None:
        return None, "⚠️ Please upload a script file!"
    
    num_images = int(num_images)

    with open(script_file.name, "r", encoding="utf-8") as f:
        text_lines = f.read().split("\n\n")  # Splitting scenes by double newlines
    
    image_paths = []
    for i, scene in enumerate(text_lines[:num_images]):
        prompt = f"Scene {i+1}: {scene.strip()}"
        image = pipe(prompt).images[0]
        image_path = f"generated_images/image_{i+1}.png"
        image.save(image_path)
        image_paths.append(image_path)
    
    return image_paths, "βœ… Images generated successfully!"


### πŸŽ₯ VIDEO CREATION FUNCTION ###
def images_to_video(image_paths, fps=1):
    if not image_paths:
        return None
    
    frame = cv2.imread(image_paths[0])
    height, width, layers = frame.shape

    video_path = "output/generated_video.mp4"
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))

    for image in image_paths:
        frame = cv2.imread(image)
        video.write(frame)
    
    video.release()
    return video_path


### πŸ”Š AUDIO-VIDEO MERGE FUNCTION ###
def merge_audio_video(video_path):
    if global_audio_path is None:
        return None, "⚠️ No audio found! Please generate Urdu TTS first."
    
    final_video_path = "output/final_video.mp4"
    
    video = ffmpeg.input(video_path)
    audio = ffmpeg.input(global_audio_path)
    
    ffmpeg.output(video, audio, final_video_path, vcodec="libx264", acodec="aac").run(overwrite_output=True)
    
    return final_video_path, "βœ… Video with Urdu voice-over generated successfully!"


### 🎬 FINAL VIDEO GENERATION PIPELINE ###
def generate_final_video(script_file, num_images):
    if script_file is None:
        return None, "⚠️ Please upload a script file for image generation!"
    
    image_paths, img_msg = generate_images(script_file, num_images)
    if not image_paths:
        return None, img_msg

    video_path = images_to_video(image_paths, fps=1)
    final_video_path, vid_msg = merge_audio_video(video_path)
    
    return final_video_path, vid_msg



### πŸš€ GRADIO UI ###
with gr.Blocks()as app:

    gr.Markdown("## # 🎬 AI-Powered Educational Video Generator")

    # TTS Section
    with gr.Tab("Script Generator"):
        topic_input = gr.Textbox(label="Enter Topic")
        duration_input = gr.Slider(minimum=1, maximum=30, step=1, label="Duration (minutes)")


        generate_button = gr.Button("Generate English Script")
        eng_output = gr.Textbox(label="Generated English Script", interactive=False)
        download_english_button = gr.Button("Download English Script")
        download_english_button.click(save_english_file, inputs=[eng_output, topic_input], outputs=[gr.File()])


        # βœ… Keyword Extraction Section
        extract_keywords_btn = gr.Button("πŸ”‘ Extract Keywords")
        keyword_output = gr.Textbox(label="πŸ” Extracted Keywords", interactive=True)
        download_keywords_btn = gr.Button("⬇️ Download Keywords")
        download_keywords_btn.click(save_keywords_file, inputs=[keyword_output, topic_input], outputs=[gr.File()])

        translate_button = gr.Button("Generate Urdu Script")
        urdu_output = gr.Textbox(label="Translated Urdu Script", interactive=False, rtl=True)
        download_urdu_button = gr.Button("Download Urdu Script")
        download_urdu_button.click(save_urdu_file, inputs=[urdu_output, topic_input], outputs=[gr.File()])

        
        final_edited_urdu_output = gr.Textbox(label="Edited Urdu Script", interactive=True, rtl=True)
        download_final_urdu_button = gr.Button("Download Final Urdu Script")
        download_final_urdu_button.click(save_final_urdu_file, inputs=[topic_input, final_edited_urdu_output], outputs=[gr.File()])


        # βœ… Button Actions
        # generate_button.click(generate_script, inputs=[topic_input, duration_input], outputs=[eng_output])
        generate_button.click(generate_script, inputs=[topic_input, duration_input],  outputs=[eng_output])
        extract_keywords_btn.click(extract_keywords, inputs=[eng_output], outputs=[keyword_output])
        translate_button.click(translate_to_urdu, inputs=[eng_output], outputs=[urdu_output])

        status_output = gr.Textbox(label="Status", interactive=False)
        finalize_button = gr.Button("Finalize Process")
        finalize_button.click(finalize_process, outputs=[status_output])

        generate_button.click(
        lambda topic, duration: (*clear_old_files(), generate_script(topic, duration)),  
        inputs=[topic_input, duration_input],  
        outputs=[keyword_output, urdu_output, final_edited_urdu_output, status_output]    )
        

    
    # TTS Section
    with gr.Tab("πŸ—£οΈ Urdu Text-to-Speech"):
        script_file_tts = gr.File(label="πŸ“‚ Upload Urdu Script for Audio", type="filepath")
        generate_audio_btn = gr.Button("πŸŽ™οΈ Generate Audio", variant="primary")
        audio_output = gr.Audio(label="πŸ”Š Urdu Speech Output", interactive=False)
        audio_status = gr.Textbox(label="ℹ️ Status", interactive=False)
    
    generate_audio_btn.click(text_to_speech, inputs=[script_file_tts], outputs=[audio_output, audio_status])
    
    # Video Generation Section
    with gr.Tab("πŸŽ₯ AI Video Generator"):
        script_file_video = gr.File(label="πŸ“‚ Upload Urdu Script for Images", type="filepath")
        num_images = gr.Number(label="πŸ“Έ Number of Scenes", value=3, minimum=1, maximum=10, step=1)
        generate_video_btn = gr.Button("🎬 Generate Video", variant="primary")
        video_output = gr.Video(label="🎞️ Generated Video")
        video_status = gr.Textbox(label="ℹ️ Status", interactive=False)
    
    generate_video_btn.click(generate_final_video, inputs=[script_file_video, num_images], outputs=[video_output, video_status])

app.launch()