Ely-testa commited on
Commit
45d1ac3
·
verified ·
1 Parent(s): 3589428

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -2,10 +2,10 @@ import base64
2
  import gradio as gr
3
  import random
4
  from fastai.vision.all import *
5
- from openai import OpenAI
6
  from pathlib import Path
7
  from PIL import Image
8
  import io
 
9
 
10
  # Dictionary of plant names and their Wikipedia links
11
  search_terms_wikipedia = {
@@ -90,6 +90,11 @@ example_images = [
90
  str(Path('example_images/example_5.jpg'))
91
  ]
92
 
 
 
 
 
 
93
 
94
  # Main function to process the uploaded image
95
  def process_image(img, generate_image=True):
@@ -109,20 +114,21 @@ def process_image(img, generate_image=True):
109
  endangerment_status = get_status(predicted_class)
110
  print(f"Status found: {endangerment_status}")
111
 
112
- # Generate artistic interpretation using DALL-E
113
- print("Sending request to DALL-E...")
114
  try:
115
- client = OpenAI()
116
-
117
  if generate_image:
118
- result = client.images.generate(
119
- model="gpt-image-1",
120
- prompt=random.choice(prompt_templates).format(flower=predicted_class),
121
- size="1024x1024",
122
- quality="low"
 
 
123
  )
124
-
125
- image_base64 = result.data[0].b64_json
 
126
  image_bytes = base64.b64decode(image_base64)
127
  generated_image = Image.open(io.BytesIO(image_bytes))
128
  else:
@@ -142,7 +148,7 @@ def clear_outputs():
142
  label_output: None,
143
  generated_image: None,
144
  wiki_output: None,
145
- endangerment_output: None # ← NEW
146
  }
147
 
148
 
 
2
  import gradio as gr
3
  import random
4
  from fastai.vision.all import *
 
5
  from pathlib import Path
6
  from PIL import Image
7
  import io
8
+ import fal
9
 
10
  # Dictionary of plant names and their Wikipedia links
11
  search_terms_wikipedia = {
 
90
  str(Path('example_images/example_5.jpg'))
91
  ]
92
 
93
+ # Function to handle FAL queue updates
94
+ def on_queue_update(update):
95
+ """Handle queue updates from FAL."""
96
+ print(f"Queue update: {update}")
97
+
98
 
99
  # Main function to process the uploaded image
100
  def process_image(img, generate_image=True):
 
114
  endangerment_status = get_status(predicted_class)
115
  print(f"Status found: {endangerment_status}")
116
 
117
+ # Generate artistic interpretation using FAL
118
+ print("Sending request to FAL...")
119
  try:
 
 
120
  if generate_image:
121
+ prompt = random.choice(prompt_templates).format(flower=predicted_class)
122
+
123
+ # Use fal-client to generate image
124
+ result = fal.subscribe(
125
+ "fal-ai/flux/dev",
126
+ input={"prompt": prompt},
127
+ on_queue_update=on_queue_update
128
  )
129
+
130
+ # Get the image from the result
131
+ image_base64 = result["images"][0]
132
  image_bytes = base64.b64decode(image_base64)
133
  generated_image = Image.open(io.BytesIO(image_bytes))
134
  else:
 
148
  label_output: None,
149
  generated_image: None,
150
  wiki_output: None,
151
+ endangerment_output: None
152
  }
153
 
154