Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,52 @@ import random
|
|
7 |
import os
|
8 |
import base64
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
client = Prodia()
|
11 |
|
12 |
|
@@ -17,12 +63,13 @@ def infer(source, target):
|
|
17 |
source_url = upload_image(source)
|
18 |
target_url = upload_image(target)
|
19 |
|
20 |
-
job = client.faceswap(
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
-
return res
|
26 |
|
27 |
|
28 |
def upload_image(file):
|
|
|
7 |
import os
|
8 |
import base64
|
9 |
|
10 |
+
|
11 |
+
class Prodia:
|
12 |
+
def __init__(self, api_key=os.getenv("PRODIA_API_KEY"), base=None):
|
13 |
+
self.base = base or "https://api.prodia.com/v1"
|
14 |
+
self.headers = {
|
15 |
+
"X-Prodia-Key": api_key
|
16 |
+
}
|
17 |
+
|
18 |
+
def faceswap(self, params):
|
19 |
+
response = self._post(f"{self.base}/faceswap", params)
|
20 |
+
return response.json()
|
21 |
+
|
22 |
+
def get_job(self, job_id):
|
23 |
+
response = self._get(f"{self.base}/job/{job_id}")
|
24 |
+
return response.json()
|
25 |
+
|
26 |
+
def wait(self, job):
|
27 |
+
job_result = job
|
28 |
+
|
29 |
+
while job_result['status'] not in ['succeeded', 'failed']:
|
30 |
+
time.sleep(0.25)
|
31 |
+
job_result = self.get_job(job['job'])
|
32 |
+
|
33 |
+
return job_result
|
34 |
+
|
35 |
+
def _post(self, url, params):
|
36 |
+
headers = {
|
37 |
+
**self.headers,
|
38 |
+
"Content-Type": "application/json"
|
39 |
+
}
|
40 |
+
response = requests.post(url, headers=headers, data=json.dumps(params))
|
41 |
+
|
42 |
+
if response.status_code != 200:
|
43 |
+
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
44 |
+
|
45 |
+
return response
|
46 |
+
|
47 |
+
def _get(self, url):
|
48 |
+
response = requests.get(url, headers=self.headers)
|
49 |
+
|
50 |
+
if response.status_code != 200:
|
51 |
+
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
52 |
+
|
53 |
+
return response
|
54 |
+
|
55 |
+
|
56 |
client = Prodia()
|
57 |
|
58 |
|
|
|
63 |
source_url = upload_image(source)
|
64 |
target_url = upload_image(target)
|
65 |
|
66 |
+
job = client.faceswap({
|
67 |
+
"sourceUrl": source_url,
|
68 |
+
"targetUrl": target_url
|
69 |
+
})
|
70 |
+
res = client.wait(job)
|
71 |
|
72 |
+
return res['imageUrl']
|
73 |
|
74 |
|
75 |
def upload_image(file):
|