aheedsajid commited on
Commit
a49d20b
·
verified ·
1 Parent(s): 011051f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -59
app.py CHANGED
@@ -1,74 +1,67 @@
1
  import numpy as np
2
  import gradio as gr
3
  import roop.globals
4
- from roop.core import start, decode_execution_providers, suggest_max_memory, suggest_execution_threads
 
 
 
 
 
5
  from roop.processors.frame.core import get_frame_processors_modules
6
  from roop.utilities import normalize_output_path
7
  import os
8
  from PIL import Image
9
- import requests
10
- from io import BytesIO
11
-
12
- def download_image(url):
13
- response = requests.get(url)
14
- return Image.open(BytesIO(response.content))
15
-
16
- def process_input(input_data):
17
- if isinstance(input_data, str) and (input_data.startswith('http://') or input_data.startswith('https://')):
18
- return np.array(download_image(input_data))
19
- return input_data
20
-
21
  def swap_face(source_file, target_file, doFaceEnhancer):
22
- try:
23
- source_path = "input.jpg"
24
- target_path = "target.jpg"
25
-
26
- source_image = Image.fromarray(source_file) if isinstance(source_file, np.ndarray) else source_file
27
- target_image = Image.fromarray(target_file) if isinstance(target_file, np.ndarray) else target_file
28
-
29
- source_image.save(source_path)
30
- target_image.save(target_path)
31
-
32
- roop.globals.source_path = source_path
33
- roop.globals.target_path = target_path
34
- output_path = "output.jpg"
35
- roop.globals.output_path = normalize_output_path(source_path, target_path, output_path)
36
- roop.globals.frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
37
- roop.globals.headless = True
38
- roop.globals.keep_fps = True
39
- roop.globals.keep_audio = True
40
- roop.globals.keep_frames = False
41
- roop.globals.many_faces = False
42
- roop.globals.video_encoder = "libx264"
43
- roop.globals.video_quality = 18
44
- roop.globals.max_memory = suggest_max_memory()
45
- roop.globals.execution_providers = decode_execution_providers(["cuda"])
46
- roop.globals.execution_threads = suggest_execution_threads()
47
-
48
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
49
- if not frame_processor.pre_check():
50
- raise Exception("Frame processor pre-check failed")
51
-
52
- start()
53
- return output_path
54
-
55
- except Exception as e:
56
- print(f"Error during face swap: {str(e)}")
57
- raise gr.Error(f"Face swap failed: {str(e)}")
58
-
 
 
 
 
 
 
59
  app = gr.Blocks()
60
  with app:
61
- gr.HTML("<div><h1>Welcome to the NSFW Face Swap & API</h1></div>")
62
- gr.HTML('<div><p>Upload your source and target images or provide URLs to swap faces.</p></div>')
63
-
64
  gr.Interface(
65
  fn=swap_face,
66
- inputs=[
67
- gr.Image(label="Source Image", type="numpy"),
68
- gr.Image(label="Target Image", type="numpy"),
69
- gr.Checkbox(label="Face Enhancer", info="Enable face enhancement")
70
- ],
71
  outputs="image"
72
  )
73
-
74
  app.launch()
 
1
  import numpy as np
2
  import gradio as gr
3
  import roop.globals
4
+ from roop.core import (
5
+ start,
6
+ decode_execution_providers,
7
+ suggest_max_memory,
8
+ suggest_execution_threads,
9
+ )
10
  from roop.processors.frame.core import get_frame_processors_modules
11
  from roop.utilities import normalize_output_path
12
  import os
13
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
14
  def swap_face(source_file, target_file, doFaceEnhancer):
15
+ source_path = "input.jpg"
16
+ target_path = "target.jpg"
17
+ source_image = Image.fromarray(source_file)
18
+ source_image.save(source_path)
19
+ target_image = Image.fromarray(target_file)
20
+ target_image.save(target_path)
21
+ print("source_path: ", source_path)
22
+ print("target_path: ", target_path)
23
+ roop.globals.source_path = source_path
24
+ roop.globals.target_path = target_path
25
+ output_path = "output.jpg"
26
+ roop.globals.output_path = normalize_output_path(
27
+ roop.globals.source_path, roop.globals.target_path, output_path
28
+ )
29
+ if doFaceEnhancer:
30
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
31
+ else:
32
+ roop.globals.frame_processors = ["face_swapper"]
33
+ roop.globals.headless = True
34
+ roop.globals.keep_fps = True
35
+ roop.globals.keep_audio = True
36
+ roop.globals.keep_frames = False
37
+ roop.globals.many_faces = False
38
+ roop.globals.video_encoder = "libx264"
39
+ roop.globals.video_quality = 18
40
+ roop.globals.max_memory = suggest_max_memory()
41
+ roop.globals.execution_providers = decode_execution_providers(["cuda"])
42
+ roop.globals.execution_threads = suggest_execution_threads()
43
+ print(
44
+ "start process",
45
+ roop.globals.source_path,
46
+ roop.globals.target_path,
47
+ roop.globals.output_path,
48
+ )
49
+ for frame_processor in get_frame_processors_modules(
50
+ roop.globals.frame_processors
51
+ ):
52
+ if not frame_processor.pre_check():
53
+ return
54
+ start()
55
+ return output_path
56
+ html_section_1 = "<div><h1>Welcome to the NSFW Face Swap & API</h1></div>"
57
+ 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>'
58
  app = gr.Blocks()
59
  with app:
60
+ gr.HTML(html_section_1)
61
+ gr.HTML(html_section_2)
 
62
  gr.Interface(
63
  fn=swap_face,
64
+ inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="face_enhancer?", info="do face enhancer?")],
 
 
 
 
65
  outputs="image"
66
  )
 
67
  app.launch()