Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
import numpy as np
|
4 |
import gradio as gr
|
5 |
import roop.globals
|
@@ -13,72 +13,183 @@ from roop.processors.frame.core import get_frame_processors_modules
|
|
13 |
from roop.utilities import normalize_output_path
|
14 |
import os
|
15 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
"
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
|
|
68 |
title = "Face - Интегратор"
|
69 |
-
|
70 |
description = r"""
|
|
|
71 |
"""
|
72 |
article = r"""
|
73 |
<br><br><br><br><br>
|
74 |
"""
|
75 |
|
76 |
-
|
77 |
app = gr.Interface(
|
78 |
fn=swap_face,
|
79 |
title=title,
|
80 |
description=description,
|
81 |
article=article,
|
82 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
83 |
)
|
84 |
-
|
|
|
|
1 |
+
# -*- coding:UTF-8 -*-
|
2 |
+
#!/usr/bin/env python
|
3 |
import numpy as np
|
4 |
import gradio as gr
|
5 |
import roop.globals
|
|
|
13 |
from roop.utilities import normalize_output_path
|
14 |
import os
|
15 |
from PIL import Image
|
16 |
+
from datetime import datetime
|
17 |
+
from huggingface_hub import HfApi, login
|
18 |
+
from datasets import load_dataset, Dataset
|
19 |
+
import json
|
20 |
+
import shutil
|
21 |
+
from dotenv import load_dotenv
|
22 |
|
23 |
+
# Load environment variables
|
24 |
+
load_dotenv()
|
25 |
|
26 |
+
class FaceIntegrDataset:
|
27 |
+
def __init__(self, repo_id="Arrcttacsrks/face_integrData"):
|
28 |
+
# Get token from environment variable
|
29 |
+
self.token = os.getenv('hf_token')
|
30 |
+
if not self.token:
|
31 |
+
raise ValueError("HF_TOKEN environment variable is not set")
|
32 |
+
|
33 |
+
self.repo_id = repo_id
|
34 |
+
self.api = HfApi()
|
35 |
+
|
36 |
+
# Login to Hugging Face
|
37 |
+
login(self.token)
|
38 |
+
|
39 |
+
# Create local temp directory for organizing files
|
40 |
+
self.temp_dir = "temp_dataset"
|
41 |
+
os.makedirs(self.temp_dir, exist_ok=True)
|
42 |
|
43 |
+
def create_date_folder(self):
|
44 |
+
"""Create folder structure based on current date"""
|
45 |
+
current_date = datetime.now().strftime("%Y-%m-%d")
|
46 |
+
folder_path = os.path.join(self.temp_dir, current_date)
|
47 |
+
os.makedirs(folder_path, exist_ok=True)
|
48 |
+
return folder_path, current_date
|
49 |
|
50 |
+
def save_metadata(self, source_path, target_path, output_path, timestamp):
|
51 |
+
"""Save metadata for the face swap operation"""
|
52 |
+
metadata = {
|
53 |
+
"timestamp": timestamp,
|
54 |
+
"source_image": source_path,
|
55 |
+
"target_image": target_path,
|
56 |
+
"output_image": output_path,
|
57 |
+
"date_created": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
58 |
+
}
|
59 |
+
return metadata
|
60 |
|
61 |
+
def upload_to_hf(self, local_folder, date_folder):
|
62 |
+
"""Upload files to Hugging Face dataset"""
|
63 |
+
try:
|
64 |
+
# Upload the files
|
65 |
+
self.api.upload_folder(
|
66 |
+
folder_path=local_folder,
|
67 |
+
repo_id=self.repo_id,
|
68 |
+
repo_type="dataset",
|
69 |
+
path_in_repo=date_folder
|
70 |
+
)
|
71 |
+
return True
|
72 |
+
except Exception as e:
|
73 |
+
print(f"Error uploading to Hugging Face: {str(e)}")
|
74 |
+
return False
|
75 |
|
76 |
+
def swap_face(source_file, target_file, doFaceEnhancer):
|
77 |
+
try:
|
78 |
+
# Initialize dataset handler
|
79 |
+
dataset_handler = FaceIntegrDataset()
|
80 |
+
|
81 |
+
# Create date-based folder
|
82 |
+
folder_path, date_folder = dataset_handler.create_date_folder()
|
83 |
+
|
84 |
+
# Generate timestamp for unique identification
|
85 |
+
timestamp = datetime.now().strftime("%S%M%H%d%m%Y")
|
86 |
+
|
87 |
+
# Save input images with timestamp in folder
|
88 |
+
source_path = os.path.join(folder_path, f"source_{timestamp}.jpg")
|
89 |
+
target_path = os.path.join(folder_path, f"target_{timestamp}.jpg")
|
90 |
+
output_path = os.path.join(folder_path, f"Image{timestamp}.jpg")
|
91 |
+
|
92 |
+
# Save the input images
|
93 |
+
source_image = Image.fromarray(source_file)
|
94 |
+
source_image.save(source_path)
|
95 |
+
target_image = Image.fromarray(target_file)
|
96 |
+
target_image.save(target_path)
|
97 |
+
|
98 |
+
print("source_path: ", source_path)
|
99 |
+
print("target_path: ", target_path)
|
100 |
+
|
101 |
+
# Set global paths
|
102 |
+
roop.globals.source_path = source_path
|
103 |
+
roop.globals.target_path = target_path
|
104 |
+
roop.globals.output_path = normalize_output_path(
|
105 |
+
roop.globals.source_path,
|
106 |
+
roop.globals.target_path,
|
107 |
+
output_path
|
108 |
+
)
|
109 |
+
|
110 |
+
# Configure face processing options
|
111 |
+
if doFaceEnhancer:
|
112 |
+
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
113 |
+
else:
|
114 |
+
roop.globals.frame_processors = ["face_swapper"]
|
115 |
+
|
116 |
+
# Set global parameters
|
117 |
+
roop.globals.headless = True
|
118 |
+
roop.globals.keep_fps = True
|
119 |
+
roop.globals.keep_audio = True
|
120 |
+
roop.globals.keep_frames = False
|
121 |
+
roop.globals.many_faces = False
|
122 |
+
roop.globals.video_encoder = "libx264"
|
123 |
+
roop.globals.video_quality = 18
|
124 |
+
roop.globals.max_memory = suggest_max_memory()
|
125 |
+
roop.globals.execution_providers = decode_execution_providers(["cuda"])
|
126 |
+
roop.globals.execution_threads = suggest_execution_threads()
|
127 |
+
|
128 |
+
print(
|
129 |
+
"start process",
|
130 |
+
roop.globals.source_path,
|
131 |
+
roop.globals.target_path,
|
132 |
+
roop.globals.output_path,
|
133 |
+
)
|
134 |
+
|
135 |
+
# Check frame processors
|
136 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
137 |
+
if not frame_processor.pre_check():
|
138 |
+
return
|
139 |
+
|
140 |
+
# Process the face swap
|
141 |
+
start()
|
142 |
+
|
143 |
+
# Save metadata
|
144 |
+
metadata = dataset_handler.save_metadata(
|
145 |
+
f"source_{timestamp}.jpg",
|
146 |
+
f"target_{timestamp}.jpg",
|
147 |
+
f"Image{timestamp}.jpg",
|
148 |
+
timestamp
|
149 |
+
)
|
150 |
+
|
151 |
+
# Save metadata to JSON file in the same folder
|
152 |
+
metadata_path = os.path.join(folder_path, f"metadata_{timestamp}.json")
|
153 |
+
with open(metadata_path, 'w') as f:
|
154 |
+
json.dump(metadata, f, indent=4)
|
155 |
+
|
156 |
+
# Upload to Hugging Face
|
157 |
+
upload_success = dataset_handler.upload_to_hf(folder_path, date_folder)
|
158 |
+
|
159 |
+
if upload_success:
|
160 |
+
print(f"Successfully uploaded files to dataset {dataset_handler.repo_id}")
|
161 |
+
else:
|
162 |
+
print("Failed to upload files to Hugging Face dataset")
|
163 |
+
|
164 |
+
# Clean up temp folder after upload
|
165 |
+
shutil.rmtree(folder_path)
|
166 |
+
|
167 |
+
return output_path
|
168 |
+
|
169 |
+
except Exception as e:
|
170 |
+
print(f"Error in face swap process: {str(e)}")
|
171 |
+
raise gr.Error("Face swap failed. Please check your Hugging Face token and try again.")
|
172 |
|
173 |
+
# Gradio interface setup
|
174 |
title = "Face - Интегратор"
|
|
|
175 |
description = r"""
|
176 |
+
The application will save the image history to Hugging Face dataset using the environment variable token.
|
177 |
"""
|
178 |
article = r"""
|
179 |
<br><br><br><br><br>
|
180 |
"""
|
181 |
|
|
|
182 |
app = gr.Interface(
|
183 |
fn=swap_face,
|
184 |
title=title,
|
185 |
description=description,
|
186 |
article=article,
|
187 |
+
inputs=[
|
188 |
+
gr.Image(),
|
189 |
+
gr.Image(),
|
190 |
+
gr.Checkbox(label="Применить алгоритм?", info="Улучшение качества изображения")
|
191 |
+
],
|
192 |
+
outputs="image"
|
193 |
)
|
194 |
+
|
195 |
+
app.launch()
|