Luis Marques commited on
Commit
6edb3ba
1 Parent(s): a2aafa1

update app.py

Browse files
Files changed (2) hide show
  1. app.py +25 -10
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,19 +1,34 @@
1
  import gradio as gr
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
 
 
3
 
4
- model_name = "./model.pkl"
5
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
 
8
 
9
- def classify_text(text):
10
- inputs = tokenizer(text, return_tensors="pt")
11
- outputs = model(**inputs)
12
- predictions = outputs.logits.argmax(dim=1)
 
 
 
 
 
 
13
 
14
- return f"Predicted class: {predictions.item()}"
15
 
 
 
16
 
17
- iface = gr.Interface(fn=classify_text, inputs="text", outputs="text")
 
 
 
 
 
 
 
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
- transformers==4.13.0
 
 
1
  gradio==2.1.2
2
+ torch
3
+ torchvision