File size: 2,151 Bytes
3266b60 2520dea 0ffaf55 1865d84 df949d3 c1942c1 45f4da3 fa3cda5 45f4da3 fa3cda5 2520dea 1865d84 45f4da3 2520dea 45f4da3 22a4581 c1942c1 1865d84 c1942c1 1865d84 c1942c1 45f4da3 c1942c1 45f4da3 1865d84 3412fa4 1865d84 0ffaf55 22a4581 c1942c1 2520dea 3412fa4 c1942c1 3412fa4 45f4da3 fa3cda5 45f4da3 fa3cda5 45f4da3 fa3cda5 3266b60 45f4da3 1865d84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 69 70 71 |
import gradio as gr
import os
import random
from PIL import Image
from io import BytesIO
import base64
import requests
import numpy as np
# ... (остальной код без изменений)
def swap_face_api(source_img, target_img, doFaceEnhancer):
try:
api_key = get_random_api_key()
api_url = "https://tuan2308-face-swap-predict.hf.space/api/predict/"
# Преобразовазуем numpy массивы в PIL изображения, затем в байты и base64
source_pil = Image.fromarray(source_img)
source_bytes = BytesIO()
source_pil.save(source_bytes, format="JPEG")
source_b64 = base64.b64encode(source_bytes.getvalue()).decode("utf-8")
target_pil = Image.fromarray(target_img)
target_bytes = BytesIO()
target_pil.save(target_bytes, format="JPEG")
target_b64 = base64.b64encode(target_bytes.getvalue()).decode("utf-8")
payload = {
"data": [
source_b64,
target_b64,
doFaceEnhancer
],
"api_key": api_key
}
response = requests.post(api_url, json=payload)
response.raise_for_status()
print(response)
result = response.json()
result_data = result["data"][0] # Проверьте индекс
result_decoded = base64.b64decode(result_data)
output_image = Image.open(BytesIO(result_decoded))
return output_image
except requests.exceptions.RequestException as e:
print(f"Ошибка API: {e}")
return None
except Exception as e:
print(f"Ошибка: {e}")
return None
iface = gr.Interface(
fn=swap_face_api,
inputs=[
gr.Image(type="numpy", label="Source Image"), # Изменено на numpy
gr.Image(type="numpy", label="Target Image"), # Изменено на numpy
gr.Checkbox(label="Face Enhancer?")
],
outputs=gr.Image(type="pil", label="Output Image"), # Остается pil
title="Face Swap via API"
)
iface.launch(server_name="0.0.0.0", server_port=7860)
|