aheedsajid commited on
Commit
e8a5179
·
verified ·
1 Parent(s): b12a5e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -95
app.py CHANGED
@@ -13,12 +13,6 @@ import os
13
  from PIL import Image
14
  import requests
15
  from io import BytesIO
16
- import time
17
- import threading
18
-
19
- # Add a semaphore to limit concurrent processing - keep this for roop function level concurrency control
20
- processing_semaphore = threading.Semaphore(5)
21
-
22
  def load_image_from_url(url):
23
  try:
24
  response = requests.get(url)
@@ -26,115 +20,82 @@ def load_image_from_url(url):
26
  return np.array(img)
27
  except:
28
  return None
29
-
30
  def swap_face(source_input, target_input, source_url, target_url, doFaceEnhancer):
31
- # Acquire semaphore with timeout - keep this to limit concurrent roop processing
32
- if not processing_semaphore.acquire(timeout=60): # 60 second timeout
33
- raise gr.Error("Server is busy. Please try again in a few minutes.")
34
-
35
- try:
36
- # Handle source image
37
- source_file = None
38
- if source_input is not None:
39
- source_file = source_input
40
- elif source_url:
41
- source_file = load_image_from_url(source_url)
42
-
43
- # Handle target image
44
- target_file = None
45
- if target_input is not None:
46
- target_file = target_input
47
- elif target_url:
48
- target_file = load_image_from_url(target_url)
49
-
50
- if source_file is None or target_file is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  return None
52
 
53
- # Generate unique filenames for concurrent processing
54
- timestamp = int(time.time() * 1000)
55
- source_path = f"input_{timestamp}.jpg"
56
- target_path = f"target_{timestamp}.jpg"
57
- output_path = f"output_{timestamp}.jpg"
58
-
59
- source_image = Image.fromarray(source_file)
60
- source_image.save(source_path)
61
- target_image = Image.fromarray(target_file)
62
- target_image.save(target_path)
63
-
64
- roop.globals.source_path = source_path
65
- roop.globals.target_path = target_path
66
- roop.globals.output_path = normalize_output_path(
67
- roop.globals.source_path, roop.globals.target_path, output_path
68
- )
69
-
70
- if doFaceEnhancer:
71
- roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
72
- else:
73
- roop.globals.frame_processors = ["face_swapper"]
74
-
75
- roop.globals.headless = True
76
- roop.globals.keep_fps = True
77
- roop.globals.keep_audio = True
78
- roop.globals.keep_frames = False
79
- roop.globals.many_faces = False
80
- roop.globals.video_encoder = "libx264"
81
- roop.globals.video_quality = 18
82
- roop.globals.max_memory = suggest_max_memory()
83
- roop.globals.execution_providers = decode_execution_providers(["cuda"])
84
- roop.globals.execution_threads = suggest_execution_threads()
85
-
86
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
87
- if not frame_processor.pre_check():
88
- return None
89
-
90
- start()
91
-
92
- # Clean up temporary files
93
- try:
94
- os.remove(source_path)
95
- os.remove(target_path)
96
- except:
97
- pass
98
-
99
- return output_path
100
-
101
- finally:
102
- # Always release the semaphore
103
- processing_semaphore.release()
104
-
105
  html_section_1 = "<div><h1>Welcome to the NSFW Face Swap & API</h1></div>"
106
- html_section_2 = '''<div>
107
- <p>Upload your source and target images to swap faces. Optionally, use the face enhancer feature for HD Results.</p>
108
- <h2><br /><strong>For fast bulk swap and API visit:</strong> 
109
- <a href="https://picfy.xyz/" target="_blank" rel="noopener">https://picfy.xyz/</a><br />
110
- <strong>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2></div>
111
- <br>Start Face Swap SaaS on WordPress:</strong> 
112
- <a href="https://www.codester.com/aheed/" target="_blank" rel="noopener">https://www.codester.com/aheed/</a>'''
113
-
114
- with gr.Blocks() as app:
115
  gr.HTML(html_section_1)
116
  gr.HTML(html_section_2)
117
  with gr.Tabs():
118
  with gr.Tab("Upload Images"):
119
- interface1 = gr.Interface(
120
  fn=lambda s, t, d: swap_face(s, t, None, None, d),
121
  inputs=[
122
  gr.Image(label="Source Image"),
123
  gr.Image(label="Target Image"),
124
  gr.Checkbox(label="face_enhancer?", info="do face enhancer?")
125
  ],
126
- outputs="image",
127
  )
