zenafey commited on
Commit
63323a0
1 Parent(s): 274b2e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -14
app.py CHANGED
@@ -2,30 +2,83 @@ import os
2
  import numpy as np
3
  from prodiapy import Prodia
4
  import gradio as gr
 
 
5
  import base64
 
6
 
7
- prodia = Prodia()
8
  STYLE_PRESETS = [None, "3d-model", "analog-film", "anime", "cinematic", "comic-book", "digital-art", "enhance", "fantasy-art", "isometric", "line-art", "low-poly",
9
  "neon-punk", "origami", "photographic", "pixel-art", "texture", "craft-clay"]
10
  MAX_SEED = np.iinfo(np.int32).max
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def generate_image(upload_images, prompt, negative_prompt, style_preset, steps, cfg_scale, strength, seed, progress=gr.Progress(track_tqdm=True)):
14
  error_if_no_img(prompt)
15
 
16
- job = prodia.create("/photomaker",
17
- imageData=[file_to_base64(img) for img in upload_images],
18
- prompt=prompt,
19
- negative_prompt=negative_prompt,
20
- style_preset=style_preset,
21
- steps=steps,
22
- cfg_scale=cfg_scale,
23
- strength=strength,
24
- seed=seed if seed != 0 else None
25
- )
26
- result = prodia.wait(job)
27
-
28
- return result.image_url
 
 
 
29
 
30
 
31
  def error_if_no_img(prompt):
 
2
  import numpy as np
3
  from prodiapy import Prodia
4
  import gradio as gr
5
+ import json
6
+ import requests
7
  import base64
8
+ import random
9
 
 
10
  STYLE_PRESETS = [None, "3d-model", "analog-film", "anime", "cinematic", "comic-book", "digital-art", "enhance", "fantasy-art", "isometric", "line-art", "low-poly",
11
  "neon-punk", "origami", "photographic", "pixel-art", "texture", "craft-clay"]
12
  MAX_SEED = np.iinfo(np.int32).max
13
 
14
 
15
+ class Prodia:
16
+ def __init__(self, api_key=os.getenv("PRODIA_API_KEY"), base=None):
17
+ self.base = base or "https://api.prodia.com/v1"
18
+ self.headers = {
19
+ "X-Prodia-Key": api_key
20
+ }
21
+
22
+ def photomaker(self, params):
23
+ response = self._post(f"{self.base}/faceswap", params)
24
+ return response.json()
25
+
26
+ def get_job(self, job_id):
27
+ response = self._get(f"{self.base}/job/{job_id}")
28
+ return response.json()
29
+
30
+ def wait(self, job):
31
+ job_result = job
32
+
33
+ while job_result['status'] not in ['succeeded', 'failed']:
34
+ time.sleep(0.25)
35
+ job_result = self.get_job(job['job'])
36
+
37
+ return job_result
38
+
39
+ def _post(self, url, params):
40
+ headers = {
41
+ **self.headers,
42
+ "Content-Type": "application/json"
43
+ }
44
+ response = requests.post(url, headers=headers, data=json.dumps(params))
45
+
46
+ if response.status_code != 200:
47
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
48
+
49
+ return response
50
+
51
+ def _get(self, url):
52
+ response = requests.get(url, headers=self.headers)
53
+
54
+ if response.status_code != 200:
55
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
56
+
57
+ return response
58
+
59
+
60
+ client = Prodia()
61
+
62
+
63
  def generate_image(upload_images, prompt, negative_prompt, style_preset, steps, cfg_scale, strength, seed, progress=gr.Progress(track_tqdm=True)):
64
  error_if_no_img(prompt)
65
 
66
+ job = client.photomaker({
67
+ "imageData": [file_to_base64(img) for img in upload_images],
68
+ "prompt": prompt,
69
+ "negative_prompt": negative_prompt,
70
+ "style_preset": style_preset,
71
+ "steps": steps,
72
+ "cfg_scale": cfg_scale,
73
+ "strength": strength,
74
+ "seed": seed if seed != 0 else random.randint(1, MAX_SEED)
75
+ })
76
+ res = prodia.wait(job)
77
+
78
+ if res['status'] == "failed":
79
+ return
80
+
81
+ return res['imageUrl']
82
 
83
 
84
  def error_if_no_img(prompt):