my-video-app / app.py
mgbam's picture
Create app.py
aa5de1c verified
raw
history blame
5.05 kB
import gradio as gr
import os
import google.generativeai as genai
# Import other SDKs for Runway, ElevenLabs, Tavily
# For example: from elevenlabs import generate, play, set_api_key
import subprocess
import json
import time
# --- 1. LOAD API KEYS FROM SECRETS ---
# This is the secure way to do it in Hugging Face Spaces
genai.configure(api_key=os.environ.get('GEMINI_API_KEY'))
# set_api_key(os.environ.get('ELEVENLABS_API_KEY'))
# ... and so on for other APIs
# --- 2. DEFINE THE CORE VIDEO GENERATION FUNCTION ---
# This function will take the client's prompt and do all the work.
def generate_video_from_topic(topic_prompt):
print(f"Starting video generation for topic: {topic_prompt}")
# Placeholder for the final video path
final_video_path = None
try:
# STEP A: RESEARCH (Tavily) - Optional but recommended
# research_results = tavily_client.search(query=f"Key points about {topic_prompt}")
# facts = research_results['results']
# STEP B: SCRIPT & SCENE PROMPTS (Gemini)
# We ask Gemini for a structured JSON output
gemini_model = genai.GenerativeModel('gemini-pro')
prompt = f"""
Create a short video script about '{topic_prompt}'.
The video should be about 30 seconds long.
Return a JSON object with two keys: 'narration_script' (a string) and 'scene_prompts' (a list of 4 detailed, cinematic visual prompts for an AI video generator).
Example:
{{
"narration_script": "This is the complete narration for the video.",
"scene_prompts": ["prompt 1", "prompt 2", "prompt 3", "prompt 4"]
}}
"""
response = gemini_model.generate_content(prompt)
script_data = json.loads(response.text)
narration = script_data['narration_script']
scene_prompts = script_data['scene_prompts']
print(f"Generated Narration: {narration}")
print(f"Generated Scene Prompts: {scene_prompts}")
# STEP C: VOICE OVER (ElevenLabs)
# audio_bytes = generate(text=narration, voice="Adam", model="eleven_multilingual_v2")
# with open("audio.mp3", "wb") as f:
# f.write(audio_bytes)
# print("Audio file generated.")
# --- MOCKUP for now ---
# Since API calls cost money, let's use a placeholder for testing
print("MOCK: Skipping real API calls for audio. Using a placeholder.")
# Create a silent audio file of the right length for testing
narration_duration = len(narration.split()) / 2.5 # Estimate duration
subprocess.run(['ffmpeg', '-f', 'lavfi', '-i', 'anullsrc=r=44100:cl=mono', '-t', str(narration_duration), '-q:a', '9', '-acodec', 'libmp3lame', 'audio.mp3', '-y'])
# STEP D: VISUALS (Runway/Hailuo)
video_clips = []
for i, scene_prompt in enumerate(scene_prompts):
print(f"Generating video for scene {i+1}: {scene_prompt}")
# MOCKUP: Create a simple placeholder video clip
clip_path = f"scene_{i+1}.mp4"
subprocess.run([
'ffmpeg', '-f', 'lavfi', '-i', f'smptebars=size=1920x1080:rate=30',
'-t', '4', '-pix_fmt', 'yuv420p', clip_path, '-y'
])
video_clips.append(clip_path)
# STEP E: STITCHING (FFmpeg)
# Create a file list for ffmpeg
with open("file_list.txt", "w") as f:
for clip in video_clips:
f.write(f"file '{clip}'\n")
# Concatenate video clips
subprocess.run(['ffmpeg', '-f', 'concat', '-safe', '0', '-i', 'file_list.txt', '-c', 'copy', 'combined_video.mp4', '-y'])
# Add audio to the combined video
final_video_path = f"final_video_{int(time.time())}.mp4"
subprocess.run([
'ffmpeg', '-i', 'combined_video.mp4', '-i', 'audio.mp3', '-c:v', 'copy',
'-c:a', 'aac', '-shortest', final_video_path, '-y'
])
print(f"Final video created at: {final_video_path}")
except Exception as e:
print(f"An error occurred: {e}")
# You can return an error message to the Gradio interface
raise gr.Error(f"Failed to generate video. Error: {e}")
# Return the path to the final video so Gradio can display it
return final_video_path
# --- 3. CREATE THE GRADIO INTERFACE ---
with gr.Blocks() as demo:
gr.Markdown("# πŸ€– My Personal AI Video Studio")
gr.Markdown("Enter a topic to generate a short-form video for social media. Used for fulfilling Fiverr orders.")
with gr.Row():
topic_input = gr.Textbox(label="Video Topic", placeholder="e.g., 'The benefits of a standing desk'")
generate_button = gr.Button("Generate Video", variant="primary")
with gr.Row():
video_output = gr.Video(label="Generated Video")
generate_button.click(
fn=generate_video_from_topic,
inputs=topic_input,
outputs=video_output
)
# --- 4. LAUNCH THE APP ---
demo.launch()