SEARO1 commited on
Commit
c4063c6
·
verified ·
1 Parent(s): a07a4bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -0
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
3
+ from PIL import Image
4
+ import torch
5
+
6
+ model_id = "SEAR01/FER_model" # 你的模型 ID
7
+ processor = AutoImageProcessor.from_pretrained(model_id)
8
+ model = AutoModelForImageClassification.from_pretrained(model_id)
9
+ emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise']
10
+
11
+ def predict_emotion(image):
12
+ inputs = processor(images=image, return_tensors="pt")
13
+ with torch.no_grad():
14
+ outputs = model(**inputs)
15
+ predicted = outputs.logits.argmax(-1).item()
16
+ emotion = emotion_labels[predicted]
17
+ return f"Detected: {emotion}"
18
+
19
+ iface = gr.Interface(fn=predict_emotion, inputs=gr.Image(type="pil"), outputs="text")
20
+ iface.launch()