sandhyasinha655 commited on
Commit
8bade78
·
verified ·
1 Parent(s): 5748686

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -25
app.py CHANGED
@@ -1,32 +1,75 @@
1
- import cv2
2
- import os
3
  import gradio as gr
4
- from run import run_faceswap
 
 
 
 
 
 
 
 
 
 
5
 
6
- def process_face_swap(source_image, target_file):
7
- source_path = "face.png"
8
- target_path = "target" + os.path.splitext(target_file.name)[-1]
 
9
 
10
- # Save uploaded numpy image using OpenCV
11
- cv2.imwrite(source_path, cv2.cvtColor(source_image, cv2.COLOR_RGB2BGR))
 
 
 
 
 
 
 
12
 
13
- # Save uploaded target file
14
- with open(target_path, "wb") as f:
15
- f.write(target_file.read())
 
 
 
 
 
 
 
 
 
16
 
17
- # Call face swap
18
- output_path = run_faceswap(source_path, target_path)
 
 
 
 
 
 
 
19
  return output_path
20
 
21
- iface = gr.Interface(
22
- fn=process_face_swap,
23
- inputs=[
24
- gr.Image(label="Source Face (face.png)", type="numpy"),
25
- gr.File(label="Target Image or Video (.jpg/.mp4)")
26
- ],
27
- outputs=gr.File(label="Swapped Output"),
28
- title="Lightweight Face Swap (User Logic)",
29
- description="Upload face.png and target image/video. Output will be result.jpg or result.mp4"
30
- )
31
-
32
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import os
3
+ import sys
4
+
5
+ # YEH SABSE ZAROORI LINE HAI
6
+ # Hum 'roop' folder ko system path mein add kar rahe hain taaki Python use dhoondh sake.
7
+ sys.path.append('roop')
8
+
9
+ # AB HUM SAHI JAGAH SE IMPORT KAR RAHE HAIN
10
+ # 'roop' folder ke andar 'core.py' file se 'run' function ko import kar rahe hain.
11
+ # Yahi aapke Colab ke video wale code ka logic hai.
12
+ from roop.core import run as roop_run
13
 
14
+ # --- Gradio Function ---
15
+ def face_swap(source_img, target_media):
16
+ if source_img is None or target_media is None:
17
+ raise gr.Error("Please upload both source image and target media.")
18
 
19
+ # Gradio se mili files ke temporary paths
20
+ source_path = source_img.name
21
+ target_path = target_media.name
22
+
23
+ # Output file ka naam tay karna
24
+ output_filename = os.path.basename(target_path)
25
+ # Output folder banayein agar nahi hai
26
+ os.makedirs("output", exist_ok=True)
27
+ output_path = os.path.join("output", f"result_{output_filename}")
28
 
29
+ # Roop ko chalane ke liye arguments set karna
30
+ args = [
31
+ "run.py", # yeh bas ek placeholder hai, asli kaam roop_run() function karega
32
+ "--source", source_path,
33
+ "--target", target_path,
34
+ "--output", output_path,
35
+ "--execution-provider", "cpu", # Free tier ke liye 'cpu'
36
+ "-e" # Face enhancer on
37
+ ]
38
+ sys.argv = args
39
+
40
+ print(f"Running roop with args: {sys.argv}")
41
 
42
+ # Asli face swap process yahan run hoga
43
+ try:
44
+ roop_run()
45
+ except Exception as e:
46
+ raise gr.Error(f"Face swap failed. Error: {str(e)}")
47
+
48
+ print(f"Processing complete. Result at: {output_path}")
49
+
50
+ # Result ko UI mein dikhana
51
  return output_path
52
 
53
+ # --- Gradio UI ---
54
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
55
+ gr.Markdown("# 🎭 Roop Face Swap AI")
56
+ gr.Markdown("Upload a source face and a target image/video, then click 'Swap Face'.")
57
+
58
+ with gr.Row():
59
+ source_image = gr.Image(label="Source Face (Single Face)", type="filepath")
60
+ target_media = gr.File(label="Target Image or Video", type="filepath")
61
+
62
+ submit_btn = gr.Button("Swap Face", variant="primary")
63
+ result_output = gr.File(label="Result", interactive=False)
64
+
65
+ submit_btn.click(
66
+ fn=face_swap,
67
+ inputs=[source_image, target_media],
68
+ outputs=result_output
69
+ )
70
+
71
+ gr.Markdown("---")
72
+ gr.Markdown("Created with help from AI. Make sure to use this tool responsibly.")
73
+
74
+ # App ko launch karein
75
+ app.launch()