banao-tech commited on
Commit
dd056d9
Β·
verified Β·
1 Parent(s): 88845c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -35
app.py CHANGED
@@ -1,61 +1,101 @@
1
- import gradio as gr
2
- import subprocess
3
  import os
 
 
4
  from pathlib import Path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def setup():
7
- if Path("setup_done.txt").exists():
 
 
 
 
8
  return
9
-
10
  print("Setting up Video-Retalking...")
11
-
12
- # Clone repo
13
- os.system("git clone https://github.com/OpenTalker/video-retalking.git vrt")
14
-
15
- # Download models
16
- os.chdir("vrt")
17
- os.system("bash scripts/download_models.sh")
18
- os.chdir("..")
19
-
20
- Path("setup_done.txt").touch()
21
  print("βœ… Setup complete!")
22
 
23
- def generate(image, audio):
24
- if not image or not audio:
25
  return None, "❌ Upload both image and audio!"
26
-
27
  try:
28
  setup()
29
-
30
- output = "result.mp4"
31
-
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Run inference
33
- cmd = f"cd vrt && python inference.py --face ../{image} --audio ../{audio} --outfile ../{output}"
34
-
35
- result = os.system(cmd)
36
-
37
- if Path(output).exists():
38
- return output, "βœ… Video generated successfully!"
39
- else:
40
- return None, "❌ Failed to generate video"
41
-
 
 
 
 
42
  except Exception as e:
43
- return None, f"❌ Error: {str(e)}"
44
 
45
- # Gradio Interface
46
  demo = gr.Interface(
47
  fn=generate,
48
  inputs=[
49
  gr.Image(type="filepath", label="πŸ“· Face Image"),
50
- gr.Audio(type="filepath", label="🎡 Audio File")
51
  ],
52
  outputs=[
53
  gr.Video(label="πŸ“Ή Generated Video"),
54
- gr.Textbox(label="Status", lines=2)
55
  ],
56
  title="🎬 Video-Retalking Lip Sync",
57
- description="Upload a face image and audio to generate lip-synced video"
58
  )
59
 
60
  if __name__ == "__main__":
61
- demo.launch()
 
 
 
1
  import os
2
+ import shutil
3
+ import subprocess
4
  from pathlib import Path
5
+ import gradio as gr
6
+
7
+ REPO_DIR = Path("vrt")
8
+ SETUP_FLAG = Path("setup_done.txt")
9
+ OUTPUT_DIR = Path("outputs")
10
+ OUTPUT_DIR.mkdir(exist_ok=True)
11
+
12
+ def run(cmd, cwd=None):
13
+ """
14
+ Run a shell command safely and raise a helpful error if it fails.
15
+ """
16
+ p = subprocess.run(
17
+ cmd,
18
+ cwd=str(cwd) if cwd else None,
19
+ stdout=subprocess.PIPE,
20
+ stderr=subprocess.STDOUT,
21
+ text=True,
22
+ check=False
23
+ )
24
+ if p.returncode != 0:
25
+ raise RuntimeError(f"Command failed ({p.returncode}): {' '.join(cmd)}\n\n{p.stdout}")
26
+ return p.stdout
27
 
28
  def setup():
29
+ """
30
+ One-time setup: clone repo + download models.
31
+ Uses a flag file so it doesn't redo work every request.
32
+ """
33
+ if SETUP_FLAG.exists() and REPO_DIR.exists():
34
  return
35
+
36
  print("Setting up Video-Retalking...")
37
+
38
+ if not REPO_DIR.exists():
39
+ run(["git", "clone", "https://github.com/OpenTalker/video-retalking.git", str(REPO_DIR)])
40
+
41
+ # Download models (idempotent in most cases)
42
+ run(["bash", "scripts/download_models.sh"], cwd=REPO_DIR)
43
+
44
+ SETUP_FLAG.touch()
 
 
45
  print("βœ… Setup complete!")
46
 
47
+ def generate(image_path, audio_path):
48
+ if not image_path or not audio_path:
49
  return None, "❌ Upload both image and audio!"
50
+
51
  try:
52
  setup()
53
+
54
+ image_path = Path(image_path).resolve()
55
+ audio_path = Path(audio_path).resolve()
56
+
57
+ if not image_path.exists():
58
+ return None, f"❌ Image not found: {image_path}"
59
+ if not audio_path.exists():
60
+ return None, f"❌ Audio not found: {audio_path}"
61
+
62
+ # Unique output name per run
63
+ out_path = (OUTPUT_DIR / "result.mp4").resolve()
64
+
65
+ # Clean old output if any
66
+ if out_path.exists():
67
+ out_path.unlink()
68
+
69
  # Run inference
70
+ cmd = [
71
+ "python",
72
+ "inference.py",
73
+ "--face", str(image_path),
74
+ "--audio", str(audio_path),
75
+ "--outfile", str(out_path),
76
+ ]
77
+ run(cmd, cwd=REPO_DIR)
78
+
79
+ if out_path.exists():
80
+ return str(out_path), "βœ… Video generated successfully!"
81
+ return None, "❌ Failed to generate video (no output file created)."
82
+
83
  except Exception as e:
84
+ return None, f"❌ Error: {e}"
85
 
 
86
  demo = gr.Interface(
87
  fn=generate,
88
  inputs=[
89
  gr.Image(type="filepath", label="πŸ“· Face Image"),
90
+ gr.Audio(type="filepath", label="🎡 Audio File"),
91
  ],
92
  outputs=[
93
  gr.Video(label="πŸ“Ή Generated Video"),
94
+ gr.Textbox(label="Status", lines=3),
95
  ],
96
  title="🎬 Video-Retalking Lip Sync",
97
+ description="Upload a face image and audio to generate a lip-synced video.",
98
  )
99
 
100
  if __name__ == "__main__":
101
+ demo.launch()