Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
+
|
| 5 |
+
model_id = "microsoft/food101-resnet50"
|
| 6 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 7 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
| 8 |
+
|
| 9 |
+
def classify_food(image):
|
| 10 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 11 |
+
outputs = model(**inputs)
|
| 12 |
+
logits = outputs.logits
|
| 13 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 14 |
+
return model.config.id2label[predicted_class_idx]
|
| 15 |
+
|
| 16 |
+
interface = gr.Interface(
|
| 17 |
+
fn=classify_food,
|
| 18 |
+
inputs=gr.Image(type="pil"),
|
| 19 |
+
outputs=gr.Textbox(),
|
| 20 |
+
title="Food Image Classification",
|
| 21 |
+
description="Upload a food image and the model will classify it."
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
interface.launch()
|