tonyassi commited on
Commit
bf6265f
Β·
verified Β·
1 Parent(s): 088b02f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +108 -2
app.py CHANGED
@@ -1,3 +1,109 @@
1
- import os
 
 
 
2
 
3
- exec(os.environ.get('CODE'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os, uuid, time
3
+ from gradio_client import Client, handle_file
4
+ from moviepy.editor import VideoFileClip # NEW
5
 
6
+ hf_token = os.environ.get("TOKEN")
7
+ output_dir = "uploads/output"
8
+ os.makedirs(output_dir, exist_ok=True)
9
+
10
+ client = Client("tonyassi/vfs2-cpu", hf_token=hf_token, download_files=output_dir)
11
+
12
+ # ── helper ───────────────────────────────────────────────────────────
13
+ def preprocess_video(path: str, target_fps: int = 12,
14
+ target_size: int = 800, target_len: int = 4) -> str:
15
+ """
16
+ Returns a path to a temp-file that is
17
+ ≀ target_len seconds, target_fps FPS,
18
+ and resized so the longest side == target_size.
19
+ """
20
+ clip = VideoFileClip(path)
21
+
22
+ # 1) trim
23
+ if clip.duration > target_len:
24
+ clip = clip.subclip(0, target_len)
25
+
26
+ # 2) resize (longest side)
27
+ w, h = clip.size
28
+ if w >= h:
29
+ clip = clip.resize(width=target_size)
30
+ else:
31
+ clip = clip.resize(height=target_size)
32
+
33
+ # 3) FPS
34
+ clip = clip.set_fps(target_fps)
35
+
36
+ # 4) write to temp file
37
+ out_path = os.path.join(
38
+ output_dir, f"pre_{uuid.uuid4().hex}.mp4"
39
+ )
40
+ clip.write_videofile(
41
+ out_path,
42
+ codec="libx264",
43
+ audio_codec="aac",
44
+ fps=target_fps,
45
+ verbose=False,
46
+ logger=None
47
+ )
48
+ clip.close()
49
+ return out_path
50
+
51
+ # ── main generate ────────────────────────────────────────────────────
52
+ def generate(input_image, input_video, gender):
53
+
54
+ if (gender=="all"): gender = None
55
+
56
+ try:
57
+ # β‡’ preprocess video locally
58
+ pre_video = preprocess_video(input_video)
59
+
60
+ # β‡’ submit job to remote Space
61
+ job = client.submit(
62
+ input_image=handle_file(input_image),
63
+ input_video={"video": handle_file(pre_video)},
64
+ device='cpu',
65
+ selector='many',
66
+ gender=gender,
67
+ race=None,
68
+ order=None,
69
+ api_name="/predict"
70
+ )
71
+
72
+ # wait for completion
73
+ while not job.done():
74
+ print("Waiting for remote job to finish...")
75
+ time.sleep(5)
76
+
77
+ if not job.status().success:
78
+ print("Job failed or was cancelled:", job.status())
79
+ return None
80
+
81
+ video_path = job.outputs()[0]["video"]
82
+ return video_path
83
+
84
+ except Exception as e:
85
+ print("Error occurred:", e)
86
+ return None
87
+
88
+ # ── Gradio interface (unchanged) ─────────────────────────────────────
89
+ demo = gr.Interface(
90
+ fn=generate,
91
+ title="Deep Fake Video",
92
+ description="""
93
+ by [Tony Assi](https://www.tonyassi.com/)
94
+
95
+ Swaps the source image face onto the target video. Videos get downsampled to 800 pixels, 4 sec, and 12 fps to cut down render time (~5 minutes). Please ❀️ this Space. [email protected] for business inquiries.
96
+ """,
97
+ inputs=[
98
+ gr.Image(type="filepath", label="Source Image"),
99
+ gr.Video(label="Target Video"),
100
+ gr.Radio(choices=["all", "female", "male"], label="Gender", value="all"),
101
+ ],
102
+ outputs=gr.Video(label="Result"),
103
+ flagging_mode="never",
104
+ examples=[['bella.jpg', 'wizard.mp4', 'all']],
105
+ cache_examples=True,
106
+ )
107
+
108
+ if __name__ == "__main__":
109
+ demo.launch()