Ely-testa commited on
Commit
ef50b14
·
verified ·
1 Parent(s): e211546

restore working version

Browse files
Files changed (1) hide show
  1. app.py +21 -80
app.py CHANGED
@@ -37,39 +37,6 @@ search_terms_wikipedia = {
37
  "goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
38
  }
39
 
40
- flowers_endangerment = {
41
- "Blazing Star": "Not considered endangered.",
42
- "Bristlecone Pine": "Least Concern (stable population).",
43
- "California Bluebell": "Not listed as endangered or threatened.",
44
- "California Buckeye": "Not endangered.",
45
- "California Buckwheat": "Generally secure.",
46
- "California Fuchsia": "Not endangered overall; some subspecies at risk.",
47
- "California Checkerbloom": "Not generally endangered; some subspecies critically imperiled.",
48
- "California Lilac": "Most species not endangered; some species are endangered.",
49
- "California Poppy": "Generally secure; some subspecies face threats.",
50
- "California Sagebrush": "Considered secure (G4-G5).",
51
- "California Wild Grape": "Apparently secure (G4).",
52
- "California Wild Rose": "Secure (G4).",
53
- "Coyote Mint": "Varies by species; some federally listed as endangered.",
54
- "Elegant Clarkia": "Secure (G5).",
55
- "Baby Blue Eyes": "Secure.",
56
- "Hummingbird Sage": "Apparently secure (G4).",
57
- "Delphinium": "Varies by species; some are endangered.",
58
- "Matilija Poppy": "Not currently endangered.",
59
- "Blue-Eyed Grass": "Not endangered.",
60
- "Penstemon Spectabilis": "Not endangered.",
61
- "Seaside Daisy": "Not endangered.",
62
- "Sticky Monkeyflower": "Not endangered.",
63
- "Tidy Tips": "Generally not endangered; some subspecies may be at risk.",
64
- "Wild Cucumber": "Generally not endangered.",
65
- "Douglas Iris": "Not endangered.",
66
- "Goldfields Coreopsis": "Varies by species; many not endangered."
67
- }
68
-
69
- def get_status(flower_name):
70
- """Return the endangerment status of a given flower name."""
71
- return flowers_endangerment.get(flower_name, "Flower not found in database.")
72
-
73
  # Templates for AI image generation
74
  prompt_templates = [
75
  "A dreamy watercolor scene of a {flower} on a misty morning trail, with golden sunbeams filtering through towering redwoods, and a curious hummingbird hovering nearby.",
@@ -95,34 +62,16 @@ def on_queue_update(update):
95
  for log in update.logs:
96
  print(log["message"])
97
 
98
-
99
-
100
- def get_status(flower_name):
101
- """Return the endangerment status of a given flower name."""
102
- # Normalize input for dictionary lookup
103
- normalized_name = flower_name.title()
104
- return flowers_endangerment.get(normalized_name, "Flower not found in database.")
105
-
106
-
107
-
108
-
109
  # Main function to process the uploaded image
110
  def process_image(img):
111
- print("Starting prediction...")
112
  predicted_class, _, probs = learn.predict(img)
113
- print(f"Prediction complete: {predicted_class}")
114
-
115
  classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
116
 
117
  # Get Wikipedia link
118
  wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
119
 
120
- # Get endangerment status
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={
@@ -132,29 +81,22 @@ def process_image(img):
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 {
151
  label_output: None,
152
- # generated_image: None,
153
- # wiki_output: None,
154
- #status_output: None # ← NEW
155
  }
156
 
157
-
158
  # Load the AI model
159
  learn = load_learner('resnet50_30_categories.pkl')
160
 
@@ -165,34 +107,33 @@ with gr.Blocks() as demo:
165
  input_image = gr.Image(height=230, width=230, label="Upload Image for Classification", type="pil")
166
 
167
  # Output section
168
- # Output section
169
  with gr.Row():
170
  with gr.Column():
171
  label_output = gr.Label(label="Classification Results")
172
- #wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
173
- #status_output = gr.Textbox(label="Endangerment Status", lines=1) # ← NEW
174
- #generated_image = gr.Image(label="AI Generated Interpretation")
175
-
176
  # Add example images using local paths
177
  gr.Examples(
178
  examples=example_images,
179
  inputs=input_image,
180
- examples_per_page=6
181
- )
182
-
183
-
 
 
184
  input_image.change(
185
  fn=process_image,
186
  inputs=input_image,
187
- outputs=[label_output]#, generated_image, wiki_output, status_output] # ← UPDATED
188
- )
189
-
190
  input_image.clear(
191
  fn=clear_outputs,
192
  inputs=[],
193
- outputs=[label_output]#, generated_image, wiki_output, status_output] # ← UPDATED
194
- )
195
-
196
 
197
  # Start the application
198
  demo.launch(inline=False)
 
37
  "goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
38
  }
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Templates for AI image generation
41
  prompt_templates = [
42
  "A dreamy watercolor scene of a {flower} on a misty morning trail, with golden sunbeams filtering through towering redwoods, and a curious hummingbird hovering nearby.",
 
62
  for log in update.logs:
63
  print(log["message"])
64
 
 
 
 
 
 
 
 
 
 
 
 
65
  # Main function to process the uploaded image
66
  def process_image(img):
67
+ # Classify the image
68
  predicted_class, _, probs = learn.predict(img)
 
 
69
  classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
70
 
71
  # Get Wikipedia link
72
  wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
73
 
74
+ # Generate artistic interpretation by calling the Flux API
 
 
 
 
 
75
  result = fal_client.subscribe(
76
  "fal-ai/flux/schnell",
77
  arguments={
 
81
  with_logs=True,
82
  on_queue_update=on_queue_update,
83
  )
84
+
85
+ # Get the generated image
 
86
  image_url = result['images'][0]['url']
 
87
  response = requests.get(image_url)
88
  generated_image = Image.open(io.BytesIO(response.content))
89
 
90
+ return classification_results, generated_image, wiki_url
 
 
 
91
 
92
  # Function to clear all outputs
93
  def clear_outputs():
94
  return {
95
  label_output: None,
96
+ generated_image: None,
97
+ wiki_output: None
 
98
  }
99
 
 
100
  # Load the AI model
101
  learn = load_learner('resnet50_30_categories.pkl')
102
 
 
107
  input_image = gr.Image(height=230, width=230, label="Upload Image for Classification", type="pil")
108
 
109
  # Output section
 
110
  with gr.Row():
111
  with gr.Column():
112
  label_output = gr.Label(label="Classification Results")
113
+ wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
114
+ generated_image = gr.Image(label="AI Generated Interpretation")
115
+
 
116
  # Add example images using local paths
117
  gr.Examples(
118
  examples=example_images,
119
  inputs=input_image,
120
+ examples_per_page=6,
121
+ fn=process_image,
122
+ outputs=[label_output, generated_image, wiki_output]
123
+ )
124
+
125
+ # Set up what happens when an image is uploaded or removed
126
  input_image.change(
127
  fn=process_image,
128
  inputs=input_image,
129
+ outputs=[label_output, generated_image, wiki_output]
130
+ )
131
+
132
  input_image.clear(
133
  fn=clear_outputs,
134
  inputs=[],
135
+ outputs=[label_output, generated_image, wiki_output]
136
+ )
 
137
 
138
  # Start the application
139
  demo.launch(inline=False)