Apex-X commited on
Commit
aeba0b0
·
verified ·
1 Parent(s): 5e25751

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -1,3 +1,9 @@
 
 
 
 
 
 
1
  import gradio as gr
2
  import roop.globals
3
  from roop.core import (
@@ -8,40 +14,35 @@ from roop.core import (
8
  )
9
  from roop.processors.frame.core import get_frame_processors_modules
10
  from roop.utilities import normalize_output_path
11
- from PIL import Image
12
- import shutil
13
 
14
 
15
- def swap_face(source_file, target_file, doFaceEnhancer, mode):
16
- # Save source image always
17
  source_path = "input.jpg"
18
  source_image = Image.open(source_file)
19
  source_image.save(source_path)
20
  roop.globals.source_path = source_path
21
 
 
22
  if mode == "Image to Image":
23
- # target is image
24
  target_path = "target.jpg"
25
  target_image = Image.open(target_file)
26
  target_image.save(target_path)
27
  output_path = "output.jpg"
28
- else:
29
- # mode == "Image to Video"
30
- # target is video file path
31
  target_path = "target.mp4"
32
- shutil.copy(target_file, target_path)
 
33
  output_path = "output.mp4"
34
 
 
35
  roop.globals.target_path = target_path
36
  roop.globals.output_path = normalize_output_path(
37
  roop.globals.source_path, roop.globals.target_path, output_path
38
  )
39
 
40
- if doFaceEnhancer:
41
- roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
42
- else:
43
- roop.globals.frame_processors = ["face_swapper"]
44
-
45
  roop.globals.headless = True
46
  roop.globals.keep_fps = True
47
  roop.globals.keep_audio = True
@@ -53,28 +54,31 @@ def swap_face(source_file, target_file, doFaceEnhancer, mode):
53
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
54
  roop.globals.execution_threads = suggest_execution_threads()
55
 
 
 
 
 
56
  for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
57
  if not frame_processor.pre_check():
58
  return "Processor check failed"
59
 
 
60
  start()
61
 
62
  return roop.globals.output_path
63
 
64
 
 
65
  app = gr.Interface(
66
  fn=swap_face,
67
  inputs=[
68
- gr.Image(type="filepath", label="Source Face Image"),
69
- gr.File(label="Target File (Image or Video)"),
70
- gr.Checkbox(label="Apply Face Enhancer?"),
71
- gr.Dropdown(
72
- choices=["Image to Image", "Image to Video"],
73
- value="Image to Image",
74
- label="Select Swap Mode",
75
- ),
76
  ],
77
- outputs=gr.Video(label="Output (Video or Image)"),
78
  )
79
 
80
- app.launch()
 
 
1
+ # -*- coding: UTF-8 -*-
2
+ # !/usr/bin/env python
3
+
4
+ import os
5
+ import shutil
6
+ from PIL import Image
7
  import gradio as gr
8
  import roop.globals
9
  from roop.core import (
 
14
  )
15
  from roop.processors.frame.core import get_frame_processors_modules
16
  from roop.utilities import normalize_output_path
 
 
17
 
18
 
19
+ def swap_face(source_file, target_file, do_face_enhancer, mode):
20
+ # Save source image
21
  source_path = "input.jpg"
22
  source_image = Image.open(source_file)
23
  source_image.save(source_path)
24
  roop.globals.source_path = source_path
25
 
26
+ # Handle target file
27
  if mode == "Image to Image":
 
28
  target_path = "target.jpg"
29
  target_image = Image.open(target_file)
30
  target_image.save(target_path)
31
  output_path = "output.jpg"
32
+ else: # Image to Video
 
 
33
  target_path = "target.mp4"
34
+ with open(target_path, "wb") as f:
35
+ f.write(target_file.read())
36
  output_path = "output.mp4"
37
 
38
+ # Assign paths
39
  roop.globals.target_path = target_path
40
  roop.globals.output_path = normalize_output_path(
41
  roop.globals.source_path, roop.globals.target_path, output_path
42
  )
43
 
44
+ # Frame processor settings
45
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"] if do_face_enhancer else ["face_swapper"]
 
 
 
46
  roop.globals.headless = True
47
  roop.globals.keep_fps = True
48
  roop.globals.keep_audio = True
 
54
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
55
  roop.globals.execution_threads = suggest_execution_threads()
56
 
57
+ # ✅ Fix: Set temp_frame_quality to avoid crash
58
+ roop.globals.temp_frame_quality = 80
59
+
60
+ # Check modules
61
  for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
62
  if not frame_processor.pre_check():
63
  return "Processor check failed"
64
 
65
+ # Start the process
66
  start()
67
 
68
  return roop.globals.output_path
69
 
70
 
71
+ # Gradio UI
72
  app = gr.Interface(
73
  fn=swap_face,
74
  inputs=[
75
+ gr.Image(label="Source Image", type="filepath"),
76
+ gr.File(label="Target (Image or Video)", file_types=[".jpg", ".jpeg", ".png", ".mp4"]),
77
+ gr.Checkbox(label="Enable Face Enhancer?"),
78
+ gr.Radio(choices=["Image to Image", "Image to Video"], label="Swap Mode", value="Image to Image")
 
 
 
 
79
  ],
80
+ outputs=gr.outputs.Image(type="filepath", label="Output") # Will auto-handle video too
81
  )
82
 
83
+ # Launch app with public link
84
+ app.launch(share=True)