Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,37 +7,18 @@ import random
|
|
| 7 |
import requests
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
| 11 |
search_terms_wikipedia = {
|
| 12 |
"blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
|
| 13 |
"bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
|
| 14 |
-
|
| 15 |
-
"california buckeye": "https://en.wikipedia.org/wiki/Aesculus_californica",
|
| 16 |
-
"california buckwheat": "https://en.wikipedia.org/wiki/Eriogonum_fasciculatum",
|
| 17 |
-
"california fuchsia": "https://en.wikipedia.org/wiki/Epilobium_canum",
|
| 18 |
-
"california checkerbloom": "https://en.wikipedia.org/wiki/Sidalcea_malviflora",
|
| 19 |
-
"california lilac": "https://en.wikipedia.org/wiki/Ceanothus",
|
| 20 |
-
"california poppy": "https://en.wikipedia.org/wiki/Eschscholzia_californica",
|
| 21 |
-
"california sagebrush": "https://en.wikipedia.org/wiki/Artemisia_californica",
|
| 22 |
-
"california wild grape": "https://en.wikipedia.org/wiki/Vitis_californica",
|
| 23 |
-
"california wild rose": "https://en.wikipedia.org/wiki/Rosa_californica",
|
| 24 |
-
"coyote mint": "https://en.wikipedia.org/wiki/Monardella",
|
| 25 |
-
"elegant clarkia": "https://en.wikipedia.org/wiki/Clarkia_unguiculata",
|
| 26 |
-
"baby blue eyes": "https://en.wikipedia.org/wiki/Nemophila_menziesii",
|
| 27 |
-
"hummingbird sage": "https://en.wikipedia.org/wiki/Salvia_spathacea",
|
| 28 |
-
"delphinium": "https://en.wikipedia.org/wiki/Delphinium",
|
| 29 |
-
"matilija poppy": "https://en.wikipedia.org/wiki/Romneya_coulteri",
|
| 30 |
-
"blue-eyed grass": "https://en.wikipedia.org/wiki/Sisyrinchium_bellum",
|
| 31 |
-
"penstemon spectabilis": "https://en.wikipedia.org/wiki/Penstemon_spectabilis",
|
| 32 |
-
"seaside daisy": "https://en.wikipedia.org/wiki/Erigeron_glaucus",
|
| 33 |
-
"sticky monkeyflower": "https://en.wikipedia.org/wiki/Diplacus_aurantiacus",
|
| 34 |
-
"tidy tips": "https://en.wikipedia.org/wiki/Layia_platyglossa",
|
| 35 |
-
"wild cucumber": "https://en.wikipedia.org/wiki/Marah_(plant)",
|
| 36 |
-
"douglas iris": "https://en.wikipedia.org/wiki/Iris_douglasiana",
|
| 37 |
"goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
|
| 38 |
}
|
| 39 |
|
| 40 |
-
#
|
| 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.",
|
|
@@ -46,32 +27,30 @@ prompt_templates = [
|
|
| 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
|
| 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 |
-
#
|
| 60 |
def on_queue_update(update):
|
| 61 |
if isinstance(update, fal_client.InProgress):
|
| 62 |
for log in update.logs:
|
| 63 |
-
|
| 64 |
|
| 65 |
-
#
|
| 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 |
-
#
|
| 72 |
-
wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
|
| 73 |
-
|
| 74 |
-
# Generate
|
| 75 |
result = fal_client.subscribe(
|
| 76 |
"fal-ai/flux/schnell",
|
| 77 |
arguments={
|
|
@@ -81,59 +60,36 @@ def process_image(img):
|
|
| 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 |
-
|
| 101 |
-
learn = load_learner('resnet50_30_categories.pkl')
|
| 102 |
|
| 103 |
-
#
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
-
#
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
-
input_image = gr.Image(height=230, width=230, label="Upload
|
| 108 |
-
|
| 109 |
-
# Output section
|
| 110 |
with gr.Row():
|
| 111 |
with gr.Column():
|
| 112 |
-
label_output = gr.Label(label="
|
| 113 |
-
wiki_output = gr.Textbox(label="Wikipedia
|
| 114 |
-
generated_image = gr.Image(label="AI
|
| 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 |
-
|
| 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 |
-
|
| 139 |
-
demo.launch(inline=False)
|
|
|
|
| 7 |
import requests
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
+
# Load your model
|
| 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 |
+
# ... (same as before)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Prompt templates for art generation
|
| 22 |
prompt_templates = [
|
| 23 |
"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.",
|
| 24 |
"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.",
|
|
|
|
| 27 |
"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."
|
| 28 |
]
|
| 29 |
|
| 30 |
+
# Example image paths (replace with actual paths on your system or Hugging Face space)
|
| 31 |
example_images = [
|
| 32 |
str(Path('example_images/example_1.jpg')),
|
| 33 |
str(Path('example_images/example_2.jpg')),
|
| 34 |
str(Path('example_images/example_3.jpg')),
|
| 35 |
str(Path('example_images/example_4.jpg')),
|
| 36 |
str(Path('example_images/example_5.jpg'))
|
|
|
|
| 37 |
]
|
| 38 |
|
| 39 |
+
# Optional: FAL generation logging
|
| 40 |
def on_queue_update(update):
|
| 41 |
if isinstance(update, fal_client.InProgress):
|
| 42 |
for log in update.logs:
|
| 43 |
+
print(log["message"])
|
| 44 |
|
| 45 |
+
# Processing function
|
| 46 |
def process_image(img):
|
|
|
|
| 47 |
predicted_class, _, probs = learn.predict(img)
|
| 48 |
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
|
| 49 |
+
|
| 50 |
+
# Wikipedia
|
| 51 |
+
wiki_url = search_terms_wikipedia.get(predicted_class.lower(), "No Wikipedia entry found.")
|
| 52 |
+
|
| 53 |
+
# Generate image via FAL
|
| 54 |
result = fal_client.subscribe(
|
| 55 |
"fal-ai/flux/schnell",
|
| 56 |
arguments={
|
|
|
|
| 60 |
with_logs=True,
|
| 61 |
on_queue_update=on_queue_update,
|
| 62 |
)
|
| 63 |
+
|
|
|
|
| 64 |
image_url = result['images'][0]['url']
|
| 65 |
response = requests.get(image_url)
|
| 66 |
generated_image = Image.open(io.BytesIO(response.content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
return classification_results, generated_image, wiki_url
|
|
|
|
| 69 |
|
| 70 |
+
# Interface
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("# 🌼 Wildflower Classifier & Artistic Generator")
|
| 73 |
+
|
| 74 |
with gr.Row():
|
| 75 |
+
input_image = gr.Image(height=230, width=230, label="Upload an image", type="pil")
|
| 76 |
+
|
|
|
|
| 77 |
with gr.Row():
|
| 78 |
with gr.Column():
|
| 79 |
+
label_output = gr.Label(label="Prediction")
|
| 80 |
+
wiki_output = gr.Textbox(label="Wikipedia Link")
|
| 81 |
+
generated_image = gr.Image(label="AI Artistic Interpretation")
|
| 82 |
+
|
|
|
|
| 83 |
gr.Examples(
|
| 84 |
examples=example_images,
|
| 85 |
inputs=input_image,
|
| 86 |
+
examples_per_page=6
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
+
|
| 89 |
+
input_image.upload(
|
|
|
|
| 90 |
fn=process_image,
|
| 91 |
inputs=input_image,
|
| 92 |
outputs=[label_output, generated_image, wiki_output]
|
| 93 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
demo.launch()
|
|
|