Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,7 @@ import random
|
|
| 7 |
import requests
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
-
#
|
| 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 |
-
#
|
| 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 |
-
#
|
| 53 |
example_images = [
|
| 54 |
-
str(Path(
|
| 55 |
-
str(Path(
|
| 56 |
-
str(Path(
|
| 57 |
-
str(Path(
|
| 58 |
-
str(Path(
|
|
|
|
| 59 |
]
|
| 60 |
|
| 61 |
-
#
|
| 62 |
def on_queue_update(update):
|
| 63 |
if isinstance(update, fal_client.InProgress):
|
| 64 |
for log in update.logs:
|
| 65 |
-
|
| 66 |
|
| 67 |
-
#
|
| 68 |
def process_image(img):
|
|
|
|
| 69 |
predicted_class, _, probs = learn.predict(img)
|
| 70 |
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 85 |
response = requests.get(image_url)
|
| 86 |
generated_image = Image.open(io.BytesIO(response.content))
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
gr.Markdown("# 🌼 California Native Plant Classifier & AI Art Generator")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Row():
|
| 95 |
-
input_image = gr.Image(
|
| 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
|
| 102 |
-
|
| 103 |
-
#
|
| 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 |
-
|
|
|
|
|
|
| 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)
|