Kareem2806 commited on
Commit
2f5d512
·
1 Parent(s): a3ae227

Add app.py and requirements.txt from Colab

Browse files
Files changed (2) hide show
  1. app.py +48 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
4
+ import gradio as gr
5
+ from PIL import Image
6
+
7
+ # ✅ Ganti label sesuai dataset kamu
8
+ id2label = {
9
+ 0: "Angry",
10
+ 1: "Disgust",
11
+ 2: "Fear",
12
+ 3: "Happy",
13
+ 4: "Sad",
14
+ 5: "Surprise",
15
+ 6: "Neutral"
16
+ }
17
+
18
+ # ✅ Ganti checkpoint/model sesuai modelmu
19
+ checkpoint = "google/vit-base-patch16-224-in21k"
20
+ processor = AutoImageProcessor.from_pretrained(checkpoint)
21
+ model = AutoModelForImageClassification.from_pretrained(
22
+ checkpoint,
23
+ num_labels=7,
24
+ id2label=id2label,
25
+ label2id={v: k for k, v in id2label.items()}
26
+ )
27
+ model.eval()
28
+
29
+ def predict(img: Image.Image):
30
+ if img.mode != "RGB":
31
+ img = img.convert("RGB")
32
+ inputs = processor(images=img, return_tensors="pt").to(model.device)
33
+ with torch.no_grad():
34
+ outputs = model(**inputs)
35
+ logits = outputs.logits
36
+ probs = torch.nn.functional.softmax(logits, dim=1)[0]
37
+ return {id2label[i]: float(probs[i]) for i in range(len(probs))}
38
+
39
+ interface = gr.Interface(
40
+ fn=predict,
41
+ inputs=gr.Image(type="pil"),
42
+ outputs=gr.Label(num_top_classes=3),
43
+ title="Facial Expression Classifier",
44
+ description="Upload a face image and the model will predict the facial expression."
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+
2
+ gradio>=3.0
3
+ torch
4
+ torchvision
5
+ transformers
6
+ Pillow
7
+ numpy