Michael Krasa commited on
Commit
d4ccb5d
·
1 Parent(s): e9c62c9

fix delete image

Browse files
Files changed (1) hide show
  1. app.py +42 -18
app.py CHANGED
@@ -6,6 +6,7 @@ import io
6
  import random
7
  import requests
8
 
 
9
  search_terms_wikipedia = {
10
  "blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
11
  "bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
@@ -35,7 +36,7 @@ search_terms_wikipedia = {
35
  "goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
36
  }
37
 
38
- # Update prompt templates
39
  prompt_templates = [
40
  "A cosmic {flower} blooming in space, with petals made of swirling galaxies and nebulae, glowing softly against a backdrop of distant stars.",
41
  "An enchanted garden filled with a bioluminescent {flower}, each petal radiating vibrant, otherworldly colors, illuminating the dark, mystical forest around them.",
@@ -44,20 +45,32 @@ prompt_templates = [
44
  "An abstract explosion of a {flower}, blending vibrant colors and fluid shapes in a chaotic, dreamlike composition, evoking movement and emotion."
45
  ]
46
 
 
 
 
 
 
 
 
 
 
 
 
47
  def on_queue_update(update):
48
  if isinstance(update, fal_client.InProgress):
49
  for log in update.logs:
50
  print(log["message"])
51
 
 
52
  def process_image(img):
53
- # First do the classification
54
- predicted_class, idx, probs = learn.predict(img)
55
  classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
56
 
57
- # Get Wikipedia URL for the predicted class
58
  wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
59
 
60
- # Generate FLUX image
61
  result = fal_client.subscribe(
62
  "fal-ai/flux/schnell",
63
  arguments={
@@ -68,47 +81,58 @@ def process_image(img):
68
  on_queue_update=on_queue_update,
69
  )
70
 
 
71
  image_url = result['images'][0]['url']
72
  response = requests.get(image_url)
73
  generated_image = Image.open(io.BytesIO(response.content))
74
 
75
  return classification_results, generated_image, wiki_url
76
 
77
- # Load the learner
 
 
 
 
 
 
 
 
78
  learn = load_learner('export.pkl')
79
 
80
- # Create Gradio interface
81
  with gr.Blocks() as demo:
 
82
  with gr.Row():
83
  input_image = gr.Image(height=192, width=192, label="Upload Image for Classification", type="pil")
 
 
84
  with gr.Row():
85
  with gr.Column():
86
  label_output = gr.Label(label="Classification Results")
87
  wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
88
  generated_image = gr.Image(label="AI Generated Interpretation")
89
 
90
- # Example images
91
- examples = [
92
- 'https://www.deserthorizonnursery.com/wp-content/uploads/2024/03/Brittlebush-Encelia-Farinosa-desert-horizon-nursery.jpg',
93
- 'https://cdn.mos.cms.futurecdn.net/VJE7gSuQ9KWbkqEsWgX5zS.jpg',
94
- 'https://www.parksconservancy.org/sites/default/files/styles/basic/public/A_GEN_131213_WTE_109.jpg?itok=9SDtr4b2',
95
- 'https://silverfallsseed.com/wp-content/uploads/2016/01/tidy-tips-_-9.jpg',
96
- 'https://valleywaternews.org/wp-content/uploads/2016/06/ceanothus-clusters.jpg?w=1440',
97
- 'https://cdn11.bigcommerce.com/s-1b9100svju/images/stencil/1280x1280/products/2044/1440/DETA-635__75522.1664817787.jpg?c=1'
98
- ]
99
  gr.Examples(
100
- examples=examples,
101
  inputs=input_image,
102
  examples_per_page=6,
103
  fn=process_image,
104
  outputs=[label_output, generated_image, wiki_output]
105
  )
106
 
107
- # Set up event handler
108
  input_image.change(
109
  fn=process_image,
110
  inputs=input_image,
111
  outputs=[label_output, generated_image, wiki_output]
112
  )
 
 
 
 
 
 
113
 
 
114
  demo.launch(inline=False)
 
6
  import random
7
  import requests
8
 
9
+ # Dictionary of plant names and their Wikipedia links
10
  search_terms_wikipedia = {
11
  "blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
12
  "bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
 
36
  "goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
37
  }
38
 
39
+ # Templates for AI image generation
40
  prompt_templates = [
41
  "A cosmic {flower} blooming in space, with petals made of swirling galaxies and nebulae, glowing softly against a backdrop of distant stars.",
42
  "An enchanted garden filled with a bioluminescent {flower}, each petal radiating vibrant, otherworldly colors, illuminating the dark, mystical forest around them.",
 
45
  "An abstract explosion of a {flower}, blending vibrant colors and fluid shapes in a chaotic, dreamlike composition, evoking movement and emotion."
46
  ]
47
 
48
+ # Example images for the interface
49
+ example_images = [
50
+ 'https://www.deserthorizonnursery.com/wp-content/uploads/2024/03/Brittlebush-Encelia-Farinosa-desert-horizon-nursery.jpg',
51
+ 'https://cdn.mos.cms.futurecdn.net/VJE7gSuQ9KWbkqEsWgX5zS.jpg',
52
+ 'https://www.parksconservancy.org/sites/default/files/styles/basic/public/A_GEN_131213_WTE_109.jpg?itok=9SDtr4b2',
53
+ 'https://silverfallsseed.com/wp-content/uploads/2016/01/tidy-tips-_-9.jpg',
54
+ 'https://valleywaternews.org/wp-content/uploads/2016/06/ceanothus-clusters.jpg?w=1440',
55
+ 'https://cdn11.bigcommerce.com/s-1b9100svju/images/stencil/1280x1280/products/2044/1440/DETA-635__75522.1664817787.jpg?c=1'
56
+ ]
57
+
58
+ # Function to handle AI generation progress updates
59
  def on_queue_update(update):
60
  if isinstance(update, fal_client.InProgress):
61
  for log in update.logs:
62
  print(log["message"])
63
 
64
+ # Main function to process the uploaded image
65
  def process_image(img):
66
+ # Classify the image
67
+ predicted_class, _, probs = learn.predict(img)
68
  classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
69
 
70
+ # Get Wikipedia link
71
  wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
72
 
73
+ # Generate artistic interpretation by calling the Flux API
74
  result = fal_client.subscribe(
75
  "fal-ai/flux/schnell",
76
  arguments={
 
81
  on_queue_update=on_queue_update,
82
  )
83
 
84
+ # Get the generated image
85
  image_url = result['images'][0]['url']
86
  response = requests.get(image_url)
87
  generated_image = Image.open(io.BytesIO(response.content))
88
 
89
  return classification_results, generated_image, wiki_url
90
 
91
+ # Function to clear all outputs
92
+ def clear_outputs():
93
+ return {
94
+ label_output: None,
95
+ generated_image: None,
96
+ wiki_output: None
97
+ }
98
+
99
+ # Load the AI model
100
  learn = load_learner('export.pkl')
101
 
102
+ # Create the web interface
103
  with gr.Blocks() as demo:
104
+ # Input section
105
  with gr.Row():
106
  input_image = gr.Image(height=192, width=192, label="Upload Image for Classification", type="pil")
107
+
108
+ # Output section
109
  with gr.Row():
110
  with gr.Column():
111
  label_output = gr.Label(label="Classification Results")
112
  wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
113
  generated_image = gr.Image(label="AI Generated Interpretation")
114
 
115
+ # Add example images
 
 
 
 
 
 
 
 
116
  gr.Examples(
117
+ examples=example_images,
118
  inputs=input_image,
119
  examples_per_page=6,
120
  fn=process_image,
121
  outputs=[label_output, generated_image, wiki_output]
122
  )
123
 
124
+ # Set up what happens when an image is uploaded or removed
125
  input_image.change(
126
  fn=process_image,
127
  inputs=input_image,
128
  outputs=[label_output, generated_image, wiki_output]
129
  )
130
+
131
+ input_image.clear(
132
+ fn=clear_outputs,
133
+ inputs=[],
134
+ outputs=[label_output, generated_image, wiki_output]
135
+ )
136
 
137
+ # Start the application
138
  demo.launch(inline=False)