Spaces:
Sleeping
Sleeping
Luis Marques
commited on
Commit
•
6edb3ba
1
Parent(s):
a2aafa1
update app.py
Browse files- app.py +25 -10
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,19 +1,34 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
model
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
return f"Predicted class: {predictions.item()}"
|
15 |
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import torchvision.transforms as transforms
|
5 |
|
6 |
+
model = torch.load("model.pkl", map_location=torch.device("cpu"))
|
7 |
+
model.eval()
|
|
|
8 |
|
9 |
|
10 |
+
def preprocess_image(image):
|
11 |
+
transform = transforms.Compose(
|
12 |
+
[
|
13 |
+
transforms.Resize((224, 224)),
|
14 |
+
transforms.ToTensor(),
|
15 |
+
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
|
16 |
+
]
|
17 |
+
)
|
18 |
+
image_tensor = transform(image)
|
19 |
+
return image_tensor.unsqueeze(0)
|
20 |
|
|
|
21 |
|
22 |
+
def classify_image(image):
|
23 |
+
input_tensor = preprocess_image(image)
|
24 |
|
25 |
+
with torch.no_grad():
|
26 |
+
outputs = model(input_tensor)
|
27 |
+
predictions = torch.argmax(outputs, dim=1).item()
|
28 |
+
|
29 |
+
return f"Predicted class: {predictions}"
|
30 |
+
|
31 |
+
|
32 |
+
iface = gr.Interface(fn=classify_image, inputs="image", outputs="text")
|
33 |
|
34 |
iface.launch()
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
gradio==2.1.2
|
2 |
-
|
|
|
|
1 |
gradio==2.1.2
|
2 |
+
torch
|
3 |
+
torchvision
|