Spaces:
Sleeping
Sleeping
updated app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
MODEL_ID = "nateraw/food"
|
| 6 |
clf = pipeline("image-classification", model=MODEL_ID)
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"""
|
| 12 |
-
if image is None:
|
| 13 |
-
return "Please upload an image."
|
| 14 |
-
|
| 15 |
-
# Perform classification
|
| 16 |
-
out = clf(image)
|
| 17 |
-
|
| 18 |
-
# Sort results and get top 3
|
| 19 |
out = sorted(out, key=lambda r: r["score"], reverse=True)[:3]
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# The ChatInterface expects a message, but we'll ignore it for this
|
| 29 |
-
# image classification task and use our image input instead.
|
| 30 |
-
# The "message" here will be None because we've set `gr.Image`
|
| 31 |
-
# as the input.
|
| 32 |
-
):
|
| 33 |
-
"""
|
| 34 |
-
This function handles the chatbot's response.
|
| 35 |
-
It receives the image and history, then calls our classification function.
|
| 36 |
-
"""
|
| 37 |
-
# The message input from the chatbot interface will be None here
|
| 38 |
-
# since our primary input is the image. We can get the image from `history`.
|
| 39 |
-
if not history:
|
| 40 |
-
return "" # Don't respond if there's no history or image
|
| 41 |
-
|
| 42 |
-
# Extract the image from the last user message in the history
|
| 43 |
-
# The input is the image, but the chatbot wraps it as a message
|
| 44 |
-
last_user_message = history[-1][0]
|
| 45 |
-
|
| 46 |
-
# We must call the classification function with the image
|
| 47 |
-
classification_result = classify_image(last_user_message)
|
| 48 |
-
|
| 49 |
-
# Yield the result as the chatbot's response
|
| 50 |
-
yield classification_result
|
| 51 |
-
|
| 52 |
-
# Create the ChatInterface
|
| 53 |
-
chatbot = gr.ChatInterface(
|
| 54 |
-
fn=respond,
|
| 55 |
-
# The input component must be specified for the user to upload an image.
|
| 56 |
-
# The message component will be hidden.
|
| 57 |
-
chatbot=gr.Chatbot(height=500),
|
| 58 |
-
textbox=gr.Textbox(
|
| 59 |
-
placeholder="Upload an image to get a classification!",
|
| 60 |
-
visible=False
|
| 61 |
-
),
|
| 62 |
-
# Use gr.Image for the user input
|
| 63 |
-
submit_btn="Classify Image",
|
| 64 |
-
clear_btn="Clear History",
|
| 65 |
-
additional_inputs=[
|
| 66 |
-
gr.Image(type="pil", label="Upload an Image"),
|
| 67 |
-
],
|
| 68 |
-
title="Image Classification Chatbot",
|
| 69 |
-
description="Upload an image and I'll classify it for you!",
|
| 70 |
-
examples=[]
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
# Launch the demo
|
| 74 |
-
if __name__ == "__main__":
|
| 75 |
-
chatbot.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
MODEL_ID = "nateraw/food" # <- replace with any model above
|
|
|
|
| 5 |
clf = pipeline("image-classification", model=MODEL_ID)
|
| 6 |
|
| 7 |
+
def predict(img):
|
| 8 |
+
out = clf(img)
|
| 9 |
+
# show top-3 with scores
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
out = sorted(out, key=lambda r: r["score"], reverse=True)[:3]
|
| 11 |
+
return {r["label"]: float(r["score"]) for r in out}
|
| 12 |
+
|
| 13 |
+
gr.Interface(
|
| 14 |
+
fn=predict,
|
| 15 |
+
inputs=gr.Image(type="pil", label="Upload image"),
|
| 16 |
+
outputs=gr.Label(num_top_classes=3),
|
| 17 |
+
title="Image Classifier (pre-tuned)",
|
| 18 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|