aiguruji97 commited on
Commit
efc97a6
·
verified ·
1 Parent(s): d27c386

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import roop.globals
3
  import roop.core
4
  import os
 
5
 
6
  # ---------------------------
7
  # GLOBAL CONFIG (RUNS ONCE)
@@ -25,6 +26,25 @@ roop.globals.max_memory = 8 # limit memory for stability
25
 
26
  print("🔥 Roop initialized (CPU optimized)")
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # ---------------------------
29
  # MAIN FUNCTION
30
  # ---------------------------
@@ -32,13 +52,9 @@ def swap_face(source_file, target_file, enable_enhancer):
32
  if source_file is None or target_file is None:
33
  return None
34
 
35
- source_path = source_file if isinstance(source_file, str) else source_file.name
36
- target_path = target_file if isinstance(target_file, str) else target_file.name
37
-
38
- # Allow only images
39
- _, target_ext = os.path.splitext(target_path)
40
- if target_ext.lower() not in ['.jpg', '.jpeg', '.png', '.bmp', '.webp']:
41
- raise gr.Error("❌ Only image files allowed (CPU space limitation).")
42
 
43
  output_path = os.path.join(os.getcwd(), "output.png")
44
 
@@ -55,7 +71,7 @@ def swap_face(source_file, target_file, enable_enhancer):
55
  roop.globals.target_path = target_path
56
  roop.globals.output_path = output_path
57
 
58
- # Processors config (NO RESET BUG)
59
  processors = ['face_swapper']
60
  if enable_enhancer:
61
  processors.append('face_enhancer')
@@ -99,7 +115,7 @@ with gr.Blocks(title="AI Guruji Face Swap") as demo:
99
  submit_btn = gr.Button("🚀 Swap Face", variant="primary")
100
 
101
  with gr.Column():
102
- result_image = gr.Image(label="Result")
103
 
104
  submit_btn.click(
105
  fn=swap_face,
@@ -112,4 +128,4 @@ with gr.Blocks(title="AI Guruji Face Swap") as demo:
112
  # ---------------------------
113
  if __name__ == "__main__":
114
  demo.queue(max_size=10) # Prevent overload
115
- demo.launch()
 
2
  import roop.globals
3
  import roop.core
4
  import os
5
+ import shutil
6
 
7
  # ---------------------------
8
  # GLOBAL CONFIG (RUNS ONCE)
 
26
 
27
  print("🔥 Roop initialized (CPU optimized)")
28
 
29
+ def get_valid_image_path(file_obj, suffix=""):
30
+ if file_obj is None:
31
+ return None
32
+
33
+ # Check if API sent string, dict, or file object
34
+ if isinstance(file_obj, str):
35
+ path = file_obj
36
+ elif isinstance(file_obj, dict) and 'path' in file_obj:
37
+ path = file_obj['path']
38
+ else:
39
+ path = getattr(file_obj, 'name', str(file_obj))
40
+
41
+ # Agari extension missing hai API ki wajah se, usko .png dedo! (Bug Fixed Here)
42
+ if not any(path.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.bmp', '.webp']):
43
+ new_path = path + suffix + ".png"
44
+ shutil.copy(path, new_path)
45
+ return new_path
46
+ return path
47
+
48
  # ---------------------------
49
  # MAIN FUNCTION
50
  # ---------------------------
 
52
  if source_file is None or target_file is None:
53
  return None
54
 
55
+ # Ab humein farq nahi padta kon kaisa image bhej raha hai!
56
+ source_path = get_valid_image_path(source_file, "_src")
57
+ target_path = get_valid_image_path(target_file, "_tgt")
 
 
 
 
58
 
59
  output_path = os.path.join(os.getcwd(), "output.png")
60
 
 
71
  roop.globals.target_path = target_path
72
  roop.globals.output_path = output_path
73
 
74
+ # Processors config
75
  processors = ['face_swapper']
76
  if enable_enhancer:
77
  processors.append('face_enhancer')
 
115
  submit_btn = gr.Button("🚀 Swap Face", variant="primary")
116
 
117
  with gr.Column():
118
+ result_image = gr.Image(label="Result", type="filepath")
119
 
120
  submit_btn.click(
121
  fn=swap_face,
 
128
  # ---------------------------
129
  if __name__ == "__main__":
130
  demo.queue(max_size=10) # Prevent overload
131
+ demo.launch()