pratikshahp commited on
Commit
100f69c
verified
1 Parent(s): 2d1ecce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -3
app.py CHANGED
@@ -1,7 +1,9 @@
1
  # app.py
2
  import gradio as gr
3
  import os
4
- #from PIL import Image
 
 
5
  from openai import OpenAI
6
  from dotenv import load_dotenv
7
 
@@ -12,6 +14,10 @@ load_dotenv()
12
  api_key = os.getenv("OPENAI_API_KEY") # Ensure your OpenAI API key is stored in the .env file
13
  client = OpenAI(api_key=api_key)
14
 
 
 
 
 
15
 
16
  def generate_image(input_text):
17
  # Call OpenAI's DALL路E 3 model for image generation
@@ -24,7 +30,12 @@ def generate_image(input_text):
24
  )
25
  # Extract the image URL from the response
26
  image_url = response.data[0].url
27
- return image_url
 
 
 
 
 
28
 
29
  # Create a Gradio interface
30
  def create_interface():
@@ -39,7 +50,7 @@ def create_interface():
39
  generate_btn = gr.Button("Generate Image")
40
 
41
  # Image output
42
- image_output = gr.Image(label="Generated Image", type="url")
43
 
44
  # Define the interaction: When the button is clicked, generate the image
45
  generate_btn.click(generate_image, inputs=input_text, outputs=image_output)
 
1
  # app.py
2
  import gradio as gr
3
  import os
4
+ from PIL import Image
5
+ import requests
6
+ from io import BytesIO
7
  from openai import OpenAI
8
  from dotenv import load_dotenv
9
 
 
14
  api_key = os.getenv("OPENAI_API_KEY") # Ensure your OpenAI API key is stored in the .env file
15
  client = OpenAI(api_key=api_key)
16
 
17
+ from io import BytesIO
18
+
19
+ # Initialize OpenAI client
20
+ client = OpenAI()
21
 
22
  def generate_image(input_text):
23
  # Call OpenAI's DALL路E 3 model for image generation
 
30
  )
31
  # Extract the image URL from the response
32
  image_url = response.data[0].url
33
+
34
+ # Fetch the image from the URL
35
+ image_response = requests.get(image_url)
36
+ img = Image.open(BytesIO(image_response.content))
37
+
38
+ return img
39
 
40
  # Create a Gradio interface
41
  def create_interface():
 
50
  generate_btn = gr.Button("Generate Image")
51
 
52
  # Image output
53
+ image_output = gr.Image(label="Generated Image", type="pil")
54
 
55
  # Define the interaction: When the button is clicked, generate the image
56
  generate_btn.click(generate_image, inputs=input_text, outputs=image_output)