Update app.py
Browse files
app.py
CHANGED
|
@@ -5,20 +5,26 @@ from PIL import Image
|
|
| 5 |
from io import BytesIO
|
| 6 |
import base64
|
| 7 |
import requests
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def get_random_api_key():
|
| 12 |
-
return random.choice(KEYS)
|
| 13 |
|
| 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 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
payload = {
|
| 24 |
"data": [
|
|
@@ -32,8 +38,10 @@ def swap_face_api(source_img, target_img, doFaceEnhancer):
|
|
| 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 |
|
|
@@ -46,16 +54,17 @@ def swap_face_api(source_img, target_img, doFaceEnhancer):
|
|
| 46 |
print(f"Ошибка: {e}")
|
| 47 |
return None
|
| 48 |
|
|
|
|
| 49 |
iface = gr.Interface(
|
| 50 |
fn=swap_face_api,
|
| 51 |
inputs=[
|
| 52 |
-
gr.Image(type="
|
| 53 |
-
gr.Image(type="
|
| 54 |
gr.Checkbox(label="Face Enhancer?")
|
| 55 |
],
|
| 56 |
-
outputs=gr.Image(type="pil", label="Output Image"),
|
| 57 |
title="Face Swap via API"
|
| 58 |
)
|
| 59 |
|
| 60 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 61 |
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
import base64
|
| 7 |
import requests
|
| 8 |
+
import numpy as np
|
| 9 |
|
| 10 |
+
# ... (остальной код без изменений)
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def swap_face_api(source_img, target_img, doFaceEnhancer):
|
| 13 |
try:
|
| 14 |
api_key = get_random_api_key()
|
| 15 |
api_url = "https://tuan2308-face-swap-predict.hf.space/api/predict/"
|
| 16 |
|
| 17 |
+
# Преобразовазуем numpy массивы в PIL изображения, затем в байты и base64
|
| 18 |
+
source_pil = Image.fromarray(source_img)
|
| 19 |
+
source_bytes = BytesIO()
|
| 20 |
+
source_pil.save(source_bytes, format="JPEG")
|
| 21 |
+
source_b64 = base64.b64encode(source_bytes.getvalue()).decode("utf-8")
|
| 22 |
+
|
| 23 |
|
| 24 |
+
target_pil = Image.fromarray(target_img)
|
| 25 |
+
target_bytes = BytesIO()
|
| 26 |
+
target_pil.save(target_bytes, format="JPEG")
|
| 27 |
+
target_b64 = base64.b64encode(target_bytes.getvalue()).decode("utf-8")
|
| 28 |
|
| 29 |
payload = {
|
| 30 |
"data": [
|
|
|
|
| 38 |
response = requests.post(api_url, json=payload)
|
| 39 |
response.raise_for_status()
|
| 40 |
|
| 41 |
+
print(response)
|
| 42 |
+
|
| 43 |
result = response.json()
|
| 44 |
+
result_data = result["data"][0] # Проверьте индекс
|
| 45 |
result_decoded = base64.b64decode(result_data)
|
| 46 |
output_image = Image.open(BytesIO(result_decoded))
|
| 47 |
|
|
|
|
| 54 |
print(f"Ошибка: {e}")
|
| 55 |
return None
|
| 56 |
|
| 57 |
+
|
| 58 |
iface = gr.Interface(
|
| 59 |
fn=swap_face_api,
|
| 60 |
inputs=[
|
| 61 |
+
gr.Image(type="numpy", label="Source Image"), # Изменено на numpy
|
| 62 |
+
gr.Image(type="numpy", label="Target Image"), # Изменено на numpy
|
| 63 |
gr.Checkbox(label="Face Enhancer?")
|
| 64 |
],
|
| 65 |
+
outputs=gr.Image(type="pil", label="Output Image"), # Остается pil
|
| 66 |
title="Face Swap via API"
|
| 67 |
)
|
| 68 |
|
| 69 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 70 |
|