Spaces:
Sleeping
Sleeping
| from fastai.vision.all import * | |
| import gradio as gr | |
| import fal_client | |
| from PIL import Image | |
| import io | |
| import random | |
| import requests | |
| from pathlib import Path | |
| # Load model | |
| learn = load_learner('resnet50_30_categories.pkl') | |
| # Wikipedia links dictionary | |
| search_terms_wikipedia = { | |
| "blazing star": "https://en.wikipedia.org/wiki/Mentzelia", | |
| "bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva", | |
| "california bluebell": "https://en.wikipedia.org/wiki/Phacelia_minor", | |
| "california buckeye": "https://en.wikipedia.org/wiki/Aesculus_californica", | |
| "california buckwheat": "https://en.wikipedia.org/wiki/Eriogonum_fasciculatum", | |
| "california fuchsia": "https://en.wikipedia.org/wiki/Epilobium_canum", | |
| "california checkerbloom": "https://en.wikipedia.org/wiki/Sidalcea_malviflora", | |
| "california lilac": "https://en.wikipedia.org/wiki/Ceanothus", | |
| "california poppy": "https://en.wikipedia.org/wiki/Eschscholzia_californica", | |
| "california sagebrush": "https://en.wikipedia.org/wiki/Artemisia_californica", | |
| "california wild grape": "https://en.wikipedia.org/wiki/Vitis_californica", | |
| "california wild rose": "https://en.wikipedia.org/wiki/Rosa_californica", | |
| "coyote mint": "https://en.wikipedia.org/wiki/Monardella", | |
| "elegant clarkia": "https://en.wikipedia.org/wiki/Clarkia_unguiculata", | |
| "baby blue eyes": "https://en.wikipedia.org/wiki/Nemophila_menziesii", | |
| "hummingbird sage": "https://en.wikipedia.org/wiki/Salvia_spathacea", | |
| "delphinium": "https://en.wikipedia.org/wiki/Delphinium", | |
| "matilija poppy": "https://en.wikipedia.org/wiki/Romneya_coulteri", | |
| "blue-eyed grass": "https://en.wikipedia.org/wiki/Sisyrinchium_bellum", | |
| "penstemon spectabilis": "https://en.wikipedia.org/wiki/Penstemon_spectabilis", | |
| "seaside daisy": "https://en.wikipedia.org/wiki/Erigeron_glaucus", | |
| "sticky monkeyflower": "https://en.wikipedia.org/wiki/Diplacus_aurantiacus", | |
| "tidy tips": "https://en.wikipedia.org/wiki/Layia_platyglossa", | |
| "wild cucumber": "https://en.wikipedia.org/wiki/Marah_(plant)", | |
| "douglas iris": "https://en.wikipedia.org/wiki/Iris_douglasiana", | |
| "goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis" | |
| } | |
| # Prompt templates for AI generation | |
| prompt_templates = [ | |
| "A dreamy watercolor scene of a {flower} on a misty morning trail...", | |
| "A loose, expressive watercolor sketch of a {flower} in a wild meadow...", | |
| "An artist's nature journal page featuring a detailed {flower} study...", | |
| "A vibrant plein air painting of a {flower} patch along a coastal trail...", | |
| "A whimsical mixed-media scene of a {flower} garden at sunrise..." | |
| ] | |
| # Local example image paths | |
| example_images = [ | |
| str(Path("example_images/example_1.jpg")), | |
| str(Path("example_images/example_2.jpg")), | |
| str(Path("example_images/example_3.jpg")), | |
| str(Path("example_images/example_4.jpg")), | |
| str(Path("example_images/example_5.jpg")), | |
| ] | |
| # Logging for FAL client | |
| def on_queue_update(update): | |
| if isinstance(update, fal_client.InProgress): | |
| for log in update.logs: | |
| print(log["message"]) | |
| # Process image and return classification + AI-generated artwork + Wiki URL | |
| def process_image(img): | |
| predicted_class, _, probs = learn.predict(img) | |
| classification_results = dict(zip(learn.dls.vocab, map(float, probs))) | |
| wiki_url = search_terms_wikipedia.get(predicted_class.lower(), "No Wikipedia entry found.") | |
| # Generate image via FAL API | |
| result = fal_client.subscribe( | |
| "fal-ai/flux/schnell", | |
| arguments={ | |
| "prompt": random.choice(prompt_templates).format(flower=predicted_class), | |
| "image_size": "portrait_4_3" | |
| }, | |
| with_logs=True, | |
| on_queue_update=on_queue_update, | |
| ) | |
| image_url = result["images"][0]["url"] | |
| response = requests.get(image_url) | |
| generated_image = Image.open(io.BytesIO(response.content)) | |
| return classification_results, generated_image, str(wiki_url) | |
| # Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🌼 California Native Plant Classifier & AI Art Generator") | |
| with gr.Row(): | |
| input_image = gr.Image(type="pil", label="Upload a Photo", height=250) | |
| with gr.Row(): | |
| with gr.Column(): | |
| label_output = gr.Label(label="Classification Results") | |
| wiki_output = gr.Textbox(label="Wikipedia Link") | |
| generated_image = gr.Image(label="AI-Generated Artistic Interpretation") | |
| # Submit button to trigger image processing | |
| submit_btn = gr.Button("Submit") | |
| submit_btn.click(fn=process_image, inputs=input_image, outputs=[label_output, generated_image, wiki_output]) | |
| # Examples | |
| gr.Examples( | |
| examples=example_images, | |
| inputs=input_image, | |
| examples_per_page=6 | |
| ) | |
| demo.launch() | |