File size: 1,788 Bytes
3266b60
2520dea
 
0ffaf55
df949d3
c1942c1
fa3cda5
2520dea
 
 
 
fa3cda5
 
 
2520dea
c1942c1
2520dea
55a5166
 
22a4581
c1942c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3412fa4
0ffaf55
22a4581
c1942c1
 
2520dea
fa3cda5
3412fa4
c1942c1
3412fa4
 
0ffaf55
fa3cda5
 
 
22a4581
 
fa3cda5
 
22a4581
fa3cda5
3266b60
 
fa3cda5
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
import gradio as gr
import os
import random
from PIL import Image
import base64
import requests

KEYS = os.getenv("KEYS").split(",")

def get_random_api_key():
    return random.choice(KEYS)

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/"  # URL вашего API

        source_b64 = gr.encode_pil_to_base64(source_img)
        target_b64 = gr.encode_pil_to_base64(target_img)

        payload = {
            "data": [
                source_b64,
                target_b64,
                doFaceEnhancer
            ],
            "api_key": api_key  # Передаем api_key в payload
        }

        response = requests.post(api_url, json=payload)
        response.raise_for_status()  # Вызывает исключение, если есть ошибка

        # Получаем результат из ответа
        result = response.json()
        result_data = result["data"]  # Извлекаем данные из ответа
        result_decoded = base64.b64decode(result_data[0]) # Декодируем base64


        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="pil", label="Source Image"),
        gr.Image(type="pil", label="Target Image"),
        gr.Checkbox(label="Face Enhancer?")
    ],
    outputs=gr.Image(type="pil", label="Output Image"),
    title="Face Swap via API"
)

iface.launch()