banao-tech commited on
Commit
e956e33
Β·
verified Β·
1 Parent(s): b081c75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -48
app.py CHANGED
@@ -1,62 +1,74 @@
1
  import gradio as gr
2
- from gradio_client import Client, handle_file
3
- import shutil
4
  from pathlib import Path
5
 
6
- def generate_video(image, audio):
7
- """Use existing HF Space API"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  try:
9
- if not image or not audio:
10
- return None, "❌ Please upload both files!"
11
 
12
- print("πŸ”„ Connecting to API...")
13
 
14
- # Use the working MuseTalk space
15
- client = Client("TMElyralab/MuseTalk")
16
 
17
- print("πŸ“€ Uploading files...")
18
 
19
- result = client.predict(
20
- audio_path=handle_file(audio),
21
- video_path=None,
22
- bbox_shift=0,
23
- api_name="/predict"
24
- )
25
-
26
- # Result is a file path
27
- if result and Path(result).exists():
28
- # Copy to local output
29
- output = "result.mp4"
30
- shutil.copy(result, output)
31
- return output, "βœ… Video generated successfully!"
32
  else:
33
- return None, "❌ API returned no result"
34
 
35
  except Exception as e:
36
  return None, f"❌ Error: {str(e)}"
37
 
38
- # Gradio UI
39
- with gr.Blocks(theme=gr.themes.Soft()) as app:
40
- gr.Markdown("# 🎬 AI Lip Sync Generator")
41
- gr.Markdown("Upload a face image and audio to create lip-synced video")
42
-
43
- with gr.Row():
44
- with gr.Column():
45
- img = gr.Image(type="filepath", label="πŸ“· Face Image")
46
- aud = gr.Audio(type="filepath", label="🎡 Audio File")
47
- btn = gr.Button("πŸš€ Generate Video", variant="primary")
48
-
49
- with gr.Column():
50
- vid = gr.Video(label="πŸ“Ή Result")
51
- status = gr.Textbox(label="Status", lines=2)
52
-
53
- btn.click(generate_video, [img, aud], [vid, status])
54
-
55
- gr.Markdown("""
56
- ### πŸ’‘ Notes:
57
- - Uses MuseTalk API (no local installation needed)
58
- - Processing time: 30-90 seconds
59
- - Best with clear front-facing images
60
- """)
61
 
62
- app.launch()
 
1
  import gradio as gr
2
+ import subprocess
3
+ import os
4
  from pathlib import Path
5
 
6
+ def setup():
7
+ """One-time setup"""
8
+ if Path("setup_done.txt").exists():
9
+ return
10
+
11
+ print("Installing dependencies...")
12
+ os.system("pip install -q gfpgan realesrgan basicsr")
13
+
14
+ print("Downloading model...")
15
+ os.system("git clone https://github.com/vinthony/video-retalking.git vrt")
16
+
17
+ # Download checkpoints
18
+ os.chdir("vrt")
19
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/30_net_gen.pth -P checkpoints/")
20
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/BFM.zip -P checkpoints/")
21
+ os.system("unzip -q checkpoints/BFM.zip -d checkpoints/")
22
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/DNet.pt -P checkpoints/")
23
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/ENet.pth -P checkpoints/")
24
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/expression.mat -P checkpoints/")
25
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/face3d_pretrain_epoch_20.pth -P checkpoints/")
26
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/GFPGANv1.3.pth -P checkpoints/")
27
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/GPEN-BFR-512.pth -P checkpoints/")
28
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/LNet.pth -P checkpoints/")
29
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/ParseNet-latest.pth -P checkpoints/")
30
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/RetinaFace-R50.pth -P checkpoints/")
31
+ os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/shape_predictor_68_face_landmarks.dat -P checkpoints/")
32
+ os.chdir("..")
33
+
34
+ Path("setup_done.txt").touch()
35
+ print("βœ… Setup complete!")
36
+
37
+ def generate(image, audio):
38
+ if not image or not audio:
39
+ return None, "Upload both files!"
40
+
41
  try:
42
+ setup()
 
43
 
44
+ output = "result.mp4"
45
 
46
+ # Run inference
47
+ cmd = f"cd vrt && python inference.py --face ../{image} --audio ../{audio} --outfile ../{output}"
48
 
49
+ result = os.system(cmd)
50
 
51
+ if Path(output).exists():
52
+ return output, "βœ… Done!"
 
 
 
 
 
 
 
 
 
 
 
53
  else:
54
+ return None, "❌ Failed to generate"
55
 
56
  except Exception as e:
57
  return None, f"❌ Error: {str(e)}"
58
 
59
+ # Simple Gradio UI
60
+ demo = gr.Interface(
61
+ fn=generate,
62
+ inputs=[
63
+ gr.Image(type="filepath", label="Face Image"),
64
+ gr.Audio(type="filepath", label="Audio File")
65
+ ],
66
+ outputs=[
67
+ gr.Video(label="Result"),
68
+ gr.Textbox(label="Status")
69
+ ],
70
+ title="AI Lip Sync Generator",
71
+ description="Upload a face image and audio to generate lip-synced video"
72
+ )
 
 
 
 
 
 
 
 
 
73
 
74
+ demo.launch()