File size: 6,105 Bytes
e8746a9 9cca2b0 69de931 da3ef81 69de931 d4ccb5d 69de931 e8746a9 d4ccb5d da3ef81 daa8242 da3ef81 d4ccb5d 9cca2b0 e8746a9 d4ccb5d 69de931 d4ccb5d e9c62c9 69de931 d4ccb5d 69de931 d4ccb5d 5c93b6c da3ef81 e9c62c9 5c93b6c d4ccb5d da3ef81 5c93b6c 69de931 e8746a9 d4ccb5d 9cca2b0 d4ccb5d 69de931 d4ccb5d 69de931 6200397 d4ccb5d 69de931 d4ccb5d 6200397 d4ccb5d 6200397 e9c62c9 6200397 69de931 d4ccb5d 69de931 d4ccb5d e8746a9 d4ccb5d 69de931 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
from fastai.vision.all import *
import gradio as gr
import fal_client
from PIL import Image
import io
import random
import requests
# Dictionary of plant names and their Wikipedia links
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",
"delphiniumr": "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"
}
# Templates for AI image generation
prompt_templates = [
"A delicate watercolor painting of a {flower} with morning dew drops, soft pastel colors blending together as the sunrise creates gentle shadows.",
"A whimsical nature journal sketch of a {flower} surrounded by butterflies and bees, with loose watercolor washes in fresh spring colors.",
"An impressionistic scene of a {flower} swaying in the morning breeze, painted in loose brushstrokes with soft blues and golden morning light.",
"A botanical illustration of a {flower} with detailed pencil lines and gentle watercolor washes, surrounded by notes about its features and habitat.",
"A dreamy plein air painting of a {flower} along a morning hiking trail, with misty mountains in the background and soft morning colors."
]
# Example images for the interface
example_images = [
'https://www.deserthorizonnursery.com/wp-content/uploads/2024/03/Brittlebush-Encelia-Farinosa-desert-horizon-nursery.jpg',
'https://cdn.mos.cms.futurecdn.net/VJE7gSuQ9KWbkqEsWgX5zS.jpg',
'https://www.parksconservancy.org/sites/default/files/styles/basic/public/A_GEN_131213_WTE_109.jpg?itok=9SDtr4b2',
'https://silverfallsseed.com/wp-content/uploads/2016/01/tidy-tips-_-9.jpg',
'https://valleywaternews.org/wp-content/uploads/2016/06/ceanothus-clusters.jpg?w=1440',
'https://cdn11.bigcommerce.com/s-1b9100svju/images/stencil/1280x1280/products/2044/1440/DETA-635__75522.1664817787.jpg?c=1'
]
# Function to handle AI generation progress updates
def on_queue_update(update):
if isinstance(update, fal_client.InProgress):
for log in update.logs:
print(log["message"])
# Main function to process the uploaded image
def process_image(img):
# Classify the image
predicted_class, _, probs = learn.predict(img)
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
# Get Wikipedia link
wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
# Generate artistic interpretation by calling the Flux 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,
)
# Get the generated image
image_url = result['images'][0]['url']
response = requests.get(image_url)
generated_image = Image.open(io.BytesIO(response.content))
return classification_results, generated_image, wiki_url
# Function to clear all outputs
def clear_outputs():
return {
label_output: None,
generated_image: None,
wiki_output: None
}
# Load the AI model
learn = load_learner('export.pkl')
# Create the web interface
with gr.Blocks() as demo:
# Input section
with gr.Row():
input_image = gr.Image(height=192, width=192, label="Upload Image for Classification", type="pil")
# Output section
with gr.Row():
with gr.Column():
label_output = gr.Label(label="Classification Results")
wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
generated_image = gr.Image(label="AI Generated Interpretation")
# Add example images
gr.Examples(
examples=example_images,
inputs=input_image,
examples_per_page=6,
fn=process_image,
outputs=[label_output, generated_image, wiki_output]
)
# Set up what happens when an image is uploaded or removed
input_image.change(
fn=process_image,
inputs=input_image,
outputs=[label_output, generated_image, wiki_output]
)
input_image.clear(
fn=clear_outputs,
inputs=[],
outputs=[label_output, generated_image, wiki_output]
)
# Start the application
demo.launch(inline=False) |