Update app.py
Browse files
app.py
CHANGED
|
@@ -37,6 +37,39 @@ search_terms_wikipedia = {
|
|
| 37 |
"goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
|
| 38 |
}
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Templates for AI image generation
|
| 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.",
|
|
@@ -75,8 +108,10 @@ def get_status(flower_name):
|
|
| 75 |
|
| 76 |
# Main function to process the uploaded image
|
| 77 |
def process_image(img):
|
| 78 |
-
|
| 79 |
predicted_class, _, probs = learn.predict(img)
|
|
|
|
|
|
|
| 80 |
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
|
| 81 |
|
| 82 |
# Get Wikipedia link
|
|
@@ -84,8 +119,10 @@ def process_image(img):
|
|
| 84 |
|
| 85 |
# Get endangerment status
|
| 86 |
endangerment_status = get_status(predicted_class)
|
| 87 |
-
|
|
|
|
| 88 |
# Generate artistic interpretation
|
|
|
|
| 89 |
result = fal_client.subscribe(
|
| 90 |
"fal-ai/flux/schnell",
|
| 91 |
arguments={
|
|
@@ -95,15 +132,19 @@ def process_image(img):
|
|
| 95 |
with_logs=True,
|
| 96 |
on_queue_update=on_queue_update,
|
| 97 |
)
|
| 98 |
-
|
|
|
|
| 99 |
# Retrieve image
|
| 100 |
image_url = result['images'][0]['url']
|
|
|
|
| 101 |
response = requests.get(image_url)
|
| 102 |
generated_image = Image.open(io.BytesIO(response.content))
|
| 103 |
|
|
|
|
| 104 |
return classification_results, generated_image, wiki_url, endangerment_status
|
| 105 |
|
| 106 |
|
|
|
|
| 107 |
# Function to clear all outputs
|
| 108 |
def clear_outputs():
|
| 109 |
return {
|
|
|
|
| 37 |
"goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
|
| 38 |
}
|
| 39 |
|
| 40 |
+
flowers_endangerment = {
|
| 41 |
+
"Blazing Star": "Not considered endangered.",
|
| 42 |
+
"Bristlecone Pine": "Least Concern (stable population).",
|
| 43 |
+
"California Bluebell": "Not listed as endangered or threatened.",
|
| 44 |
+
"California Buckeye": "Not endangered.",
|
| 45 |
+
"California Buckwheat": "Generally secure.",
|
| 46 |
+
"California Fuchsia": "Not endangered overall; some subspecies at risk.",
|
| 47 |
+
"California Checkerbloom": "Not generally endangered; some subspecies critically imperiled.",
|
| 48 |
+
"California Lilac": "Most species not endangered; some species are endangered.",
|
| 49 |
+
"California Poppy": "Generally secure; some subspecies face threats.",
|
| 50 |
+
"California Sagebrush": "Considered secure (G4-G5).",
|
| 51 |
+
"California Wild Grape": "Apparently secure (G4).",
|
| 52 |
+
"California Wild Rose": "Secure (G4).",
|
| 53 |
+
"Coyote Mint": "Varies by species; some federally listed as endangered.",
|
| 54 |
+
"Elegant Clarkia": "Secure (G5).",
|
| 55 |
+
"Baby Blue Eyes": "Secure.",
|
| 56 |
+
"Hummingbird Sage": "Apparently secure (G4).",
|
| 57 |
+
"Delphinium": "Varies by species; some are endangered.",
|
| 58 |
+
"Matilija Poppy": "Not currently endangered.",
|
| 59 |
+
"Blue-Eyed Grass": "Not endangered.",
|
| 60 |
+
"Penstemon Spectabilis": "Not endangered.",
|
| 61 |
+
"Seaside Daisy": "Not endangered.",
|
| 62 |
+
"Sticky Monkeyflower": "Not endangered.",
|
| 63 |
+
"Tidy Tips": "Generally not endangered; some subspecies may be at risk.",
|
| 64 |
+
"Wild Cucumber": "Generally not endangered.",
|
| 65 |
+
"Douglas Iris": "Not endangered.",
|
| 66 |
+
"Goldfields Coreopsis": "Varies by species; many not endangered."
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
def get_status(flower_name):
|
| 70 |
+
"""Return the endangerment status of a given flower name."""
|
| 71 |
+
return flowers_endangerment.get(flower_name, "Flower not found in database.")
|
| 72 |
+
|
| 73 |
# Templates for AI image generation
|
| 74 |
prompt_templates = [
|
| 75 |
"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.",
|
|
|
|
| 108 |
|
| 109 |
# Main function to process the uploaded image
|
| 110 |
def process_image(img):
|
| 111 |
+
print("Starting prediction...")
|
| 112 |
predicted_class, _, probs = learn.predict(img)
|
| 113 |
+
print(f"Prediction complete: {predicted_class}")
|
| 114 |
+
|
| 115 |
classification_results = dict(zip(learn.dls.vocab, map(float, probs)))
|
| 116 |
|
| 117 |
# Get Wikipedia link
|
|
|
|
| 119 |
|
| 120 |
# Get endangerment status
|
| 121 |
endangerment_status = get_status(predicted_class)
|
| 122 |
+
print(f"Status found: {endangerment_status}")
|
| 123 |
+
|
| 124 |
# Generate artistic interpretation
|
| 125 |
+
print("Sending request to FAL API...")
|
| 126 |
result = fal_client.subscribe(
|
| 127 |
"fal-ai/flux/schnell",
|
| 128 |
arguments={
|
|
|
|
| 132 |
with_logs=True,
|
| 133 |
on_queue_update=on_queue_update,
|
| 134 |
)
|
| 135 |
+
print("FAL API responded")
|
| 136 |
+
|
| 137 |
# Retrieve image
|
| 138 |
image_url = result['images'][0]['url']
|
| 139 |
+
print(f"Image URL: {image_url}")
|
| 140 |
response = requests.get(image_url)
|
| 141 |
generated_image = Image.open(io.BytesIO(response.content))
|
| 142 |
|
| 143 |
+
print("Image retrieved and ready to return")
|
| 144 |
return classification_results, generated_image, wiki_url, endangerment_status
|
| 145 |
|
| 146 |
|
| 147 |
+
|
| 148 |
# Function to clear all outputs
|
| 149 |
def clear_outputs():
|
| 150 |
return {
|