Stiphan commited on
Commit
8005ca5
·
verified ·
1 Parent(s): e9892ba

Add AI Face Swap Video feature

Browse files

Swaps a provided face image into a target video using AI face swap model.

Files changed (1) hide show
  1. face_swap.py +60 -0
face_swap.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import tempfile
4
+ import os
5
+
6
+ # Hugging Face API Config
7
+ HF_API_TOKEN = "YOUR_HF_API_KEY" # Apna token lagao
8
+ MODEL_URL = "https://api-inference.huggingface.co/models/deepinsight/inswapper"
9
+ HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
10
+
11
+ def swap_face(video_file, face_image):
12
+ """🎥 Video + Face → AI Swapped Video"""
13
+ if video_file is None or face_image is None:
14
+ return None
15
+
16
+ try:
17
+ # Save temp video
18
+ temp_vid = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
19
+ with open(temp_vid.name, "wb") as f:
20
+ f.write(video_file.read())
21
+
22
+ # Save temp face image
23
+ temp_face = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
24
+ face_image.save(temp_face.name)
25
+
26
+ # Send to Hugging Face model (if supports both inputs together)
27
+ files = {
28
+ "video": open(temp_vid.name, "rb"),
29
+ "target_face": open(temp_face.name, "rb")
30
+ }
31
+
32
+ resp = requests.post(MODEL_URL, headers=HEADERS, files=files)
33
+
34
+ if resp.status_code != 200:
35
+ print("Error:", resp.text)
36
+ return None
37
+
38
+ # Save output
39
+ temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
40
+ with open(temp_output.name, "wb") as f:
41
+ f.write(resp.content)
42
+
43
+ return temp_output.name
44
+ except Exception as e:
45
+ print(f"Error: {e}")
46
+ return None
47
+
48
+ # ---------- Gradio UI ----------
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("<h2 style='text-align:center'>🤖 AI Face Swap Video — SaEdit MultiAi</h2>")
51
+
52
+ vid_input = gr.File(type="file", file_types=[".mp4"], label="🎥 Upload Target Video")
53
+ face_input = gr.Image(type="pil", label="🖼 Upload Face Image")
54
+ btn = gr.Button("🔄 Swap Face")
55
+ output_video = gr.Video(label="🎬 Face Swapped Output")
56
+
57
+ btn.click(swap_face, inputs=[vid_input, face_input], outputs=output_video)
58
+
59
+ if __name__ == "__main__":
60
+ demo.launch()