KabeerAmjad
commited on
Commit
•
9f320da
1
Parent(s):
34d6afa
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load your Hugging Face model
|
7 |
+
model_id = "KabeerAmjad/food_classification_model" # Replace with your actual model ID
|
8 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
9 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
10 |
+
|
11 |
+
# Define the prediction function
|
12 |
+
def classify_image(img):
|
13 |
+
inputs = feature_extractor(images=img, return_tensors="pt")
|
14 |
+
with torch.no_grad():
|
15 |
+
outputs = model(**inputs)
|
16 |
+
probs = torch.softmax(outputs.logits, dim=-1)
|
17 |
+
|
18 |
+
# Get the label with the highest probability
|
19 |
+
top_label = model.config.id2label[probs.argmax().item()]
|
20 |
+
return top_label
|
21 |
+
|
22 |
+
# Create the Gradio interface
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=classify_image,
|
25 |
+
inputs=gr.Image(type="pil"),
|
26 |
+
outputs="text",
|
27 |
+
title="Food Image Classification",
|
28 |
+
description="Upload an image to classify if it’s an apple pie, etc."
|
29 |
+
)
|
30 |
+
|
31 |
+
# Launch the app
|
32 |
+
iface.launch()
|