Download app.py from 291-180/README: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/spaces/291-180/README/resolve/main/app.py
- Command line
-
hf download hf://spaces/291-180/README/app.py
-
curl -L -o app.py https://huggingface.co/spaces/291-180/README/resolve/main/app.py
3.56 kB
| import gradio as gr | |
| import requests | |
| import random | |
| import os | |
| # -------------------------- | |
| # CONFIG | |
| HF_TOKEN = "YOUR_HF_API_KEY" # Hugging Face token | |
| TMP_DIR = "tmp" | |
| os.makedirs(TMP_DIR, exist_ok=True) | |
| # -------------------------- | |
| # HELPER FUNCTIONS | |
| # 1️⃣ Generate viral YouTube script | |
| def generate_script(topic): | |
| url = "https://api-inference.huggingface.co/models/gpt2-large" | |
| headers = {"Authorization": f"Bearer {HF_TOKEN}"} | |
| payload = {"inputs": f"Write an 8-10 minute viral YouTube script about: {topic}"} | |
| r = requests.post(url, headers=headers, json=payload) | |
| if r.status_code != 200: return "Error generating script" | |
| return r.json()[0]['generated_text'] | |
| # 2️⃣ Generate AI voice narration | |
| def generate_voice(script, index=0): | |
| # Using TTS model from Hugging Face (free) | |
| url = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits" | |
| headers = {"Authorization": f"Bearer {HF_TOKEN}"} | |
| payload = {"inputs": script} | |
| r = requests.post(url, headers=headers) | |
| path = os.path.join(TMP_DIR, f"voice_{index}.mp3") | |
| with open(path, "wb") as f: | |
| f.write(r.content) | |
| return path | |
| # 3️⃣ Generate a video clip (placeholder using sample clips for demo) | |
| def generate_clip(scene_prompt, index=0): | |
| # For full version, replace with text-to-video model | |
| sample_videos = [ | |
| "https://samplelib.com/lib/preview/mp4/sample-5s.mp4", | |
| "https://samplelib.com/lib/preview/mp4/sample-10s.mp4" | |
| ] | |
| return random.choice(sample_videos) | |
| # 4️⃣ Assemble long-form video | |
| def assemble_video(script, voice_file, clips): | |
| return clips[0] | |
| # Add audio narration | |
| audio = AudioFileClip(voice_file) | |
| final_clip = final_clip.set_audio(audio) | |
| output_path = os.path.join(TMP_DIR, "final_video.mp4") | |
| final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac") | |
| return output_path | |
| # 5️⃣ Generate captions | |
| def generate_captions(script): | |
| # Split into lines for simplicity | |
| lines = script.split(". ") | |
| return "\n".join([line.strip() for line in lines if line]) | |
| # 6️⃣ Generate thumbnail prompt | |
| def generate_thumbnail_prompt(topic): | |
| return f"A highly engaging, colorful thumbnail for a YouTube video titled: '{topic}'" | |
| # -------------------------- | |
| # MAIN PIPELINE | |
| def full_pipeline(topic): | |
| script = generate_script(topic) | |
| voice = generate_voice(script) | |
| # Generate 3 demo clips | |
| clips = [generate_clip(topic) for _ in range(3)] | |
| video = assemble_video(script, voice, clips) | |
| captions = generate_captions(script) | |
| thumbnail_prompt = generate_thumbnail_prompt(topic) | |
| return script, video, voice, captions, thumbnail_prompt | |
| # -------------------------- | |
| # GRADIO UI | |
| with gr.Blocks() as app: | |
| gr.Markdown("# 🎬 Viral YouTube Automation Tool (FREE)") | |
| gr.Markdown("Enter a topic → Generate script, AI voice, video, captions & thumbnail prompts!") | |
| topic_input = gr.Textbox(label="Video Topic", placeholder="Top 10 AI Tools for 2026") | |
| generate_btn = gr.Button("Generate Full Video") | |
| script_output = gr.Textbox(label="Generated Script") | |
| video_output = gr.Video(label="Full Video") | |
| voice_output = gr.Audio(label="AI Voice Narration") | |
| captions_output = gr.Textbox(label="Auto Captions") | |
| thumbnail_output = gr.Textbox(label="Thumbnail Prompt") | |
| generate_btn.click( | |
| full_pipeline, | |
| inputs=topic_input, | |
| outputs=[script_output, video_output, voice_output, captions_output, thumbnail_output] | |
| ) | |
| app.launch() |