Phauglin commited on
Commit
7894f56
·
verified ·
1 Parent(s): d858f1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -90,7 +90,7 @@ example_images = [
90
 
91
 
92
  # Main function to process the uploaded image
93
- def process_image(img):
94
  print("Starting prediction...")
95
  predicted_class, _, probs = learn.predict(img)
96
  print(f"Prediction complete: {predicted_class}")
@@ -109,16 +109,19 @@ def process_image(img):
109
  try:
110
  client = OpenAI()
111
 
112
- result = client.images.generate(
113
- model="gpt-image-1",
114
- prompt=random.choice(prompt_templates).format(flower=predicted_class),
115
- size="1024x1024",
116
- background="transparent",
117
- quality="medium"
118
- )
 
119
 
120
- image_base64 = result.data[0].b64_json
121
- generated_image = base64.b64decode(image_base64)
 
 
122
 
123
  except Exception as e:
124
  print(f"Error generating image: {e}")
@@ -160,12 +163,12 @@ with gr.Blocks() as demo:
160
  examples=example_images,
161
  inputs=input_image,
162
  examples_per_page=6,
163
- fn=process_image,
164
  outputs=[label_output, generated_image, wiki_output, endangerment_output]
165
  )
166
 
167
  input_image.change(
168
- fn=process_image,
169
  inputs=input_image,
170
  outputs=[label_output, generated_image, wiki_output, endangerment_output]
171
  )
 
90
 
91
 
92
  # Main function to process the uploaded image
93
+ def process_image(img, generate_image=True):
94
  print("Starting prediction...")
95
  predicted_class, _, probs = learn.predict(img)
96
  print(f"Prediction complete: {predicted_class}")
 
109
  try:
110
  client = OpenAI()
111
 
112
+ if generate_image:
113
+ result = client.images.generate(
114
+ model="gpt-image-1",
115
+ prompt=random.choice(prompt_templates).format(flower=predicted_class),
116
+ size="1024x1024",
117
+ background="transparent",
118
+ quality="medium"
119
+ )
120
 
121
+ image_base64 = result.data[0].b64_json
122
+ generated_image = base64.b64decode(image_base64)
123
+ else:
124
+ generated_image = None
125
 
126
  except Exception as e:
127
  print(f"Error generating image: {e}")
 
163
  examples=example_images,
164
  inputs=input_image,
165
  examples_per_page=6,
166
+ fn=lambda img: process_image(img, generate_image=False),
167
  outputs=[label_output, generated_image, wiki_output, endangerment_output]
168
  )
169
 
170
  input_image.change(
171
+ fn=lambda img: process_image(img, generate_image=True),
172
  inputs=input_image,
173
  outputs=[label_output, generated_image, wiki_output, endangerment_output]
174
  )