Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,66 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import subprocess
|
| 3 |
-
import os
|
| 4 |
import uuid
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
uid = str(uuid.uuid4())
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# Merge audio
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
return
|
| 23 |
|
| 24 |
demo = gr.Interface(
|
| 25 |
-
fn=
|
| 26 |
inputs=[
|
| 27 |
-
gr.Textbox(label="Enter
|
| 28 |
-
gr.Audio(type="filepath", label="Upload Audio
|
| 29 |
],
|
| 30 |
-
outputs=gr.Video(label="Generated Video"),
|
| 31 |
-
title="Text + Audio
|
| 32 |
-
description="
|
| 33 |
)
|
| 34 |
|
| 35 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import replicate
|
| 3 |
import subprocess
|
|
|
|
| 4 |
import uuid
|
| 5 |
+
import os
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
# Replicate API token - you can securely set this in the "Secrets" tab on HF
|
| 9 |
+
os.environ["REPLICATE_API_TOKEN"] = "your_replicate_api_key" # <-- REPLACE THIS
|
| 10 |
|
| 11 |
+
def generate_ai_video(text, audio_file):
|
| 12 |
uid = str(uuid.uuid4())
|
| 13 |
+
video_url = None
|
| 14 |
+
|
| 15 |
+
# Step 1: Call Replicate AI video model (zeroscope)
|
| 16 |
+
print("Calling Replicate model...")
|
| 17 |
+
try:
|
| 18 |
+
output = replicate.run(
|
| 19 |
+
"cjwbw/zeroscope-v2-576w",
|
| 20 |
+
input={
|
| 21 |
+
"prompt": text,
|
| 22 |
+
"num_frames": 24,
|
| 23 |
+
"fps": 8,
|
| 24 |
+
"width": 576,
|
| 25 |
+
"height": 320,
|
| 26 |
+
"guidance_scale": 12.5
|
| 27 |
+
}
|
| 28 |
+
)
|
| 29 |
+
video_url = output[-1] # Last frame (mp4)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error from Replicate: {str(e)}"
|
| 32 |
+
|
| 33 |
+
raw_video = f"{uid}_raw.mp4"
|
| 34 |
+
final_video = f"{uid}_final.mp4"
|
| 35 |
|
| 36 |
+
# Step 2: Download video
|
| 37 |
+
try:
|
| 38 |
+
print("Downloading video from Replicate...")
|
| 39 |
+
r = requests.get(video_url)
|
| 40 |
+
with open(raw_video, 'wb') as f:
|
| 41 |
+
f.write(r.content)
|
| 42 |
+
except:
|
| 43 |
+
return "Failed to download generated video."
|
| 44 |
|
| 45 |
+
# Step 3: Merge uploaded audio with AI video using FFmpeg
|
| 46 |
+
try:
|
| 47 |
+
print("Merging audio with video...")
|
| 48 |
+
cmd = f"ffmpeg -y -i {raw_video} -i {audio_file} -map 0:v -map 1:a -shortest -c:v copy -c:a aac {final_video}"
|
| 49 |
+
subprocess.call(cmd, shell=True)
|
| 50 |
+
except:
|
| 51 |
+
return "Error during audio-video merge."
|
| 52 |
|
| 53 |
+
return final_video
|
| 54 |
|
| 55 |
demo = gr.Interface(
|
| 56 |
+
fn=generate_ai_video,
|
| 57 |
inputs=[
|
| 58 |
+
gr.Textbox(label="Enter Video Prompt / Script"),
|
| 59 |
+
gr.Audio(type="filepath", label="Upload Audio (.mp3 or .wav)")
|
| 60 |
],
|
| 61 |
+
outputs=gr.Video(label="AI Generated Video"),
|
| 62 |
+
title="Text + Audio → AI Video Generator",
|
| 63 |
+
description="Creates an AI-generated video using your script and audio voiceover using a free model."
|
| 64 |
)
|
| 65 |
|
| 66 |
demo.launch()
|