Rooni commited on
Commit
0176c0d
·
1 Parent(s): 199e8c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -28
app.py CHANGED
@@ -1,44 +1,55 @@
 
1
  import gradio as gr
2
  import requests
3
- import os
4
 
 
5
  BASE_URL = os.getenv("BASE_URL")
6
  API_KEY = os.getenv("API_KEY")
7
 
 
8
  def generate_image(prompt, negative_prompt, sampling_steps, cfg_scale, seed):
9
- headers = {
10
- 'Authorization': f'Bearer {API_KEY}',
11
- }
12
-
13
- data = {
14
- 'prompt': prompt,
15
- 'negative_prompt': negative_prompt,
16
- 'sampling_steps': sampling_steps,
17
- 'cfg_scale': cfg_scale,
18
- 'seed': seed,
19
  }
 
 
 
 
 
 
 
 
20
 
21
- response = requests.post(BASE_URL, headers=headers, json=data)
22
-
23
- if response.status_code == 200:
24
- image_data = response.json()['image']
25
- return image_data
26
- else:
27
- return None
28
-
29
  iface = gr.Interface(
30
  fn=generate_image,
31
  inputs=[
32
- gr.Textbox('text', label='Prompt'),
33
- gr.Textbox('text', label='Negative Prompt'),
34
  ],
35
- outputs=gr.Image(label='Generated Image'),
36
  live=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  )
38
 
39
- with iface.launch(debug=True):
40
- iface.add_slider('Sampling Steps', 1, 30, 1)
41
- iface.add_slider('CFG Scale', 1, 20, 1)
42
- iface.add_textbox('Seed', default='-1')
43
- iface.add_button('Generate', None)
44
- iface.add_download_button('Download Image')
 
1
+ import os
2
  import gradio as gr
3
  import requests
 
4
 
5
+ # Получение URL модели и API-ключа из переменных окружения
6
  BASE_URL = os.getenv("BASE_URL")
7
  API_KEY = os.getenv("API_KEY")
8
 
9
+ # Функция для отправки запроса к API модели
10
  def generate_image(prompt, negative_prompt, sampling_steps, cfg_scale, seed):
11
+ headers = {"Authorization": f"Bearer {API_KEY}"}
12
+
13
+ # Параметры для запроса
14
+ params = {
15
+ "prompt": prompt,
16
+ "negative_prompt": negative_prompt,
17
+ "sampling_steps": int(sampling_steps),
18
+ "cfg_scale": int(cfg_scale),
19
+ "seed": int(seed)
 
20
  }
21
+
22
+ # Отправка запроса к API модели
23
+ response = requests.post(BASE_URL, headers=headers, json=params)
24
+
25
+ # Получение сгенерированного изображения
26
+ generated_image = response.content
27
+
28
+ return generated_image
29
 
30
+ # Определение интерфейса Gradio
 
 
 
 
 
 
 
31
  iface = gr.Interface(
32
  fn=generate_image,
33
  inputs=[
34
+ gr.Textbox(prompt="Prompt", default=""),
35
+ gr.Textbox(prompt="Negative Prompt", default=""),
36
  ],
37
+ outputs=gr.Image(type="pil"),
38
  live=True,
39
+ layout="vertical",
40
+ title="Hugging Face Image Generator",
41
+ description="Generate images using Hugging Face Inference API.",
42
+ theme="compact",
43
+ tabs=[
44
+ "Prompt Input",
45
+ [
46
+ gr.Slider("Sampling Steps", minimum=1, maximum=30, step=1, default=15),
47
+ gr.Slider("CFG Scale", minimum=1, maximum=20, step=1, default=10),
48
+ gr.Textbox("Seed", default="-1"),
49
+ ]
50
+ ],
51
+ interpretation="default"
52
  )
53
 
54
+ # Запуск интерфейса Gradio
55
+ iface.launch()