Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +49 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
from moviepy.editor import *
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
ELEVENLABS_API_KEY = "YOUR_API_KEY_HERE"
|
| 9 |
+
VOICE_ID = "EXAVITQu4vr4xnSDxMaL" # Rachel
|
| 10 |
+
|
| 11 |
+
def generate_voiceover(script):
|
| 12 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
|
| 13 |
+
headers = {
|
| 14 |
+
"xi-api-key": ELEVENLABS_API_KEY,
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
payload = {
|
| 18 |
+
"text": script,
|
| 19 |
+
"model_id": "eleven_monolingual_v1",
|
| 20 |
+
"voice_settings": {"stability": 0.4, "similarity_boost": 0.9}
|
| 21 |
+
}
|
| 22 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 23 |
+
if response.status_code == 200:
|
| 24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
|
| 25 |
+
f.write(response.content)
|
| 26 |
+
return f.name
|
| 27 |
+
else:
|
| 28 |
+
raise Exception("Failed to generate voiceover.")
|
| 29 |
+
|
| 30 |
+
def create_video_with_audio(code_text, voiceover_path):
|
| 31 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as out:
|
| 32 |
+
image_path = "/usr/share/icons/gnome/256x256/apps/utilities-terminal.png"
|
| 33 |
+
clip = ImageClip(image_path, duration=10).resize(width=720)
|
| 34 |
+
audio = AudioFileClip(voiceover_path).subclip(0, clip.duration)
|
| 35 |
+
final = clip.set_audio(audio)
|
| 36 |
+
final.write_videofile(out.name, fps=24)
|
| 37 |
+
return out.name
|
| 38 |
+
|
| 39 |
+
def main(code_input):
|
| 40 |
+
script = f"This is a short explanation of the following code: {code_input[:300]}..."
|
| 41 |
+
voiceover_file = generate_voiceover(script)
|
| 42 |
+
video_path = create_video_with_audio(code_input, voiceover_file)
|
| 43 |
+
return video_path
|
| 44 |
+
|
| 45 |
+
demo = gr.Interface(fn=main, inputs=gr.Textbox(lines=15, label="Paste Your Code"),
|
| 46 |
+
outputs=gr.Video(label="Generated Video with Voiceover"),
|
| 47 |
+
title="AI Code to Video Bot")
|
| 48 |
+
|
| 49 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
moviepy
|
| 3 |
+
requests
|