zenafey commited on
Commit
326cf1a
·
verified ·
1 Parent(s): c0e5ce3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -3,18 +3,39 @@ from prodiapy import Prodia
3
  from PIL import Image
4
  from io import BytesIO
5
  import requests
 
 
6
  import base64
7
 
8
  client = Prodia()
9
 
10
 
11
- def run_face_swap(source_image, target_image):
12
  if source_image is None or target_image is None:
13
  return
14
 
15
- print(source_image, target_image)
 
 
 
 
16
 
17
- return "https://images.prodia.xyz/acfe8b01-b706-46d9-8c2d-287466337609.png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  def image_to_base64(image: Image):
@@ -34,13 +55,13 @@ with gr.Blocks() as demo:
34
  gr.DuplicateButton(size="lg")
35
 
36
  with gr.Row():
37
- with gr.Column():
38
  source_image = gr.Image(type="filepath", label="Source Image")
39
  target_image = gr.Image(type="filepath", label="Target Image")
40
  with gr.Column():
41
- run_button = gr.Button("Swap Faces", variant="primary")
42
  result = gr.Image()
 
43
 
44
  run_button.click(fn=run_face_swap, inputs=[source_image, target_image], outputs=result)
45
 
46
- demo.queue(max_size=50).launch(show_api=False)
 
3
  from PIL import Image
4
  from io import BytesIO
5
  import requests
6
+ import random
7
+ import os
8
  import base64
9
 
10
  client = Prodia()
11
 
12
 
13
+ def run_face_swap(source, target):
14
  if source_image is None or target_image is None:
15
  return
16
 
17
+ source_url = upload_image(source)
18
+ target_url = upload_image(target)
19
+
20
+ job = client.faceswap(source_url=source_url, target_url=target_url)
21
+ res = client.wait(job)
22
 
23
+ return res.image_url
24
+
25
+
26
+ def upload_image(file):
27
+ files = {'file': open(file, 'rb')}
28
+ img_id = requests.post(os.getenv("image-api-1"), files=files).json()['id']
29
+
30
+ payload = {
31
+ "content": "",
32
+ "nonce": f"{random.randint(1, 10000000)}H9X42KSEJFNNH",
33
+ "replies": [],
34
+ "attachments": [img_id]
35
+ }
36
+ res = requests.post(os.getenv("image-api-2"), json=payload, headers={"x-session-token": os.getenv("session-token")})
37
+
38
+ return f"{os.getenv('image-api-1')}/{img_id}/{res.json()['attachments'][0]['filename']}"
39
 
40
 
41
  def image_to_base64(image: Image):
 
55
  gr.DuplicateButton(size="lg")
56
 
57
  with gr.Row():
58
+ with gr.Row():
59
  source_image = gr.Image(type="filepath", label="Source Image")
60
  target_image = gr.Image(type="filepath", label="Target Image")
61
  with gr.Column():
 
62
  result = gr.Image()
63
+ run_button = gr.Button("Swap Faces", variant="primary")
64
 
65
  run_button.click(fn=run_face_swap, inputs=[source_image, target_image], outputs=result)
66
 
67
+ demo.queue(max_size=20, api_open=False).launch(max_threads=400)