Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
from gradio_client import Client
|
3 |
import os
|
4 |
import random
|
5 |
-
from io import BytesIO
|
6 |
from PIL import Image
|
7 |
import base64
|
|
|
8 |
|
9 |
KEYS = os.getenv("KEYS").split(",")
|
10 |
|
@@ -14,32 +13,38 @@ def get_random_api_key():
|
|
14 |
def swap_face_api(source_img, target_img, doFaceEnhancer):
|
15 |
try:
|
16 |
api_key = get_random_api_key()
|
17 |
-
|
18 |
|
19 |
-
# Кодируем PIL изображения в base64, используя gr.processing_utils
|
20 |
source_b64 = gr.encode_pil_to_base64(source_img)
|
21 |
target_b64 = gr.encode_pil_to_base64(target_img)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
output_image = Image.open(BytesIO(result_decoded))
|
35 |
return output_image
|
36 |
|
37 |
-
except
|
38 |
-
print(f"Ошибка
|
39 |
return None
|
40 |
|
41 |
except Exception as e:
|
42 |
-
print(f"
|
43 |
return None
|
44 |
|
45 |
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
import random
|
|
|
4 |
from PIL import Image
|
5 |
import base64
|
6 |
+
import requests
|
7 |
|
8 |
KEYS = os.getenv("KEYS").split(",")
|
9 |
|
|
|
13 |
def swap_face_api(source_img, target_img, doFaceEnhancer):
|
14 |
try:
|
15 |
api_key = get_random_api_key()
|
16 |
+
api_url = "https://tuan2308-face-swap-predict.hf.space/api/predict/" # URL вашего API
|
17 |
|
|
|
18 |
source_b64 = gr.encode_pil_to_base64(source_img)
|
19 |
target_b64 = gr.encode_pil_to_base64(target_img)
|
20 |
|
21 |
+
payload = {
|
22 |
+
"data": [
|
23 |
+
source_b64,
|
24 |
+
target_b64,
|
25 |
+
doFaceEnhancer
|
26 |
+
],
|
27 |
+
"api_key": api_key # Передаем api_key в payload
|
28 |
+
}
|
29 |
+
|
30 |
+
response = requests.post(api_url, json=payload)
|
31 |
+
response.raise_for_status() # Вызывает исключение, если есть ошибка
|
32 |
+
|
33 |
+
# Получаем результат из ответа
|
34 |
+
result = response.json()
|
35 |
+
result_data = result["data"] # Извлекаем данные из ответа
|
36 |
+
result_decoded = base64.b64decode(result_data[0]) # Декодируем base64
|
37 |
+
|
38 |
+
|
39 |
output_image = Image.open(BytesIO(result_decoded))
|
40 |
return output_image
|
41 |
|
42 |
+
except requests.exceptions.RequestException as e:
|
43 |
+
print(f"Ошибка API: {e}")
|
44 |
return None
|
45 |
|
46 |
except Exception as e:
|
47 |
+
print(f"Ошибка: {e}")
|
48 |
return None
|
49 |
|
50 |
|