Ely-testa commited on
Commit
3a82508
·
verified ·
1 Parent(s): 8c0549b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -40
app.py CHANGED
@@ -7,10 +7,7 @@ import random
7
  import requests
8
  from pathlib import Path
9
 
10
- # Load model
11
- learn = load_learner('resnet50_30_categories.pkl')
12
-
13
- # Wikipedia links dictionary
14
  search_terms_wikipedia = {
15
  "blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
16
  "bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
@@ -40,37 +37,41 @@ search_terms_wikipedia = {
40
  "goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
41
  }
42
 
43
- # Prompt templates for AI generation
44
  prompt_templates = [
45
- "A dreamy watercolor scene of a {flower} on a misty morning trail...",
46
- "A loose, expressive watercolor sketch of a {flower} in a wild meadow...",
47
- "An artist's nature journal page featuring a detailed {flower} study...",
48
- "A vibrant plein air painting of a {flower} patch along a coastal trail...",
49
- "A whimsical mixed-media scene of a {flower} garden at sunrise..."
50
  ]
51
 
52
- # Local example image paths
53
  example_images = [
54
- str(Path("example_images/example_1.jpg")),
55
- str(Path("example_images/example_2.jpg")),
56
- str(Path("example_images/example_3.jpg")),
57
- str(Path("example_images/example_4.jpg")),
58
- str(Path("example_images/example_5.jpg")),
 
59
  ]
60
 
61
- # Logging for FAL client
62
  def on_queue_update(update):
63
  if isinstance(update, fal_client.InProgress):
64
  for log in update.logs:
65
- print(log["message"])
66
 
67
- # Process image and return classification + AI-generated artwork + Wiki URL
68
  def process_image(img):
 
69
  predicted_class, _, probs = learn.predict(img)
70
  classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
71
- wiki_url = search_terms_wikipedia.get(predicted_class.lower(), "No Wikipedia entry found.")
72
-
73
- # Generate image via FAL API
 
 
74
  result = fal_client.subscribe(
75
  "fal-ai/flux/schnell",
76
  arguments={
@@ -80,35 +81,59 @@ def process_image(img):
80
  with_logs=True,
81
  on_queue_update=on_queue_update,
82
  )
83
-
84
- image_url = result["images"][0]["url"]
 
85
  response = requests.get(image_url)
86
  generated_image = Image.open(io.BytesIO(response.content))
 
 
87
 
88
- return classification_results, generated_image, str(wiki_url)
 
 
 
 
 
 
89
 
90
- # Gradio interface
91
- with gr.Blocks() as demo:
92
- gr.Markdown("# 🌼 California Native Plant Classifier & AI Art Generator")
93
 
 
 
 
94
  with gr.Row():
95
- input_image = gr.Image(type="pil", label="Upload a Photo", height=250)
96
-
 
97
  with gr.Row():
98
  with gr.Column():
99
  label_output = gr.Label(label="Classification Results")
100
- wiki_output = gr.Textbox(label="Wikipedia Link")
101
- generated_image = gr.Image(label="AI-Generated Artistic Interpretation")
102
-
103
- # Submit button to trigger image processing
104
- submit_btn = gr.Button("Submit")
105
- submit_btn.click(fn=process_image, inputs=input_image, outputs=[label_output, generated_image, wiki_output])
106
-
107
- # Examples
108
  gr.Examples(
109
  examples=example_images,
110
  inputs=input_image,
111
- examples_per_page=6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  )
113
 
114
- demo.launch()
 
 
7
  import requests
8
  from pathlib import Path
9
 
10
+ # Dictionary of plant names and their Wikipedia links
 
 
 
11
  search_terms_wikipedia = {
12
  "blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
13
  "bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
 
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.",
43
+ "A loose, expressive watercolor sketch of a {flower} in a wild meadow, surrounded by dancing butterflies and morning dew drops sparkling like diamonds in the dawn light.",
44
+ "An artist's nature journal page featuring a detailed {flower} study, with delicate ink lines and soft watercolor washes, complete with small sketches of bees and field notes in the margins.",
45
+ "A vibrant plein air painting of a {flower} patch along a coastal hiking trail, with crashing waves and rugged cliffs in the background, painted in bold, energetic brushstrokes.",
46
+ "A whimsical mixed-media scene of a {flower} garden at sunrise, combining loose watercolor washes with detailed botanical illustrations, featuring hidden wildlife and morning fog rolling through the valley."
47
  ]
48
 
49
+ # Example images (using local paths)
50
  example_images = [
51
+ str(Path('example_images/example_1.jpg')),
52
+ str(Path('example_images/example_2.jpg')),
53
+ str(Path('example_images/example_3.jpg')),
54
+ str(Path('example_images/example_4.jpg')),
55
+ str(Path('example_images/example_5.jpg'))
56
+
57
  ]
58
 
59
+ # Function to handle AI generation progress updates
60
  def on_queue_update(update):
61
  if isinstance(update, fal_client.InProgress):
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
 
103
+ # Create the web interface
104
+ with gr.Blocks() as demo:
105
+ # Input section
106
  with gr.Row():
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)