Update app.py
Browse files
app.py
CHANGED
|
@@ -62,6 +62,17 @@ def on_queue_update(update):
|
|
| 62 |
for log in update.logs:
|
| 63 |
print(log["message"])
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# Main function to process the uploaded image
|
| 66 |
def process_image(img):
|
| 67 |
# Classify the image
|
|
@@ -71,7 +82,10 @@ def process_image(img):
|
|
| 71 |
# Get Wikipedia link
|
| 72 |
wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
|
| 73 |
|
| 74 |
-
#
|
|
|
|
|
|
|
|
|
|
| 75 |
result = fal_client.subscribe(
|
| 76 |
"fal-ai/flux/schnell",
|
| 77 |
arguments={
|
|
@@ -82,21 +96,24 @@ def process_image(img):
|
|
| 82 |
on_queue_update=on_queue_update,
|
| 83 |
)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
image_url = result['images'][0]['url']
|
| 87 |
response = requests.get(image_url)
|
| 88 |
generated_image = Image.open(io.BytesIO(response.content))
|
| 89 |
|
| 90 |
-
return classification_results, generated_image, wiki_url
|
|
|
|
| 91 |
|
| 92 |
# Function to clear all outputs
|
| 93 |
def clear_outputs():
|
| 94 |
return {
|
| 95 |
label_output: None,
|
| 96 |
generated_image: None,
|
| 97 |
-
wiki_output: None
|
|
|
|
| 98 |
}
|
| 99 |
|
|
|
|
| 100 |
# Load the AI model
|
| 101 |
learn = load_learner('resnet50_30_categories.pkl')
|
| 102 |
|
|
@@ -107,33 +124,35 @@ with gr.Blocks() as demo:
|
|
| 107 |
input_image = gr.Image(height=230, width=230, label="Upload Image for Classification", type="pil")
|
| 108 |
|
| 109 |
# Output section
|
|
|
|
| 110 |
with gr.Row():
|
| 111 |
with gr.Column():
|
| 112 |
label_output = gr.Label(label="Classification Results")
|
| 113 |
wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
|
|
|
|
| 114 |
generated_image = gr.Image(label="AI Generated Interpretation")
|
| 115 |
-
|
| 116 |
# Add example images using local paths
|
| 117 |
gr.Examples(
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# Set up what happens when an image is uploaded or removed
|
| 126 |
input_image.change(
|
| 127 |
fn=process_image,
|
| 128 |
inputs=input_image,
|
| 129 |
-
outputs=[label_output, generated_image, wiki_output]
|
| 130 |
-
|
| 131 |
-
|
| 132 |
input_image.clear(
|
| 133 |
fn=clear_outputs,
|
| 134 |
inputs=[],
|
| 135 |
-
outputs=[label_output, generated_image, wiki_output]
|
| 136 |
-
|
|
|
|
| 137 |
|
| 138 |
# Start the application
|
| 139 |
demo.launch(inline=False)
|
|
|
|
| 62 |
for log in update.logs:
|
| 63 |
print(log["message"])
|
| 64 |
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def get_status(flower_name):
|
| 68 |
+
"""Return the endangerment status of a given flower name."""
|
| 69 |
+
# Normalize input for dictionary lookup
|
| 70 |
+
normalized_name = flower_name.title()
|
| 71 |
+
return flowers_endangerment.get(normalized_name, "Flower not found in database.")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
# Main function to process the uploaded image
|
| 77 |
def process_image(img):
|
| 78 |
# Classify the image
|
|
|
|
| 82 |
# Get Wikipedia link
|
| 83 |
wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
|
| 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={
|
|
|
|
| 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 {
|
| 110 |
label_output: None,
|
| 111 |
generated_image: None,
|
| 112 |
+
wiki_output: None,
|
| 113 |
+
status_output: None # ← NEW
|
| 114 |
}
|
| 115 |
|
| 116 |
+
|
| 117 |
# Load the AI model
|
| 118 |
learn = load_learner('resnet50_30_categories.pkl')
|
| 119 |
|
|
|
|
| 124 |
input_image = gr.Image(height=230, width=230, label="Upload Image for Classification", type="pil")
|
| 125 |
|
| 126 |
# Output section
|
| 127 |
+
# Output section
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column():
|
| 130 |
label_output = gr.Label(label="Classification Results")
|
| 131 |
wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
|
| 132 |
+
status_output = gr.Textbox(label="Endangerment Status", lines=1) # ← NEW
|
| 133 |
generated_image = gr.Image(label="AI Generated Interpretation")
|
| 134 |
+
|
| 135 |
# Add example images using local paths
|
| 136 |
gr.Examples(
|
| 137 |
+
examples=example_images,
|
| 138 |
+
inputs=input_image,
|
| 139 |
+
examples_per_page=6,
|
| 140 |
+
fn=process_image,
|
| 141 |
+
outputs=[label_output, generated_image, wiki_output, status_output] # ← UPDATED
|
| 142 |
+
)
|
| 143 |
+
|
|
|
|
| 144 |
input_image.change(
|
| 145 |
fn=process_image,
|
| 146 |
inputs=input_image,
|
| 147 |
+
outputs=[label_output, generated_image, wiki_output, status_output] # ← UPDATED
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
input_image.clear(
|
| 151 |
fn=clear_outputs,
|
| 152 |
inputs=[],
|
| 153 |
+
outputs=[label_output, generated_image, wiki_output, status_output] # ← UPDATED
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
|
| 157 |
# Start the application
|
| 158 |
demo.launch(inline=False)
|