sandhyasinha655 commited on
Commit
04053a0
·
verified ·
1 Parent(s): 914fc1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -3,8 +3,11 @@ import os
3
  import sys
4
  import base64
5
  import uuid
 
6
 
7
- # --- Purana Code (Koi Change Nahi) ---
 
 
8
  sys.path.append('roop')
9
  from roop.core import run as roop_run
10
 
@@ -18,30 +21,33 @@ def face_swap_logic(source_path, target_path):
18
  "--output", output_path, "--execution-provider", "cpu",
19
  ]
20
  sys.argv = args
21
- print(f"Running roop with args: {sys.argv}")
22
-
23
  try:
24
  roop_run()
25
  except SystemExit: pass
26
  except Exception as e:
27
- print(f"Error during face swap: {e}")
28
  return None
29
 
30
  if not os.path.exists(output_path):
31
  return None
32
-
33
  return output_path
34
 
35
- # --- Gradio UI (Yeh Web Users ke liye hai) ---
36
- with gr.Blocks(theme=gr.themes.Soft()) as app: # Yahan 'app' ek Gradio Blocks object hai
 
 
37
  gr.Markdown("# 🎭 Roop Face Swap AI")
38
  with gr.Tabs():
39
  with gr.TabItem("🖼️ Swap on Image"):
 
40
  with gr.Row():
41
  source_face_img = gr.Image(label="Source Face", type="filepath")
42
  target_image = gr.Image(label="Target Image", type="filepath")
43
  submit_btn_image = gr.Button("Swap Face on Image")
44
  result_image = gr.Image(label="Result Image")
 
45
  with gr.TabItem("🎬 Swap on Video"):
46
  with gr.Row():
47
  source_face_vid = gr.Image(label="Source Face", type="filepath")
@@ -49,16 +55,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as app: # Yahan 'app' ek Gradio Blocks ob
49
  submit_btn_video = gr.Button("Swap Face on Video")
50
  result_video = gr.Video(label="Result Video")
51
 
52
- def web_ui_swap(source, target):
53
- return face_swap_logic(source, target)
54
-
55
- submit_btn_image.click(fn=web_ui_swap, inputs=[source_face_img, target_image], outputs=result_image)
56
- submit_btn_video.click(fn=web_ui_swap, inputs=[source_face_vid, target_video], outputs=result_video)
57
 
58
-
59
- # --- Naya API Code (Yeh Aapke Android App ke liye hai) ---
60
- # Hum FastAPI ko alag se banane ke bajaye, seedha Gradio ke chalte hue server (app.app) mein hi apna API endpoint jod denge.
61
- # Yeh sabse saaf aur sahi tarika hai.
 
62
 
63
  def save_base64_to_file(b64_string, extension):
64
  filename = f"/tmp/{uuid.uuid4().hex}.{extension}"
@@ -66,7 +71,8 @@ def save_base64_to_file(b64_string, extension):
66
  f.write(base64.b64decode(b64_string))
67
  return filename
68
 
69
- @app.post("/api/swap-face") # Yahan 'app' Gradio ka Blocks object hai
 
70
  async def api_face_swap(payload: dict):
71
  source_b64 = payload.get("source_face_b64")
72
  target_b64 = payload.get("target_media_b64")
@@ -87,10 +93,14 @@ async def api_face_swap(payload: dict):
87
  os.remove(source_file_path)
88
  os.remove(target_file_path)
89
  os.remove(result_path)
90
-
91
  return {"result_b64": result_b64}
92
  else:
93
  return {"error": "Face swap fail hua."}
94
 
95
- # AAKHRI AUR SABSE ZAROORI LINE - APP KO LAUNCH KARNA
96
- app.launch()
 
 
 
 
 
 
3
  import sys
4
  import base64
5
  import uuid
6
+ from fastapi import FastAPI
7
 
8
+ # -------------------------------------------------------------------
9
+ # PART 1: CORE LOGIC (Yeh bilkul perfect hai, ismein koi change nahi)
10
+ # -------------------------------------------------------------------
11
  sys.path.append('roop')
12
  from roop.core import run as roop_run
13
 
 
21
  "--output", output_path, "--execution-provider", "cpu",
22
  ]
23
  sys.argv = args
24
+
 
25
  try:
26
  roop_run()
27
  except SystemExit: pass
28
  except Exception as e:
29
+ print(f"Face swap fail hua. Error: {str(e)}")
30
  return None
31
 
32
  if not os.path.exists(output_path):
33
  return None
34
+
35
  return output_path
36
 
37
+ # -------------------------------------------------------------------
38
+ # PART 2: GRADIO UI DEFINITION (Aapka Web Interface)
39
+ # -------------------------------------------------------------------
40
+ with gr.Blocks(theme=gr.themes.Soft()) as gradio_ui:
41
  gr.Markdown("# 🎭 Roop Face Swap AI")
42
  with gr.Tabs():
43
  with gr.TabItem("🖼️ Swap on Image"):
44
+ # ... (UI ka baaki code bilkul same)
45
  with gr.Row():
46
  source_face_img = gr.Image(label="Source Face", type="filepath")
47
  target_image = gr.Image(label="Target Image", type="filepath")
48
  submit_btn_image = gr.Button("Swap Face on Image")
49
  result_image = gr.Image(label="Result Image")
50
+
51
  with gr.TabItem("🎬 Swap on Video"):
52
  with gr.Row():
53
  source_face_vid = gr.Image(label="Source Face", type="filepath")
 
55
  submit_btn_video = gr.Button("Swap Face on Video")
56
  result_video = gr.Video(label="Result Video")
57
 
58
+ # UI ke buttons ko logic se jodna
59
+ submit_btn_image.click(fn=face_swap_logic, inputs=[source_face_img, target_image], outputs=result_image)
60
+ submit_btn_video.click(fn=face_swap_logic, inputs=[source_face_vid, target_video], outputs=result_video)
 
 
61
 
62
+ # -------------------------------------------------------------------
63
+ # PART 3: FASTAPI APP DEFINITION (Aapke Android App ke liye)
64
+ # -------------------------------------------------------------------
65
+ # Yahan hum main app banayenge jo Hugging Face chalaayega
66
+ app = FastAPI()
67
 
68
  def save_base64_to_file(b64_string, extension):
69
  filename = f"/tmp/{uuid.uuid4().hex}.{extension}"
 
71
  f.write(base64.b64decode(b64_string))
72
  return filename
73
 
74
+ # API ka darwaza
75
+ @app.post("/api/swap-face")
76
  async def api_face_swap(payload: dict):
77
  source_b64 = payload.get("source_face_b64")
78
  target_b64 = payload.get("target_media_b64")
 
93
  os.remove(source_file_path)
94
  os.remove(target_file_path)
95
  os.remove(result_path)
 
96
  return {"result_b64": result_b64}
97
  else:
98
  return {"error": "Face swap fail hua."}
99
 
100
+ # -------------------------------------------------------------------
101
+ # PART 4: MOUNTING (Dono ko Jodna)
102
+ # -------------------------------------------------------------------
103
+ # Yeh aakhri line Gradio UI (gradio_ui) ko FastAPI app (app) ke upar mount karti hai
104
+ app = gr.mount_gradio_app(app, gradio_ui, path="/")
105
+
106
+ # Ismein app.launch() ki zaroorat nahi hoti. Hugging Face khud 'app' object ko chala leta hai.