Upload 12 files
Browse files- .gitattributes +5 -0
- README.md +13 -0
- app.py +128 -65
- example_1.jpg +3 -0
- example_2.jpg +3 -0
- example_3.jpg +0 -0
- example_4.jpg +0 -0
- example_5.jpg +3 -0
- example_6.jpg +3 -0
- from fastai.vision.all import.txt +430 -0
- gitattributes +36 -0
- requirements.txt +3 -0
- resnet50_30_categories.pkl +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
example_1.jpg filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
example_2.jpg filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
example_5.jpg filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
example_6.jpg filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
resnet50_30_categories.pkl filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Flower Classifier
|
| 3 |
+
emoji: 🌖
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.15.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
short_description: flower classifier
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,78 +1,141 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
'
|
| 16 |
-
'
|
| 17 |
-
'
|
| 18 |
-
'
|
| 19 |
-
'
|
| 20 |
-
|
| 21 |
-
'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly',
|
| 22 |
-
'ruby-lipped cattleya', 'cape flower', 'great masterwort', 'siam tulip', 'lenten rose',
|
| 23 |
-
'barberton daisy', 'daffodil', 'sword lily', 'poinsettia', 'bolero deep blue',
|
| 24 |
-
'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',
|
| 25 |
-
'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus',
|
| 26 |
-
'bishop of llandaff', 'gaura', 'geranium', 'orange dahlia', 'pink-yellow dahlia',
|
| 27 |
-
'cautleya spicata', 'japanese anemone', 'black-eyed susan', 'silverbush', 'californian poppy',
|
| 28 |
-
'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',
|
| 29 |
-
'gazania', 'azalea', 'water lily', 'rose', 'thorn apple',
|
| 30 |
-
'morning glory', 'passion flower', 'lotus', 'toad lily', 'anthurium',
|
| 31 |
-
'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose',
|
| 32 |
-
'tree mallow', 'magnolia', 'cyclamen ', 'watercress', 'canna lily',
|
| 33 |
-
'hippeastrum ', 'bee balm', 'pink quill', 'foxglove', 'bougainvillea',
|
| 34 |
-
'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower',
|
| 35 |
-
'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose'
|
| 36 |
]
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
confidence, class_idx = torch.max(probs, 0)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
st.info(f"Confidence: {confidence.item():.2%}")
|
|
|
|
| 1 |
+
from fastai.vision.all import *
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import fal_client
|
| 4 |
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import random
|
| 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",
|
| 14 |
+
"california bluebell": "https://en.wikipedia.org/wiki/Phacelia_minor",
|
| 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 |
+
# Templates for AI image generation
|
| 41 |
+
prompt_templates = [
|
| 42 |
+
"A dreamy Picasso-like 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 crayon etching, 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 |
+
"In a dim, graffiti-covered subway station, a vibrant spray-painted {flower} with neon petals and shimmering gold accents blooms defiantly on a grimy tiled wall, its dripping paint and curling vines injecting unexpected life into the underground gloom.",
|
| 48 |
+
"Amid a bleak industrial wasteland strewn with rusted metal and trash, a single {flower} bathed in a shaft of golden light stands in stark contrast, its delicate form glowing with quiet resilience against the surrounding decay."
|
| 49 |
+
]
|
| 50 |
|
| 51 |
+
# Example images (using local paths)
|
| 52 |
+
example_images = [
|
| 53 |
+
str(Path('example_images/example_1.jpg')),
|
| 54 |
+
str(Path('example_images/example_2.jpg')),
|
| 55 |
+
str(Path('example_images/example_3.jpg')),
|
| 56 |
+
str(Path('example_images/example_4.jpg')),
|
| 57 |
+
str(Path('example_images/example_5.jpg'))
|
| 58 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
]
|
| 60 |
|
| 61 |
+
# Function to handle AI generation progress updates
|
| 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 |
+
# Main function to process the uploaded image
|
| 68 |
+
def process_image(img):
|
| 69 |
+
# Classify the image
|
| 70 |
+
predicted_class, _, probs = learn.predict(img)
|
| 71 |
+
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
|
| 72 |
|
| 73 |
+
# Get Wikipedia link
|
| 74 |
+
wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
|
| 75 |
|
| 76 |
+
# Generate artistic interpretation by calling the Flux API
|
| 77 |
+
result = fal_client.subscribe(
|
| 78 |
+
"fal-ai/flux/schnell",
|
| 79 |
+
arguments={
|
| 80 |
+
"prompt": random.choice(prompt_templates).format(flower=predicted_class),
|
| 81 |
+
"image_size": "portrait_4_3"
|
| 82 |
+
},
|
| 83 |
+
with_logs=True,
|
| 84 |
+
on_queue_update=on_queue_update,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Get the generated image
|
| 88 |
+
image_url = result['images'][0]['url']
|
| 89 |
+
response = requests.get(image_url)
|
| 90 |
+
generated_image = Image.open(io.BytesIO(response.content))
|
| 91 |
+
|
| 92 |
+
return classification_results, generated_image, wiki_url
|
| 93 |
|
| 94 |
+
# Function to clear all outputs
|
| 95 |
+
def clear_outputs():
|
| 96 |
+
return {
|
| 97 |
+
label_output: None,
|
| 98 |
+
generated_image: None,
|
| 99 |
+
wiki_output: None
|
| 100 |
+
}
|
| 101 |
|
| 102 |
+
# Load the AI model
|
| 103 |
+
learn = load_learner('resnet50_30_categories.pkl')
|
| 104 |
|
| 105 |
+
# Create the web interface
|
| 106 |
+
with gr.Blocks() as demo:
|
| 107 |
+
# Input section
|
| 108 |
+
with gr.Row():
|
| 109 |
+
input_image = gr.Image(height=230, width=230, label="Upload Image for Classification", type="pil")
|
| 110 |
+
|
| 111 |
+
# Output section
|
| 112 |
+
with gr.Row():
|
| 113 |
+
with gr.Column():
|
| 114 |
+
label_output = gr.Label(label="Classification Results")
|
| 115 |
+
wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
|
| 116 |
+
generated_image = gr.Image(label="AI Generated Interpretation")
|
| 117 |
+
|
| 118 |
+
# Add example images using local paths
|
| 119 |
+
gr.Examples(
|
| 120 |
+
examples=example_images,
|
| 121 |
+
inputs=input_image,
|
| 122 |
+
examples_per_page=6,
|
| 123 |
+
fn=process_image,
|
| 124 |
+
outputs=[label_output, generated_image, wiki_output]
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Set up what happens when an image is uploaded or removed
|
| 128 |
+
input_image.change(
|
| 129 |
+
fn=process_image,
|
| 130 |
+
inputs=input_image,
|
| 131 |
+
outputs=[label_output, generated_image, wiki_output]
|
| 132 |
+
)
|
| 133 |
|
| 134 |
+
input_image.clear(
|
| 135 |
+
fn=clear_outputs,
|
| 136 |
+
inputs=[],
|
| 137 |
+
outputs=[label_output, generated_image, wiki_output]
|
| 138 |
+
)
|
|
|
|
| 139 |
|
| 140 |
+
# Start the application
|
| 141 |
+
demo.launch(inline=False)
|
|
|
example_1.jpg
ADDED
|
Git LFS Details
|
example_2.jpg
ADDED
|
Git LFS Details
|
example_3.jpg
ADDED
|
example_4.jpg
ADDED
|
example_5.jpg
ADDED
|
Git LFS Details
|
example_6.jpg
ADDED
|
Git LFS Details
|
from fastai.vision.all import.txt
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastai.vision.all import *
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import fal_client
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import random
|
| 7 |
+
import requests
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
# =========================================================
|
| 11 |
+
# WIKIPEDIA LINKS
|
| 12 |
+
# =========================================================
|
| 13 |
+
search_terms_wikipedia = {
|
| 14 |
+
"blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
|
| 15 |
+
"bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
|
| 16 |
+
"california bluebell": "https://en.wikipedia.org/wiki/Phacelia_minor",
|
| 17 |
+
"california buckeye": "https://en.wikipedia.org/wiki/Aesculus_californica",
|
| 18 |
+
"california buckwheat": "https://en.wikipedia.org/wiki/Eriogonum_fasciculatum",
|
| 19 |
+
"california fuchsia": "https://en.wikipedia.org/wiki/Epilobium_canum",
|
| 20 |
+
"california checkerbloom": "https://en.wikipedia.org/wiki/Sidalcea_malviflora",
|
| 21 |
+
"california lilac": "https://en.wikipedia.org/wiki/Ceanothus",
|
| 22 |
+
"california poppy": "https://en.wikipedia.org/wiki/Eschscholzia_californica",
|
| 23 |
+
"california sagebrush": "https://en.wikipedia.org/wiki/Artemisia_californica",
|
| 24 |
+
"california wild grape": "https://en.wikipedia.org/wiki/Vitis_californica",
|
| 25 |
+
"california wild rose": "https://en.wikipedia.org/wiki/Rosa_californica",
|
| 26 |
+
"coyote mint": "https://en.wikipedia.org/wiki/Monardella",
|
| 27 |
+
"elegant clarkia": "https://en.wikipedia.org/wiki/Clarkia_unguiculata",
|
| 28 |
+
"baby blue eyes": "https://en.wikipedia.org/wiki/Nemophila_menziesii",
|
| 29 |
+
"hummingbird sage": "https://en.wikipedia.org/wiki/Salvia_spathacea",
|
| 30 |
+
"delphinium": "https://en.wikipedia.org/wiki/Delphinium",
|
| 31 |
+
"matilija poppy": "https://en.wikipedia.org/wiki/Romneya_coulteri",
|
| 32 |
+
"blue-eyed grass": "https://en.wikipedia.org/wiki/Sisyrinchium_bellum",
|
| 33 |
+
"penstemon spectabilis": "https://en.wikipedia.org/wiki/Penstemon_spectabilis",
|
| 34 |
+
"seaside daisy": "https://en.wikipedia.org/wiki/Erigeron_glaucus",
|
| 35 |
+
"sticky monkeyflower": "https://en.wikipedia.org/wiki/Diplacus_aurantiacus",
|
| 36 |
+
"tidy tips": "https://en.wikipedia.org/wiki/Layia_platyglossa",
|
| 37 |
+
"wild cucumber": "https://en.wikipedia.org/wiki/Marah_(plant)",
|
| 38 |
+
"douglas iris": "https://en.wikipedia.org/wiki/Iris_douglasiana",
|
| 39 |
+
"goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# =========================================================
|
| 43 |
+
# AI PROMPTS
|
| 44 |
+
# =========================================================
|
| 45 |
+
prompt_templates = [
|
| 46 |
+
|
| 47 |
+
"A dreamy watercolor painting of a {flower} in a magical forest with glowing sunlight and butterflies.",
|
| 48 |
+
|
| 49 |
+
"A cinematic artistic interpretation of a {flower} blooming beside a mountain trail at sunrise.",
|
| 50 |
+
|
| 51 |
+
"A fantasy botanical artwork featuring a {flower} surrounded by mist and colorful wildlife.",
|
| 52 |
+
|
| 53 |
+
"An impressionist oil painting of a {flower} field with vibrant brush strokes and golden light.",
|
| 54 |
+
|
| 55 |
+
"A detailed nature journal illustration of a {flower} with artistic sketches and handwritten notes."
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
# =========================================================
|
| 59 |
+
# EXAMPLE IMAGES
|
| 60 |
+
# =========================================================
|
| 61 |
+
example_images = [
|
| 62 |
+
str(Path("example_images/example_1.jpg")),
|
| 63 |
+
str(Path("example_images/example_2.jpg")),
|
| 64 |
+
str(Path("example_images/example_3.jpg")),
|
| 65 |
+
str(Path("example_images/example_4.jpg")),
|
| 66 |
+
str(Path("example_images/example_5.jpg"))
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
# =========================================================
|
| 70 |
+
# LOAD MODEL
|
| 71 |
+
# =========================================================
|
| 72 |
+
learn = load_learner("resnet50_30_categories.pkl")
|
| 73 |
+
|
| 74 |
+
# =========================================================
|
| 75 |
+
# FAL LOGS
|
| 76 |
+
# =========================================================
|
| 77 |
+
def on_queue_update(update):
|
| 78 |
+
|
| 79 |
+
if isinstance(update, fal_client.InProgress):
|
| 80 |
+
|
| 81 |
+
for log in update.logs:
|
| 82 |
+
|
| 83 |
+
print(log["message"])
|
| 84 |
+
|
| 85 |
+
# =========================================================
|
| 86 |
+
# MAIN FUNCTION
|
| 87 |
+
# =========================================================
|
| 88 |
+
def process_image(
|
| 89 |
+
img,
|
| 90 |
+
art_style,
|
| 91 |
+
creativity_level,
|
| 92 |
+
image_quality
|
| 93 |
+
):
|
| 94 |
+
|
| 95 |
+
if img is None:
|
| 96 |
+
|
| 97 |
+
return (
|
| 98 |
+
None,
|
| 99 |
+
None,
|
| 100 |
+
"⚠️ Please upload a flower image.",
|
| 101 |
+
{}
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# =====================================================
|
| 105 |
+
# CLASSIFICATION
|
| 106 |
+
# =====================================================
|
| 107 |
+
predicted_class, _, probs = learn.predict(img)
|
| 108 |
+
|
| 109 |
+
classification_results = dict(
|
| 110 |
+
zip(
|
| 111 |
+
learn.dls.vocab,
|
| 112 |
+
map(float, probs)
|
| 113 |
+
)
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
confidence = max(classification_results.values()) * 100
|
| 117 |
+
|
| 118 |
+
# =====================================================
|
| 119 |
+
# WIKIPEDIA
|
| 120 |
+
# =====================================================
|
| 121 |
+
wiki_url = search_terms_wikipedia.get(
|
| 122 |
+
predicted_class,
|
| 123 |
+
"No article found."
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# =====================================================
|
| 127 |
+
# AI PROMPT
|
| 128 |
+
# =====================================================
|
| 129 |
+
prompt = random.choice(
|
| 130 |
+
prompt_templates
|
| 131 |
+
).format(
|
| 132 |
+
flower=predicted_class
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
final_prompt = f"""
|
| 136 |
+
{prompt}
|
| 137 |
+
|
| 138 |
+
Style: {art_style}
|
| 139 |
+
|
| 140 |
+
Creativity Level: {creativity_level}
|
| 141 |
+
|
| 142 |
+
Image Quality: {image_quality}
|
| 143 |
+
"""
|
| 144 |
+
|
| 145 |
+
# =====================================================
|
| 146 |
+
# IMAGE GENERATION
|
| 147 |
+
# =====================================================
|
| 148 |
+
result = fal_client.subscribe(
|
| 149 |
+
"fal-ai/flux/schnell",
|
| 150 |
+
arguments={
|
| 151 |
+
"prompt": final_prompt,
|
| 152 |
+
"image_size": "portrait_4_3"
|
| 153 |
+
},
|
| 154 |
+
with_logs=True,
|
| 155 |
+
on_queue_update=on_queue_update,
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
image_url = result["images"][0]["url"]
|
| 159 |
+
|
| 160 |
+
response = requests.get(image_url)
|
| 161 |
+
|
| 162 |
+
generated_image = Image.open(
|
| 163 |
+
io.BytesIO(response.content)
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# =====================================================
|
| 167 |
+
# RESULT TEXT
|
| 168 |
+
# =====================================================
|
| 169 |
+
result_text = f"""
|
| 170 |
+
# 🌸 Flower Classification Complete
|
| 171 |
+
|
| 172 |
+
### Predicted Flower:
|
| 173 |
+
## {predicted_class.title()}
|
| 174 |
+
|
| 175 |
+
### Confidence Score:
|
| 176 |
+
## {confidence:.2f}%
|
| 177 |
+
|
| 178 |
+
### AI artistic interpretation generated successfully.
|
| 179 |
+
"""
|
| 180 |
+
|
| 181 |
+
# =====================================================
|
| 182 |
+
# TOP PROBABILITIES
|
| 183 |
+
# =====================================================
|
| 184 |
+
sorted_results = dict(
|
| 185 |
+
sorted(
|
| 186 |
+
classification_results.items(),
|
| 187 |
+
key=lambda x: x[1],
|
| 188 |
+
reverse=True
|
| 189 |
+
)[:5]
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
return (
|
| 193 |
+
generated_image,
|
| 194 |
+
wiki_url,
|
| 195 |
+
result_text,
|
| 196 |
+
sorted_results
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
# =========================================================
|
| 200 |
+
# CUSTOM CSS
|
| 201 |
+
# =========================================================
|
| 202 |
+
custom_css = """
|
| 203 |
+
body {
|
| 204 |
+
background: #f5f7fb;
|
| 205 |
+
font-family: 'Segoe UI', sans-serif;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
.gradio-container {
|
| 209 |
+
max-width: 1350px !important;
|
| 210 |
+
margin: auto;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
.hero {
|
| 214 |
+
background: linear-gradient(135deg,#065f46,#16a34a);
|
| 215 |
+
padding: 40px;
|
| 216 |
+
border-radius: 30px;
|
| 217 |
+
color: white;
|
| 218 |
+
margin-bottom: 20px;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
.hero h1 {
|
| 222 |
+
font-size: 52px;
|
| 223 |
+
font-weight: 800;
|
| 224 |
+
margin-bottom: 10px;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
.hero p {
|
| 228 |
+
font-size: 18px;
|
| 229 |
+
opacity: 0.92;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
.card {
|
| 233 |
+
background: white;
|
| 234 |
+
border-radius: 24px;
|
| 235 |
+
padding: 22px;
|
| 236 |
+
box-shadow: 0 6px 18px rgba(0,0,0,0.08);
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
button {
|
| 240 |
+
height: 60px !important;
|
| 241 |
+
border-radius: 18px !important;
|
| 242 |
+
border: none !important;
|
| 243 |
+
background: linear-gradient(135deg,#16a34a,#15803d) !important;
|
| 244 |
+
color: white !important;
|
| 245 |
+
font-size: 20px !important;
|
| 246 |
+
font-weight: 700 !important;
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
button:hover {
|
| 250 |
+
background: linear-gradient(135deg,#15803d,#166534) !important;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
input, textarea, select {
|
| 254 |
+
border-radius: 16px !important;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
@media (max-width:768px){
|
| 258 |
+
|
| 259 |
+
.hero {
|
| 260 |
+
padding: 22px;
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
.hero h1 {
|
| 264 |
+
font-size: 32px;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
.hero p {
|
| 268 |
+
font-size: 15px;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
button {
|
| 272 |
+
height: 54px !important;
|
| 273 |
+
font-size: 17px !important;
|
| 274 |
+
}
|
| 275 |
+
}
|
| 276 |
+
"""
|
| 277 |
+
|
| 278 |
+
# =========================================================
|
| 279 |
+
# HERO SECTION
|
| 280 |
+
# =========================================================
|
| 281 |
+
hero_html = """
|
| 282 |
+
<div class="hero">
|
| 283 |
+
|
| 284 |
+
<h1>🌸 AI Flower Classifier & Art Generator</h1>
|
| 285 |
+
|
| 286 |
+
<p>
|
| 287 |
+
Upload flower images, identify plant species instantly, and generate AI-powered artistic interpretations.
|
| 288 |
+
Modern responsive interface optimized for mobile and desktop devices.
|
| 289 |
+
</p>
|
| 290 |
+
|
| 291 |
+
</div>
|
| 292 |
+
"""
|
| 293 |
+
|
| 294 |
+
# =========================================================
|
| 295 |
+
# INTERFACE
|
| 296 |
+
# =========================================================
|
| 297 |
+
with gr.Blocks(
|
| 298 |
+
css=custom_css,
|
| 299 |
+
theme=gr.themes.Soft(
|
| 300 |
+
primary_hue="green",
|
| 301 |
+
secondary_hue="emerald"
|
| 302 |
+
)
|
| 303 |
+
) as demo:
|
| 304 |
+
|
| 305 |
+
gr.HTML(hero_html)
|
| 306 |
+
|
| 307 |
+
# =====================================================
|
| 308 |
+
# TOP INFO
|
| 309 |
+
# =====================================================
|
| 310 |
+
with gr.Row():
|
| 311 |
+
|
| 312 |
+
with gr.Column():
|
| 313 |
+
gr.Markdown("""
|
| 314 |
+
### ⚡ Fast AI Recognition
|
| 315 |
+
Instant flower classification
|
| 316 |
+
""")
|
| 317 |
+
|
| 318 |
+
with gr.Column():
|
| 319 |
+
gr.Markdown("""
|
| 320 |
+
### 🎨 AI Art Generator
|
| 321 |
+
Create artistic flower scenes
|
| 322 |
+
""")
|
| 323 |
+
|
| 324 |
+
with gr.Column():
|
| 325 |
+
gr.Markdown("""
|
| 326 |
+
### 📱 Mobile Responsive
|
| 327 |
+
Optimized modern UI
|
| 328 |
+
""")
|
| 329 |
+
|
| 330 |
+
# =====================================================
|
| 331 |
+
# MAIN AREA
|
| 332 |
+
# =====================================================
|
| 333 |
+
with gr.Row():
|
| 334 |
+
|
| 335 |
+
# =================================================
|
| 336 |
+
# LEFT SIDE
|
| 337 |
+
# =================================================
|
| 338 |
+
with gr.Column(scale=1):
|
| 339 |
+
|
| 340 |
+
input_image = gr.Image(
|
| 341 |
+
type="pil",
|
| 342 |
+
label="🌸 Upload Flower Image"
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
art_style = gr.Dropdown(
|
| 346 |
+
choices=[
|
| 347 |
+
"Watercolor",
|
| 348 |
+
"Oil Painting",
|
| 349 |
+
"Fantasy Art",
|
| 350 |
+
"Impressionist",
|
| 351 |
+
"Botanical Illustration"
|
| 352 |
+
],
|
| 353 |
+
value="Watercolor",
|
| 354 |
+
label="🎨 Art Style"
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
creativity_level = gr.Slider(
|
| 358 |
+
minimum=10,
|
| 359 |
+
maximum=100,
|
| 360 |
+
value=75,
|
| 361 |
+
step=5,
|
| 362 |
+
label="🧠 Creativity Level"
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
image_quality = gr.Dropdown(
|
| 366 |
+
choices=[
|
| 367 |
+
"Standard",
|
| 368 |
+
"High",
|
| 369 |
+
"Ultra"
|
| 370 |
+
],
|
| 371 |
+
value="High",
|
| 372 |
+
label="✨ Image Quality"
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
generate_btn = gr.Button(
|
| 376 |
+
"🚀 Analyze & Generate Art",
|
| 377 |
+
variant="primary"
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
# =================================================
|
| 381 |
+
# RIGHT SIDE
|
| 382 |
+
# =================================================
|
| 383 |
+
with gr.Column(scale=1):
|
| 384 |
+
|
| 385 |
+
output_image = gr.Image(
|
| 386 |
+
label="🖌️ AI Artistic Interpretation"
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
+
wiki_output = gr.Textbox(
|
| 390 |
+
label="📚 Wikipedia Article",
|
| 391 |
+
lines=1
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
result_output = gr.Markdown()
|
| 395 |
+
|
| 396 |
+
probability_output = gr.Label(
|
| 397 |
+
label="📊 Top Predictions"
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
# =====================================================
|
| 401 |
+
# EXAMPLES
|
| 402 |
+
# =====================================================
|
| 403 |
+
gr.Examples(
|
| 404 |
+
examples=example_images,
|
| 405 |
+
inputs=input_image
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# =====================================================
|
| 409 |
+
# BUTTON ACTION
|
| 410 |
+
# =====================================================
|
| 411 |
+
generate_btn.click(
|
| 412 |
+
fn=process_image,
|
| 413 |
+
inputs=[
|
| 414 |
+
input_image,
|
| 415 |
+
art_style,
|
| 416 |
+
creativity_level,
|
| 417 |
+
image_quality
|
| 418 |
+
],
|
| 419 |
+
outputs=[
|
| 420 |
+
output_image,
|
| 421 |
+
wiki_output,
|
| 422 |
+
result_output,
|
| 423 |
+
probability_output
|
| 424 |
+
]
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
# =========================================================
|
| 428 |
+
# LAUNCH
|
| 429 |
+
# =========================================================
|
| 430 |
+
demo.launch()
|
gitattributes
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Brittlebush-Encelia-Farinosa-desert-horizon-nursery.jpg filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
fastai<2.8.0
|
| 3 |
+
fal-client
|
resnet50_30_categories.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:747ca29c640764fff86b0c62f4148f18cb6e8aa556511da2d6cf3ef051b83a54
|
| 3 |
+
size 103013051
|