File size: 2,260 Bytes
e7f74df
 
 
 
 
 
 
 
 
 
 
 
 
408deaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7f74df
408deaa
 
e7f74df
408deaa
 
 
e7f74df
408deaa
 
58dcef7
e7f74df
408deaa
 
 
 
 
 
 
e7f74df
 
 
5cf9b17
 
 
 
 
 
 
 
 
 
 
 
 
 
3b25fb8
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
import gradio as gr
import io
import requests
import os
import base64
import cv2
from PIL import Image
import json
import numpy as np
import rsa

def FaceSwap(source_img, dest_img):

    secret = os.environ['API_KEY']
    privateKey = os.environ["PRIVATE_KEY"]
    publicKey = os.environ["PUBLIC_KEY"]
    baseurl = os.environ["BASE_URL"]

    n,e,d,p,q = privateKey.split(",")
    n1,e1 = publicKey.split(",")
    privateKey = rsa.PrivateKey(int(n),int(e),int(d),int(p),int(q))
    publicKey = rsa.PublicKey(int(n1), int(e1))

    encMessage = rsa.encrypt(secret.encode('utf-8'), publicKey)
    encMessage = base64.b64encode(encMessage).decode("utf-8")
    
    src_size = source_img.size
    dest_size = dest_img.size
    
    src_base64 = base64.b64encode(bytearray(source_img.tobytes())).decode('utf-8')
    dest_base64 = base64.b64encode(bytearray(dest_img.tobytes())).decode('utf-8')

    #Sending request to a FaceSwap API I deployed it on AWS ec2. 
    #Contact maytamaku.saidhwar@gmail.com to get access to the API.
    api_url = f"{baseurl}/api/faceswap"
    
    payload = {"source_img": str(src_base64), "dest_img": str(dest_base64), "source_size": src_size, "dest_size": dest_size}
    headers = {'Content-Type': 'multipart/form-data', "Authorization": encMessage}
    res = requests.post(api_url, json=payload, headers=headers)

    if (res.status_code >= 200 and res.status_code <= 299):
        res = np.array(res.json())
        return res
    elif (res.status_code >= 400 and res.status_code <= 499):
        raise gr.Error(f"Code {res.status_code}: Erron on client")
    else:
        raise gr.Error(f"Code {res.status_code}: Erron on server. Try after some time")
    
if __name__ == "__main__":
    with gr.Blocks(title="Face Swap | Meet With Your new Personality") as demo:
        gr.Interface(
            fn=FaceSwap,
            inputs=[
                gr.Image(type="pil"),
                gr.Image(type="pil")
            ],
            outputs=[
                gr.Image(
                    type="pil",
                    show_download_button=False,
                    show_share_button=False)
            ],
            allow_flagging="never"
            )
    demo.queue().launch(show_api=False, favicon_path="./favicon.ico")