Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import random
|
4 |
from PIL import Image
|
|
|
5 |
import base64
|
6 |
import requests
|
7 |
|
@@ -13,7 +14,8 @@ def get_random_api_key():
|
|
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/"
|
|
|
17 |
|
18 |
source_b64 = gr.encode_pil_to_base64(source_img)
|
19 |
target_b64 = gr.encode_pil_to_base64(target_img)
|
@@ -24,30 +26,26 @@ def swap_face_api(source_img, target_img, doFaceEnhancer):
|
|
24 |
target_b64,
|
25 |
doFaceEnhancer
|
26 |
],
|
27 |
-
"api_key": api_key
|
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
|
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 |
-
|
51 |
iface = gr.Interface(
|
52 |
fn=swap_face_api,
|
53 |
inputs=[
|
@@ -59,4 +57,5 @@ iface = gr.Interface(
|
|
59 |
title="Face Swap via API"
|
60 |
)
|
61 |
|
62 |
-
iface.launch()
|
|
|
|
2 |
import os
|
3 |
import random
|
4 |
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
import base64
|
7 |
import requests
|
8 |
|
|
|
14 |
def swap_face_api(source_img, target_img, doFaceEnhancer):
|
15 |
try:
|
16 |
api_key = get_random_api_key()
|
17 |
+
api_url = "https://tuan2308-face-swap-predict.hf.space/api/predict/"
|
18 |
+
|
19 |
|
20 |
source_b64 = gr.encode_pil_to_base64(source_img)
|
21 |
target_b64 = gr.encode_pil_to_base64(target_img)
|
|
|
26 |
target_b64,
|
27 |
doFaceEnhancer
|
28 |
],
|
29 |
+
"api_key": api_key
|
30 |
}
|
31 |
|
32 |
response = requests.post(api_url, json=payload)
|
33 |
+
response.raise_for_status()
|
34 |
|
|
|
35 |
result = response.json()
|
36 |
+
result_data = result["data"][0] # Убедитесь, что индекс корректный
|
37 |
+
result_decoded = base64.b64decode(result_data)
|
|
|
|
|
38 |
output_image = Image.open(BytesIO(result_decoded))
|
39 |
+
|
40 |
return output_image
|
41 |
|
42 |
except requests.exceptions.RequestException as e:
|
43 |
print(f"Ошибка API: {e}")
|
44 |
return None
|
|
|
45 |
except Exception as e:
|
46 |
print(f"Ошибка: {e}")
|
47 |
return None
|
48 |
|
|
|
49 |
iface = gr.Interface(
|
50 |
fn=swap_face_api,
|
51 |
inputs=[
|
|
|
57 |
title="Face Swap via API"
|
58 |
)
|
59 |
|
60 |
+
iface.launch(server_name="0.0.0.0", server_port=7860) # Запускаем на всех интерфейсах
|
61 |
+
|