Spaces:
Running
Running
| import gradio as gr | |
| from transformers import pipeline | |
| import subprocess | |
| import os | |
| import pyttsx3 | |
| import shutil | |
| # === TEXT SCRIPT GENERATOR === | |
| generator = pipeline("text-generation", model="gpt2") | |
| def generate_script(prompt): | |
| result = generator(f"Scene: {prompt}\nDescription:", max_length=80, num_return_sequences=1) | |
| return result[0]["generated_text"] | |
| # === VOICE GENERATOR using pyttsx3 (Fast + CPU Friendly) | |
| def generate_voice(script): | |
| engine = pyttsx3.init() | |
| engine.setProperty('rate', 150) | |
| engine.save_to_file(script, 'voice.wav') | |
| engine.runAndWait() | |
| return "voice.wav" | |
| # === BACKGROUND MUSIC MIXER === | |
| def merge_voice_and_music(voice_path, music_path, output_path="final_audio.wav"): | |
| command = [ | |
| "ffmpeg", "-y", | |
| "-i", voice_path, | |
| "-i", music_path, | |
| "-filter_complex", "[1:0]volume=0.3[a1];[0:0][a1]amix=inputs=2:duration=shortest", | |
| "-c:a", "aac", | |
| "-shortest", | |
| output_path | |
| ] | |
| subprocess.call(command) | |
| return output_path | |
| # === DUMMY VIDEO GENERATOR (use uploaded sample) | |
| def generate_video(prompt): | |
| shutil.copyfile("sample.mp4", "modelscope_output.mp4") | |
| return "modelscope_output.mp4" | |
| # === MERGE AUDIO + VIDEO === | |
| def merge_audio_video(audio_path, video_path): | |
| output = "final_output.mp4" | |
| subprocess.call([ | |
| "ffmpeg", "-y", "-i", video_path, "-i", audio_path, | |
| "-c:v", "copy", "-c:a", "aac", "-shortest", output | |
| ]) | |
| return output | |
| # === FULL PIPELINE === | |
| def full_pipeline(prompt): | |
| script = generate_script(prompt) | |
| voice = generate_voice(script) | |
| final_audio = merge_voice_and_music(voice, "bg_music.mp3") | |
| video = generate_video(prompt) | |
| final_video = merge_audio_video(final_audio, video) | |
| return final_video, script | |
| # === UI === | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π¬ Free AI Reels Generator (CPU Only Version)") | |
| gr.Markdown("Script + Voice + Music + Video on 100% free CPU tier") | |
| with gr.Row(): | |
| input_prompt = gr.Textbox(label="π€ Scene Prompt", placeholder="e.g. A monk in the Himalayas walking through mist") | |
| generate_btn = gr.Button("Generate Reel") | |
| output_video = gr.Video(label="ποΈ Final Output") | |
| output_script = gr.Textbox(label="π Script") | |
| generate_btn.click(fn=full_pipeline, inputs=input_prompt, outputs=[output_video, output_script]) | |
| demo.launch() | |