Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,118 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from prodiapy import Prodia
|
3 |
-
from PIL import Image
|
4 |
-
from io import BytesIO
|
5 |
-
import requests
|
6 |
-
import random
|
7 |
-
import os
|
8 |
-
import base64
|
9 |
-
import json
|
10 |
-
import time
|
11 |
-
|
12 |
-
|
13 |
-
class Prodia:
|
14 |
-
def __init__(self, api_key=os.getenv("PRODIA_API_KEY"), base=None):
|
15 |
-
self.base = base or "https://api.prodia.com/v1"
|
16 |
-
self.headers = {
|
17 |
-
"X-Prodia-Key": api_key
|
18 |
-
}
|
19 |
-
|
20 |
-
def faceswap(self, params):
|
21 |
-
response = self._post(f"{self.base}/faceswap", params)
|
22 |
-
return response.json()
|
23 |
-
|
24 |
-
def get_job(self, job_id):
|
25 |
-
response = self._get(f"{self.base}/job/{job_id}")
|
26 |
-
return response.json()
|
27 |
-
|
28 |
-
def wait(self, job):
|
29 |
-
job_result = job
|
30 |
-
|
31 |
-
while job_result['status'] not in ['succeeded', 'failed']:
|
32 |
-
time.sleep(0.25)
|
33 |
-
job_result = self.get_job(job['job'])
|
34 |
-
|
35 |
-
return job_result
|
36 |
-
|
37 |
-
def _post(self, url, params):
|
38 |
-
headers = {
|
39 |
-
**self.headers,
|
40 |
-
"Content-Type": "application/json"
|
41 |
-
}
|
42 |
-
response = requests.post(url, headers=headers, data=json.dumps(params))
|
43 |
-
|
44 |
-
if response.status_code != 200:
|
45 |
-
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
46 |
-
|
47 |
-
return response
|
48 |
-
|
49 |
-
def _get(self, url):
|
50 |
-
response = requests.get(url, headers=self.headers)
|
51 |
-
|
52 |
-
if response.status_code != 200:
|
53 |
-
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
54 |
-
|
55 |
-
return response
|
56 |
-
|
57 |
-
|
58 |
-
client = Prodia()
|
59 |
-
|
60 |
-
|
61 |
-
def infer(source, target):
|
62 |
-
if source_image is None or target_image is None:
|
63 |
-
return
|
64 |
-
|
65 |
-
source_url = upload_image(source)
|
66 |
-
target_url = upload_image(target)
|
67 |
-
|
68 |
-
job = client.faceswap({
|
69 |
-
"sourceUrl": source_url,
|
70 |
-
"targetUrl": target_url
|
71 |
-
})
|
72 |
-
res = client.wait(job)
|
73 |
-
|
74 |
-
if res['status'] == "failed":
|
75 |
-
return
|
76 |
-
|
77 |
-
return res['imageUrl']
|
78 |
-
|
79 |
-
|
80 |
-
def upload_image(file):
|
81 |
-
files = {'file': open(file, 'rb')}
|
82 |
-
img_id = requests.post(os.getenv("IMAGE_API_1"), files=files).json()['id']
|
83 |
-
|
84 |
-
payload = {
|
85 |
-
"content": "",
|
86 |
-
"nonce": f"{random.randint(1, 10000000)}H9X42KSEJFNNH",
|
87 |
-
"replies": [],
|
88 |
-
"attachments": [img_id]
|
89 |
-
}
|
90 |
-
res = requests.post(os.getenv("IMAGE_API_2"), json=payload, headers={"x-session-token": os.getenv("SESSION_TOKEN")})
|
91 |
-
|
92 |
-
return f"{os.getenv('IMAGE_API_1')}/{img_id}/{res.json()['attachments'][0]['filename']}"
|
93 |
-
|
94 |
-
|
95 |
-
def image_to_base64(image: Image):
|
96 |
-
buffered = BytesIO()
|
97 |
-
image.save(buffered, format="PNG")
|
98 |
-
img_str = base64.b64encode(buffered.getvalue())
|
99 |
-
return img_str.decode('utf-8')
|
100 |
-
|
101 |
-
|
102 |
-
with gr.Blocks(theme="NoCrypt/miku") as demo:
|
103 |
-
gr.Markdown("### Sorry for the inconvenience. The model is currently running on the CPU, which might affect performance. We appreciate your understanding.")
|
104 |
-
|
105 |
-
with gr.Column():
|
106 |
-
gr.HTML("<h1><center>Face Swap</center></h1>")
|
107 |
-
|
108 |
-
with gr.Row():
|
109 |
-
with gr.Row():
|
110 |
-
source_image = gr.Image(type="filepath", label="Source Image")
|
111 |
-
target_image = gr.Image(type="filepath", label="Target Image")
|
112 |
-
with gr.Column():
|
113 |
-
result = gr.Image()
|
114 |
-
run_button = gr.Button("Swap Face", variant="primary")
|
115 |
-
|
116 |
-
run_button.click(fn=infer, inputs=[source_image, target_image], outputs=[result])
|
117 |
-
|
118 |
-
demo.queue(max_size=20, api_open=False).launch(show_api=False, max_threads=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|