Mohammadreza Ghaffarzadeh
commited on
Commit
•
b174fde
1
Parent(s):
82c3910
finito
Browse files- app.py +15 -6
- requirements.txt +2 -0
app.py
CHANGED
@@ -2,6 +2,8 @@ import numpy as np
|
|
2 |
import gradio as gr
|
3 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
4 |
from PIL import Image
|
|
|
|
|
5 |
|
6 |
|
7 |
|
@@ -9,20 +11,27 @@ processor = AutoImageProcessor.from_pretrained("Moreza009/HF_CVcourse_FoodClassi
|
|
9 |
model = AutoModelForImageClassification.from_pretrained("Moreza009/HF_CVcourse_FoodClassifier")
|
10 |
|
11 |
def classifier(image):
|
12 |
-
image
|
|
|
|
|
13 |
inputs = processor(images=image, return_tensors="pt")
|
14 |
outputs = model(**inputs)
|
15 |
logits = outputs.logits
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
food = gr.Interface(
|
21 |
fn=classifier,
|
22 |
-
inputs=gr.Image(),
|
23 |
-
outputs="
|
24 |
title = "what's your eating?",
|
25 |
-
description = "
|
26 |
)
|
27 |
|
|
|
28 |
food.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
4 |
from PIL import Image
|
5 |
+
import torch
|
6 |
+
import pandas as pd
|
7 |
|
8 |
|
9 |
|
|
|
11 |
model = AutoModelForImageClassification.from_pretrained("Moreza009/HF_CVcourse_FoodClassifier")
|
12 |
|
13 |
def classifier(image):
|
14 |
+
if isinstance(image, np.ndarray):
|
15 |
+
image = Image.fromarray(image)
|
16 |
+
#image = Image.open(image)
|
17 |
inputs = processor(images=image, return_tensors="pt")
|
18 |
outputs = model(**inputs)
|
19 |
logits = outputs.logits
|
20 |
+
probabilities = torch.nn.functional.softmax(logits, dim=-1)
|
21 |
+
predicted_class_idxs = probabilities.topk(5, dim=-1)[1].tolist()[0]
|
22 |
+
probabilities = probabilities.tolist()[0][:5]
|
23 |
+
classes = [model.config.id2label[idx] for idx in predicted_class_idxs]
|
24 |
+
df = pd.DataFrame({'food':classes , 'posibility': probabilities})
|
25 |
+
return df.to_html(index=False)
|
26 |
|
27 |
|
28 |
food = gr.Interface(
|
29 |
fn=classifier,
|
30 |
+
inputs=gr.Image(type="pil"),
|
31 |
+
outputs="html",
|
32 |
title = "what's your eating?",
|
33 |
+
description = " :) "
|
34 |
)
|
35 |
|
36 |
+
|
37 |
food.launch()
|
requirements.txt
CHANGED
@@ -2,4 +2,6 @@ torch
|
|
2 |
transformers
|
3 |
gradio
|
4 |
pillow
|
|
|
|
|
5 |
numpy
|
|
|
2 |
transformers
|
3 |
gradio
|
4 |
pillow
|
5 |
+
numpy
|
6 |
+
pandas
|
7 |
numpy
|