e1010101 commited on
Commit
cb531f9
1 Parent(s): 17745af

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForImageClassification
3
+ import torch
4
+ from PIL import Image
5
+
6
+ model_name = 'e1010101/vit-384-tongue-image'
7
+ processor = AutoProcessor.from_pretrained(model_name)
8
+ model = AutoModelForImageClassification.from_pretrained(model_name)
9
+
10
+ def classify_image(image):
11
+ inputs = processor(images=image, return_tensors="pt")
12
+ with torch.no_grad():
13
+ outputs = model(**inputs)
14
+ logits = outputs.logits
15
+ # Apply sigmoid for multi-label classification
16
+ probs = torch.sigmoid(logits)[0].numpy()
17
+ # Get label names
18
+ labels = model.config.id2label.values()
19
+ # Create a dictionary of labels and probabilities
20
+ result = {label: float(prob) for label, prob in zip(labels, probs)}
21
+ # Sort results by probability
22
+ result = dict(sorted(result.items(), key=lambda item: item[1], reverse=True))
23
+ return result
24
+
25
+ interface = gr.Interface(
26
+ fn=classify_image,
27
+ inputs=gr.inputs.Image(type="pil"),
28
+ outputs=gr.outputs.Label(num_top_classes=None),
29
+ title="Multi-Label Image Classification",
30
+ description="Upload an image to get classification results."
31
+ )
32
+
33
+
34
+ if __name__ == "__main__":
35
+ interface.launch()