Spaces:
Sleeping
Sleeping
File size: 3,614 Bytes
e8746a9 9cca2b0 69de931 da3ef81 cc03be5 69de931 c7b6a4c 69de931 c7b6a4c 69de931 e8746a9 c7b6a4c da3ef81 d3e55a2 da3ef81 c7b6a4c d4ccb5d cc03be5 b6f9430 d4ccb5d 9cca2b0 c7b6a4c ddf33a1 e8746a9 c7b6a4c 69de931 d4ccb5d e9c62c9 c7b6a4c 5c93b6c da3ef81 e9c62c9 5c93b6c c7b6a4c da3ef81 d4ccb5d c7b6a4c 9cca2b0 c7b6a4c 69de931 c7b6a4c 69de931 c7b6a4c 69de931 c7b6a4c e211546 c7b6a4c ef50b14 c7b6a4c 69de931 ef50b14 e8746a9 c7b6a4c |
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 |
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 your model
learn = load_learner('resnet50_30_categories.pkl')
# Wikipedia links
search_terms_wikipedia = {
"blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
"bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
# ... (same as before)
"goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
}
# Prompt templates for art generation
prompt_templates = [
"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.",
"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.",
"An artist's nature journal page featuring a detailed {flower} study, with delicate ink lines and soft watercolor washes, complete with small sketches of bees and field notes in the margins.",
"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.",
"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."
]
# Example image paths (replace with actual paths on your system or Hugging Face space)
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'))
]
def on_queue_update(update):
if isinstance(update, fal_client.InProgress):
for log in update.logs:
print(log["message"])
else:
print("Received non-InProgress update:", update)
# Processing function
def process_image(img):
predicted_class, _, probs = learn.predict(img)
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
# Wikipedia
wiki_url = search_terms_wikipedia.get(predicted_class.lower(), "No Wikipedia entry found.")
# Generate image via FAL
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, wiki_url
# Interface
with gr.Blocks() as demo:
gr.Markdown("# 🌼 Wildflower Classifier & Artistic Generator")
with gr.Row():
input_image = gr.Image(height=230, width=230, label="Upload an image", type="pil")
with gr.Row():
with gr.Column():
label_output = gr.Label(label="Prediction")
wiki_output = gr.Textbox(label="Wikipedia Link")
generated_image = gr.Image(label="AI Artistic Interpretation")
gr.Examples(
examples=example_images,
inputs=input_image,
examples_per_page=6
)
input_image.upload(
fn=process_image,
inputs=input_image,
outputs=[label_output, generated_image, wiki_output]
)
demo.launch()
|