dongsiqie commited on
Commit
7607d4f
·
1 Parent(s): bbdb00e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -1,13 +1,7 @@
1
- import os
2
  import requests
3
  import gradio as gr
4
 
5
- api_key = os.getenv("API_KEY")
6
-
7
- def generate_image(prompt, code):
8
- if code != os.getenv("code"):
9
- raise gr.Error("❗ Не верный ключ!")
10
- return None
11
  headers = {
12
  "Authorization": f"Bearer {api_key}",
13
  "Content-Type": "application/json",
@@ -16,25 +10,31 @@ def generate_image(prompt, code):
16
  "prompt": prompt,
17
  "num_images": 1,
18
  }
19
- response = requests.post("https://api.openai.com/v1/images/generations", headers=headers, json=data)
20
  response_json = response.json()
21
 
22
- # Получаем URL первого сгенерированного изображения
23
  image_url = response_json["data"][0]["url"]
24
 
25
  return image_url
 
26
  css = """
27
  footer {visibility: hidden !important;}
28
  """
29
- # Создание интерфейса с помощью Gradio
 
30
  with gr.Blocks(css=css, theme='YTheme/Sketch') as demo:
31
  with gr.Row():
32
- code = gr.Textbox(label="Ключ доступа", type="password")
 
 
 
 
33
  with gr.Row():
34
- prompt_input = gr.Textbox(label="Описание изображения")
35
- submit_btn = gr.Button("Генерация", variant='primary')
36
- image_output = gr.Image(label="Изображение")
37
 
38
- submit_btn.click(fn=generate_image, inputs=[prompt_input, code], outputs=image_output)
39
 
40
- demo.launch()
 
 
1
  import requests
2
  import gradio as gr
3
 
4
+ def generate_image(prompt, api_key, request_url):
 
 
 
 
 
5
  headers = {
6
  "Authorization": f"Bearer {api_key}",
7
  "Content-Type": "application/json",
 
10
  "prompt": prompt,
11
  "num_images": 1,
12
  }
13
+ response = requests.post(request_url, headers=headers, json=data)
14
  response_json = response.json()
15
 
16
+ # Get the URL of the first generated image
17
  image_url = response_json["data"][0]["url"]
18
 
19
  return image_url
20
+
21
  css = """
22
  footer {visibility: hidden !important;}
23
  """
24
+
25
+ # Create the interface using Gradio
26
  with gr.Blocks(css=css, theme='YTheme/Sketch') as demo:
27
  with gr.Row():
28
+ gr.Markdown("## Image Generation Tool")
29
+ gr.Markdown("Enter the required information below to generate an image.")
30
+ with gr.Row():
31
+ api_key_input = gr.Textbox(label="API Key", type="password")
32
+ request_url_input = gr.Textbox(label="Request URL", value="https://api.openai.com/v1/images/generations")
33
  with gr.Row():
34
+ prompt_input = gr.Textbox(label="Image Description")
35
+ submit_btn = gr.Button("Generate Image", variant='primary')
36
+ image_output = gr.Image(label="Generated Image")
37
 
38
+ submit_btn.click(fn=generate_image, inputs=[prompt_input, api_key_input, request_url_input], outputs=image_output)
39
 
40
+ demo.launch()