benner3000 commited on
Commit
dee344d
·
verified ·
1 Parent(s): ec4c855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -19
app.py CHANGED
@@ -14,6 +14,38 @@ from roop.utilities import normalize_output_path
14
  import os
15
  from PIL import Image
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  article_text = """
18
  <div style="text-align: center;">
19
  <p>Leer zelf Generative AI-tools te bouwen en te begrijpen!</p>
@@ -22,8 +54,14 @@ article_text = """
22
  """
23
 
24
 
25
- def swap_face(source_file, target_file,doFaceEnhancer):
26
-
 
 
 
 
 
 
27
  source_path = "input.jpg"
28
  target_path = "target.jpg"
29
 
@@ -42,7 +80,7 @@ def swap_face(source_file, target_file,doFaceEnhancer):
42
  roop.globals.source_path, roop.globals.target_path, output_path
43
  )
44
  if doFaceEnhancer == True:
45
- roop.globals.frame_processors = ["face_swapper","face_enhancer"]
46
  else:
47
  roop.globals.frame_processors = ["face_swapper"]
48
  roop.globals.headless = True
@@ -70,19 +108,46 @@ def swap_face(source_file, target_file,doFaceEnhancer):
70
  return
71
 
72
  start()
73
- return output_path
74
-
75
-
76
- app = gr.Interface(
77
- fn=swap_face,
78
- inputs=[gr.Image(),
79
- gr.Image(),
80
- gr.Checkbox(label="face_enhancer?",
81
- info="do face enhancer?")],
82
- outputs="image",
83
- article=article_text,
84
- allow_flagging="never"
85
- )
86
- app.launch(
87
- show_api=False
88
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  import os
15
  from PIL import Image
16
 
17
+ # Loader HTML and CSS
18
+ loader_html = """
19
+ <div id="loader" style="
20
+ position: fixed;
21
+ top: 0;
22
+ left: 0;
23
+ width: 100%;
24
+ height: 100%;
25
+ background: rgba(255, 255, 255, 0.9);
26
+ display: none; /* Hidden by default */
27
+ justify-content: center;
28
+ align-items: center;
29
+ z-index: 1000;
30
+ ">
31
+ <div class="spinner" style="
32
+ width: 50px;
33
+ height: 50px;
34
+ border: 4px solid #f3f3f3;
35
+ border-top: 4px solid #3498db;
36
+ border-radius: 50%;
37
+ animation: spin 1s linear infinite;
38
+ "></div>
39
+ </div>
40
+
41
+ <style>
42
+ @keyframes spin {
43
+ 0% { transform: rotate(0deg); }
44
+ 100% { transform: rotate(360deg); }
45
+ }
46
+ </style>
47
+ """
48
+
49
  article_text = """
50
  <div style="text-align: center;">
51
  <p>Leer zelf Generative AI-tools te bouwen en te begrijpen!</p>
 
54
  """
55
 
56
 
57
+ def swap_face(source_file, target_file, doFaceEnhancer):
58
+ # Show the loader
59
+ show_loader = """
60
+ <script>
61
+ document.getElementById('loader').style.display = 'flex';
62
+ </script>
63
+ """
64
+
65
  source_path = "input.jpg"
66
  target_path = "target.jpg"
67
 
 
80
  roop.globals.source_path, roop.globals.target_path, output_path
81
  )
82
  if doFaceEnhancer == True:
83
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
84
  else:
85
  roop.globals.frame_processors = ["face_swapper"]
86
  roop.globals.headless = True
 
108
  return
109
 
110
  start()
111
+
112
+ # Hide the loader
113
+ hide_loader = """
114
+ <script>
115
+ document.getElementById('loader').style.display = 'none';
116
+ </script>
117
+ """
118
+
119
+ return output_path, hide_loader
120
+
121
+
122
+ # ============================================================
123
+ # CHANGES START HERE
124
+ # ============================================================
125
+
126
+ # Use Gradio Blocks for more flexibility
127
+ with gr.Blocks() as demo:
128
+
129
+ # Add the loader HTML
130
+ gr.HTML(loader_html)
131
+
132
+ # Add the face-swap interface
133
+ with gr.Column(): # Stack everything vertically
134
+ source_image = gr.Image(label="Source Image")
135
+ target_image = gr.Image(label="Target Image")
136
+ do_face_enhancer = gr.Checkbox(label="Face Enhancer?", info="Do face enhancer?")
137
+ submit_button = gr.Button("Swap Faces")
138
+ output_image = gr.Image(label="Output Image")
139
+ gr.HTML(article_text)
140
+
141
+ # Define the function to call on button click
142
+ submit_button.click(
143
+ fn=swap_face,
144
+ inputs=[source_image, target_image, do_face_enhancer],
145
+ outputs=[output_image, gr.HTML()] # Output image and HTML (to hide loader)
146
+ )
147
+
148
+ # ============================================================
149
+ # CHANGES END HERE
150
+ # ============================================================
151
+
152
+ # Launch the app
153
+ demo.launch(show_api=False)