128
  with gr.Tab("Image URLs"):
129
- interface2 = gr.Interface(
130
  fn=lambda s, t, d: swap_face(None, None, s, t, d),
131
  inputs=[
132
  gr.Textbox(label="Source Image URL"),
133
  gr.Textbox(label="Target Image URL"),
134
  gr.Checkbox(label="face_enhancer?", info="do face enhancer?")
135
  ],
136
- outputs="image",
137
  )
138
-
139
- if __name__ == "__main__":
140
- app.queue(default_concurrency_limit=10).launch() # Increased default_concurrency_limit to 10
 
13
  from PIL import Image
14
  import requests
15
  from io import BytesIO
 
 
 
 
 
 
16
  def load_image_from_url(url):
17
  try:
18
  response = requests.get(url)
 
20
  return np.array(img)
21
  except:
22
  return None
 
23
  def swap_face(source_input, target_input, source_url, target_url, doFaceEnhancer):
24
+ # Handle source image
25
+ source_file = None
26
+ if source_input is not None:
27
+ source_file = source_input
28
+ elif source_url:
29
+ source_file = load_image_from_url(source_url)
30
+
31
+ # Handle target image
32
+ target_file = None
33
+ if target_input is not None:
34
+ target_file = target_input
35
+ elif target_url:
36
+ target_file = load_image_from_url(target_url)
37
+
38
+ if source_file is None or target_file is None:
39
+ return None
40
+ source_path = "input.jpg"
41
+ target_path = "target.jpg"
42
+ source_image = Image.fromarray(source_file)
43
+ source_image.save(source_path)
44
+ target_image = Image.fromarray(target_file)
45
+ target_image.save(target_path)
46
+ roop.globals.source_path = source_path
47
+ roop.globals.target_path = target_path
48
+ output_path = "output.jpg"
49
+ roop.globals.output_path = normalize_output_path(
50
+ roop.globals.source_path, roop.globals.target_path, output_path
51
+ )
52
+
53
+ if doFaceEnhancer:
54
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
55
+ else:
56
+ roop.globals.frame_processors = ["face_swapper"]
57
+
58
+ roop.globals.headless = True
59
+ roop.globals.keep_fps = True
60
+ roop.globals.keep_audio = True
61
+ roop.globals.keep_frames = False
62
+ roop.globals.many_faces = False
63
+ roop.globals.video_encoder = "libx264"
64
+ roop.globals.video_quality = 18
65
+ roop.globals.max_memory = suggest_max_memory()
66
+ roop.globals.execution_providers = decode_execution_providers(["cuda"])
67
+ roop.globals.execution_threads = suggest_execution_threads()
68
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
69
+ if not frame_processor.pre_check():
70
  return None
71
 
72
+ start()
73
+ return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  html_section_1 = "<div><h1>Welcome to the NSFW Face Swap & API</h1></div>"
75
+ html_section_2 = '<div><p>Upload your source and target images to swap faces. Optionally, use the face enhancer feature for HD Results.</p><h2><br /><strong>For fast bulk swap and API visit:</strong>&nbsp;<a href="https://picfy.xyz/" target="_blank" rel="noopener">https://picfy.xyz/</a><br /> <strong>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2></div><br>Start Face Swap SaaS on WordPress:</strong>&nbsp;<a href="https://www.codester.com/aheed/" target="_blank" rel="noopener">https://www.codester.com/aheed/</a>'
76
+ app = gr.Blocks()
77
+ with app:
 
 
 
 
 
 
78
  gr.HTML(html_section_1)
79
  gr.HTML(html_section_2)
80
  with gr.Tabs():
81
  with gr.Tab("Upload Images"):
82
+ gr.Interface(
83
  fn=lambda s, t, d: swap_face(s, t, None, None, d),
84
  inputs=[
85
  gr.Image(label="Source Image"),
86
  gr.Image(label="Target Image"),
87
  gr.Checkbox(label="face_enhancer?", info="do face enhancer?")
88
  ],
89
+ outputs="image"
90
  )
91
  with gr.Tab("Image URLs"):
92
+ gr.Interface(
93
  fn=lambda s, t, d: swap_face(None, None, s, t, d),
94
  inputs=[
95
  gr.Textbox(label="Source Image URL"),
96
  gr.Textbox(label="Target Image URL"),
97
  gr.Checkbox(label="face_enhancer?", info="do face enhancer?")
98
  ],
99
+ outputs="image"
100
  )
101
+ app.launch()