Spaces:
Runtime error
Runtime error
Download app.py from Talibaan/Codeassitant: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/Talibaan/Codeassitant/resolve/main/app.py
- Command line
-
hf download hf://spaces/Talibaan/Codeassitant/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Talibaan/Codeassitant/resolve/main/app.py
1.86 kB
| import gradio as gr | |
| import requests | |
| import base64 | |
| from moviepy.editor import * | |
| from datetime import datetime | |
| import tempfile | |
| ELEVENLABS_API_KEY = "YOUR_API_KEY_HERE" | |
| VOICE_ID = "EXAVITQu4vr4xnSDxMaL" # Rachel | |
| def generate_voiceover(script): | |
| url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}" | |
| headers = { | |
| "xi-api-key": sk_2b6737d21819816d70cf165384cfd5b2d4a44e49c8f8ffb8Y, | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "text": script, | |
| "model_id": "eleven_monolingual_v1", | |
| "voice_settings": {"stability": 0.4, "similarity_boost": 0.9} | |
| } | |
| response = requests.post(url, headers=headers, json=payload) | |
| if response.status_code == 200: | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f: | |
| f.write(response.content) | |
| return f.name | |
| else: | |
| raise Exception("Failed to generate voiceover.") | |
| def create_video_with_audio(code_text, voiceover_path): | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as out: | |
| image_path = "/usr/share/icons/gnome/256x256/apps/utilities-terminal.png" | |
| clip = ImageClip(image_path, duration=10).resize(width=720) | |
| audio = AudioFileClip(voiceover_path).subclip(0, clip.duration) | |
| final = clip.set_audio(audio) | |
| final.write_videofile(out.name, fps=24) | |
| return out.name | |
| def main(code_input): | |
| script = f"This is a short explanation of the following code: {code_input[:300]}..." | |
| voiceover_file = generate_voiceover(script) | |
| video_path = create_video_with_audio(code_input, voiceover_file) | |
| return video_path | |
| demo = gr.Interface(fn=main, inputs=gr.Textbox(lines=15, label="Paste Your Code"), | |
| outputs=gr.Video(label="Generated Video with Voiceover"), | |
| title="AI Code to Video Bot") | |
| demo.launch() | |