Phauglin commited on
Commit
ba829b6
·
verified ·
1 Parent(s): f7a6ead

change to openai image generation

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -106,6 +106,7 @@ def get_status(flower_name):
106
 
107
 
108
 
 
109
  # Main function to process the uploaded image
110
  def process_image(img):
111
  print("Starting prediction...")
@@ -121,30 +122,33 @@ def process_image(img):
121
  endangerment_status = get_status(predicted_class)
122
  print(f"Status found: {endangerment_status}")
123
 
124
- # Generate artistic interpretation
125
- print("Sending request to FAL API...")
126
- result = fal_client.subscribe(
127
- "fal-ai/flux/schnell",
128
- arguments={
129
- "prompt": random.choice(prompt_templates).format(flower=predicted_class),
130
- "image_size": "portrait_4_3"
131
- },
132
- with_logs=True,
133
- on_queue_update=on_queue_update,
134
- )
135
- print("FAL API responded")
136
-
137
- # Retrieve image
138
- image_url = result['images'][0]['url']
139
- print(f"Image URL: {image_url}")
140
- response = requests.get(image_url)
141
- generated_image = Image.open(io.BytesIO(response.content))
 
 
 
 
142
 
143
  print("Image retrieved and ready to return")
144
  return classification_results, generated_image, wiki_url, endangerment_status
145
 
146
 
147
-
148
  # Function to clear all outputs
149
  def clear_outputs():
150
  return {
 
106
 
107
 
108
 
109
+ # Main function to process the uploaded image
110
  # Main function to process the uploaded image
111
  def process_image(img):
112
  print("Starting prediction...")
 
122
  endangerment_status = get_status(predicted_class)
123
  print(f"Status found: {endangerment_status}")
124
 
125
+ # Generate artistic interpretation using DALL-E
126
+ print("Sending request to DALL-E...")
127
+ try:
128
+ response = openai.images.generate(
129
+ model="dall-e-3",
130
+ prompt=random.choice(prompt_templates).format(flower=predicted_class),
131
+ size="1024x1024",
132
+ quality="standard",
133
+ n=1,
134
+ )
135
+
136
+ # Get the image URL
137
+ image_url = response.data[0].url
138
+ print(f"Image URL: {image_url}")
139
+
140
+ # Download the generated image
141
+ response = requests.get(image_url)
142
+ generated_image = Image.open(io.BytesIO(response.content))
143
+
144
+ except Exception as e:
145
+ print(f"Error generating image: {e}")
146
+ generated_image = None
147
 
148
  print("Image retrieved and ready to return")
149
  return classification_results, generated_image, wiki_url, endangerment_status
150
 
151
 
 
152
  # Function to clear all outputs
153
  def clear_outputs():
154
  return {