johnkezia9008 commited on
Commit
f23eaa2
·
verified ·
1 Parent(s): 9380d8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -20
app.py CHANGED
@@ -1,35 +1,66 @@
1
  import gradio as gr
 
2
  import subprocess
3
- import os
4
  import uuid
5
- from PIL import Image, ImageDraw
 
 
 
 
6
 
7
- def generate_video(text, audio_file):
8
  uid = str(uuid.uuid4())
9
- image_file = f"{uid}.png"
10
- output_video = f"{uid}.mp4"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Create image with text
13
- img = Image.new('RGB', (720, 1280), color=(255, 255, 255))
14
- draw = ImageDraw.Draw(img)
15
- draw.text((50, 600), text, fill=(0, 0, 0))
16
- img.save(image_file)
 
 
 
17
 
18
- # Merge audio and image using FFmpeg
19
- command = f"ffmpeg -y -loop 1 -i {image_file} -i {audio_file} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest {output_video}"
20
- subprocess.call(command, shell=True)
 
 
 
 
21
 
22
- return output_video
23
 
24
  demo = gr.Interface(
25
- fn=generate_video,
26
  inputs=[
27
- gr.Textbox(label="Enter Text"),
28
- gr.Audio(type="filepath", label="Upload Audio File")
29
  ],
30
- outputs=gr.Video(label="Generated Video"),
31
- title="Text + Audio to Video Generator",
32
- description="Merges input text and audio into a short MP4 video."
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()