Rooni commited on
Commit
c6da11a
·
verified ·
1 Parent(s): 4377b05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import json
2
  import time
 
3
  import requests
4
  import base64
5
  from io import BytesIO
@@ -22,6 +23,8 @@ styles = {
22
  "Кандинский": "KANDINSKY"
23
  }
24
 
 
 
25
  api_key = os.getenv("api_key")
26
  secret_key = os.getenv("secret_key")
27
 
@@ -39,10 +42,10 @@ class Text2ImageAPI:
39
  data = response.json()
40
  return data[0]['id']
41
 
42
- def generate(self, prompt, negative, style, width, height, model, num_images):
43
  params = {
44
  "type": "GENERATE",
45
- "numImages": num_images,
46
  "style": f"{style}",
47
  "width": width,
48
  "height": height,
@@ -57,7 +60,7 @@ class Text2ImageAPI:
57
  'model_id': (None, model),
58
  'params': (None, json.dumps(params), 'application/json')
59
  }
60
- response = requests.post(self.URL + 'key/api/v1/text2image/run', headers=self.AUTH_HEADERS, files=data)
61
  data = response.json()
62
  return data['uuid']
63
 
@@ -72,20 +75,23 @@ class Text2ImageAPI:
72
  time.sleep(delay)
73
 
74
 
75
- def api_gradio(prompt, negative, style, width, height, num_images):
76
  api = Text2ImageAPI('https://api-key.fusionbrain.ai/', api_key, secret_key)
77
  model_id = api.get_model()
78
- uuid = api.generate(prompt, negative, styles[style], width, height, model_id, num_images)
79
  images = api.check_generation(uuid)
80
 
81
- decoded_images = [Image.open(BytesIO(base64.b64decode(image))) for image in images]
82
- return decoded_images
 
 
 
83
 
84
 
85
  with gr.Blocks(css=css) as demo:
86
  with gr.Column():
87
  with gr.Row():
88
- prompt = gr.Textbox(show_label=False, placeholder="Описание изображения", lines=3, interactive=True, scale=20)
89
  with gr.Row():
90
  style = gr.Radio(show_label=False, value="Свой стиль", choices=list(styles.keys()))
91
  with gr.Row():
@@ -93,14 +99,14 @@ with gr.Blocks(css=css) as demo:
93
  with gr.Accordion("Дополнительные настройки", open=False):
94
  with gr.Row():
95
  negative = gr.Textbox(label="Отрицательная подсказка", placeholder="Исключения, чего не должно быть на фото", max_lines=3, lines=1, interactive=True, scale=20)
96
- with gr.Row():
97
- width = gr.Slider(label="Ширина", minimum=128, maximum=1024, step=1, value=1024, interactive=True)
98
- height = gr.Slider(label="Высота", minimum=128, maximum=1024, step=1, value=1024, interactive=True)
99
- with gr.Row():
100
- num_images = gr.Slider(label="Количество изображений", minimum=1, maximum=4, step=1, value=1, interactive=True)
101
  with gr.Row():
102
- gallery = gr.Gallery(show_label=False, columns=2)
103
 
104
- button.click(api_gradio, inputs=[prompt, negative, style, width, height, num_images], outputs=gallery)
105
 
106
- demo.queue(max_size=100).launch(show_api=False)
 
1
  import json
2
  import time
3
+
4
  import requests
5
  import base64
6
  from io import BytesIO
 
23
  "Кандинский": "KANDINSKY"
24
  }
25
 
26
+ timeout = 125
27
+
28
  api_key = os.getenv("api_key")
29
  secret_key = os.getenv("secret_key")
30
 
 
42
  data = response.json()
43
  return data[0]['id']
44
 
45
+ def generate(self, prompt, negative, style, width, height, model):
46
  params = {
47
  "type": "GENERATE",
48
+ "numImages": 1,
49
  "style": f"{style}",
50
  "width": width,
51
  "height": height,
 
60
  'model_id': (None, model),
61
  'params': (None, json.dumps(params), 'application/json')
62
  }
63
+ response = requests.post(self.URL + 'key/api/v1/text2image/run', headers=self.AUTH_HEADERS, files=data, timeout=timeout)
64
  data = response.json()
65
  return data['uuid']
66
 
 
75
  time.sleep(delay)
76
 
77
 
78
+ def api_gradio(prompt, negative, style, width, height):
79
  api = Text2ImageAPI('https://api-key.fusionbrain.ai/', api_key, secret_key)
80
  model_id = api.get_model()
81
+ uuid = api.generate(prompt, negative, styles[style], width, height, model_id)
82
  images = api.check_generation(uuid)
83
 
84
+ decoded_data = base64.b64decode(images[0])
85
+ image = Image.open(BytesIO(decoded_data))
86
+
87
+ return image
88
+
89
 
90
 
91
  with gr.Blocks(css=css) as demo:
92
  with gr.Column():
93
  with gr.Row():
94
+ prompt = gr.Textbox(show_label=False, placeholder="Описание изображения", max_lines=3, lines=1, interactive=True, scale=20)
95
  with gr.Row():
96
  style = gr.Radio(show_label=False, value="Свой стиль", choices=list(styles.keys()))
97
  with gr.Row():
 
99
  with gr.Accordion("Дополнительные настройки", open=False):
100
  with gr.Row():
101
  negative = gr.Textbox(label="Отрицательная подсказка", placeholder="Исключения, чего не должно быть на фото", max_lines=3, lines=1, interactive=True, scale=20)
102
+ width = gr.Slider(label="Ширина", minimum=512, maximum=1024, step=1, value=1024, interactive=True)
103
+ height = gr.Slider(label="Высота", minimum=512, maximum=1024, step=1, value=1024, interactive=True)
104
+
105
+ # with gr.Row():
106
+ # images = gr.Slider(label="Количество изображений", minimum=1, maximum=4, step=1, value=1, interactive=True)
107
  with gr.Row():
108
+ gallery = gr.Image(show_label=False)
109
 
110
+ button.click(api_gradio, inputs=[prompt, negative, style, width, height], outputs=gallery)
111
 
112
+ demo.queue(max_size=100).launch(show_api=False)