Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,51 +7,70 @@ import random
|
|
| 7 |
import requests
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
learn = load_learner('resnet50_30_categories.pkl')
|
| 12 |
|
| 13 |
-
# Wikipedia links
|
| 14 |
search_terms_wikipedia = {
|
| 15 |
"blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
|
| 16 |
"bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
|
| 19 |
}
|
| 20 |
|
| 21 |
-
# Prompt templates for
|
| 22 |
prompt_templates = [
|
| 23 |
-
"A dreamy watercolor scene of a {flower} on a misty morning trail
|
| 24 |
-
"A loose, expressive watercolor sketch of a {flower} in a wild meadow
|
| 25 |
-
"An artist's nature journal page featuring a detailed {flower} study
|
| 26 |
-
"A vibrant plein air painting of a {flower} patch along a coastal
|
| 27 |
-
"A whimsical mixed-media scene of a {flower} garden at sunrise
|
| 28 |
]
|
| 29 |
|
| 30 |
-
#
|
| 31 |
example_images = [
|
| 32 |
-
str(Path(
|
| 33 |
-
str(Path(
|
| 34 |
-
str(Path(
|
| 35 |
-
str(Path(
|
| 36 |
-
str(Path(
|
| 37 |
]
|
| 38 |
|
|
|
|
| 39 |
def on_queue_update(update):
|
| 40 |
if isinstance(update, fal_client.InProgress):
|
| 41 |
for log in update.logs:
|
| 42 |
print(log["message"])
|
| 43 |
-
else:
|
| 44 |
-
print("Received non-InProgress update:", update)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
def process_image(img):
|
| 48 |
predicted_class, _, probs = learn.predict(img)
|
| 49 |
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
|
| 50 |
-
|
| 51 |
-
# Wikipedia
|
| 52 |
wiki_url = search_terms_wikipedia.get(predicted_class.lower(), "No Wikipedia entry found.")
|
| 53 |
|
| 54 |
-
# Generate image via FAL
|
| 55 |
result = fal_client.subscribe(
|
| 56 |
"fal-ai/flux/schnell",
|
| 57 |
arguments={
|
|
@@ -62,35 +81,34 @@ def process_image(img):
|
|
| 62 |
on_queue_update=on_queue_update,
|
| 63 |
)
|
| 64 |
|
| 65 |
-
image_url = result[
|
| 66 |
response = requests.get(image_url)
|
| 67 |
generated_image = Image.open(io.BytesIO(response.content))
|
| 68 |
|
| 69 |
-
return classification_results, generated_image, wiki_url
|
| 70 |
|
| 71 |
-
#
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
-
gr.Markdown("# 🌼
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
-
input_image = gr.Image(
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column():
|
| 80 |
-
label_output = gr.Label(label="
|
| 81 |
wiki_output = gr.Textbox(label="Wikipedia Link")
|
| 82 |
-
generated_image = gr.Image(label="AI Artistic Interpretation")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
|
|
|
| 84 |
gr.Examples(
|
| 85 |
examples=example_images,
|
| 86 |
inputs=input_image,
|
| 87 |
examples_per_page=6
|
| 88 |
)
|
| 89 |
|
| 90 |
-
input_image.upload(
|
| 91 |
-
fn=process_image,
|
| 92 |
-
inputs=input_image,
|
| 93 |
-
outputs=[label_output, generated_image, wiki_output]
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
demo.launch()
|
|
|
|
| 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",
|
| 17 |
+
"california bluebell": "https://en.wikipedia.org/wiki/Phacelia_minor",
|
| 18 |
+
"california buckeye": "https://en.wikipedia.org/wiki/Aesculus_californica",
|
| 19 |
+
"california buckwheat": "https://en.wikipedia.org/wiki/Eriogonum_fasciculatum",
|
| 20 |
+
"california fuchsia": "https://en.wikipedia.org/wiki/Epilobium_canum",
|
| 21 |
+
"california checkerbloom": "https://en.wikipedia.org/wiki/Sidalcea_malviflora",
|
| 22 |
+
"california lilac": "https://en.wikipedia.org/wiki/Ceanothus",
|
| 23 |
+
"california poppy": "https://en.wikipedia.org/wiki/Eschscholzia_californica",
|
| 24 |
+
"california sagebrush": "https://en.wikipedia.org/wiki/Artemisia_californica",
|
| 25 |
+
"california wild grape": "https://en.wikipedia.org/wiki/Vitis_californica",
|
| 26 |
+
"california wild rose": "https://en.wikipedia.org/wiki/Rosa_californica",
|
| 27 |
+
"coyote mint": "https://en.wikipedia.org/wiki/Monardella",
|
| 28 |
+
"elegant clarkia": "https://en.wikipedia.org/wiki/Clarkia_unguiculata",
|
| 29 |
+
"baby blue eyes": "https://en.wikipedia.org/wiki/Nemophila_menziesii",
|
| 30 |
+
"hummingbird sage": "https://en.wikipedia.org/wiki/Salvia_spathacea",
|
| 31 |
+
"delphinium": "https://en.wikipedia.org/wiki/Delphinium",
|
| 32 |
+
"matilija poppy": "https://en.wikipedia.org/wiki/Romneya_coulteri",
|
| 33 |
+
"blue-eyed grass": "https://en.wikipedia.org/wiki/Sisyrinchium_bellum",
|
| 34 |
+
"penstemon spectabilis": "https://en.wikipedia.org/wiki/Penstemon_spectabilis",
|
| 35 |
+
"seaside daisy": "https://en.wikipedia.org/wiki/Erigeron_glaucus",
|
| 36 |
+
"sticky monkeyflower": "https://en.wikipedia.org/wiki/Diplacus_aurantiacus",
|
| 37 |
+
"tidy tips": "https://en.wikipedia.org/wiki/Layia_platyglossa",
|
| 38 |
+
"wild cucumber": "https://en.wikipedia.org/wiki/Marah_(plant)",
|
| 39 |
+
"douglas iris": "https://en.wikipedia.org/wiki/Iris_douglasiana",
|
| 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={
|
|
|
|
| 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()
|