Spaces:
Build error
Build error
| import gradio as gr | |
| import requests | |
| import json | |
| from gtts import gTTS | |
| import ffmpeg | |
| import runway | |
| # ๐น API Keys | |
| GEMINI_API_KEY = "AIzaSyAfjSQ7WnSkMNOJ9RRIU_-jYy4ik4yhWUA" | |
| RUNWAY_API_KEY = "e17d25a4363b223feb9e5a177b77384b7445fddfc5b467179f8dbd00d76e573e99e291cc665aff0243dae1b92746b23cc05b5e85669b1dcbe358272e4676a10d" | |
| # ๐น Function to Generate a Video Script | |
| def generate_script(topic): | |
| url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GEMINI_API_KEY}" | |
| headers = {"Content-Type": "application/json"} | |
| data = {"contents": [{"parts": [{"text": f"Create a YouTube/TikTok video script about: {topic}"}]}]} | |
| response = requests.post(url, headers=headers, data=json.dumps(data)) | |
| if response.status_code == 200: | |
| return response.json()["candidates"][0]["content"]["parts"][0]["text"] | |
| return "Error generating script." | |
| # ๐น Function to Convert Script to Speech | |
| def convert_text_to_speech(script_text): | |
| tts = gTTS(text=script_text, lang="en") | |
| tts.save("audio.mp3") | |
| return "audio.mp3" | |
| # ๐น Function to Generate Video Using Runway ML | |
| def generate_video_with_runway(script_text): | |
| try: | |
| runway.init(api_key=RUNWAY_API_KEY) | |
| model = runway.connect('text-to-video') | |
| video_url = model.query('generate_video', inputs={"text": script_text}) | |
| return video_url | |
| except Exception as e: | |
| print(f"โ Error generating video: {e}") | |
| return None | |
| # ๐น Add Text Overlay to Video | |
| def add_text_overlay(video_file, text, output_file="video_with_text.mp4"): | |
| try: | |
| ffmpeg.input(video_file).output(output_file, vf=f"drawtext=text='{text}':fontcolor=white:fontsize=24:x=10:y=10").run() | |
| return output_file | |
| except Exception as e: | |
| return f"Error adding text overlay: {e}" | |
| # ๐น Add Background Music to Video | |
| def add_background_music(video_file, music_file, output_file="video_with_music.mp4"): | |
| try: | |
| ffmpeg.input(video_file).input(music_file).output(output_file, vcodec="libx264", acodec="aac", strict="experimental").run() | |
| return output_file | |
| except Exception as e: | |
| return f"Error adding background music: {e}" | |
| # ๐น Function to Create Video with Progress Bar | |
| def create_video_with_progress(topic, custom_audio=None, music_file=None): | |
| progress = gr.Progress() | |
| try: | |
| script = generate_script(topic) | |
| if script: | |
| progress(10, "Script generated") | |
| audio_file = convert_text_to_speech(script) if not custom_audio else custom_audio | |
| progress(30, "Audio converted") | |
| video_file = generate_video_with_runway(script) | |
| progress(50, "Video generated") | |
| video_with_overlay = add_text_overlay(video_file, "Your Video Title Here") | |
| progress(70, "Text overlay added") | |
| video_with_music = add_background_music(video_with_overlay, music_file if music_file else "default_music.mp3") | |
| progress(90, "Background music added") | |
| progress(100, "Video created") | |
| return video_with_music | |
| except Exception as e: | |
| print(f"โ Error during video creation: {e}") | |
| return "Error creating video." | |
| # Gradio UI setup | |
| app = gr.Interface( | |
| fn=create_video_with_progress, | |
| inputs=["text", gr.inputs.File(label="Upload Audio (Optional)"), gr.inputs.File(label="Upload Music (Optional)")], | |
| outputs="file", | |
| title="AI Video Creator with Runway ML" | |
| ) | |
| app.launch() |