Rishithjk commited on
Commit
767abf3
·
verified ·
1 Parent(s): c6ba449

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -1,24 +1,28 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- # Load emotion detection model
5
- emotion_pipe = pipeline("image-classification", model="dima806/facial_emotions_image_detection")
 
6
 
 
7
  def detect_emotion(image):
8
- results = emotion_pipe(image)
9
- if results:
10
- top = results[0]
11
- return {
12
- "top_prediction": f"{top['label']} ({100*top['score']:.1f}%)",
13
- "all_predictions": results
14
- }
15
- return {"top_prediction": "No face detected", "all_predictions": []}
16
 
17
- # Create Gradio interface
18
- gr.Interface(
19
  fn=detect_emotion,
20
  inputs=gr.Image(type="pil"),
21
- outputs="json", # Changed from "text" to "json"
22
- title="High Accuracy Emotion Detector",
23
- description="Powered by dima806/facial_emotions_image_detection (~91% accurate)"
24
- ).launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
3
+ import torch
4
+ from PIL import Image
5
 
6
+ # Load model and processor
7
+ processor = AutoImageProcessor.from_pretrained("dima806/facial_emotions_image_detection")
8
+ model = AutoModelForImageClassification.from_pretrained("dima806/facial_emotions_image_detection")
9
 
10
+ # Function to predict emotion
11
  def detect_emotion(image):
12
+ inputs = processor(images=image, return_tensors="pt")
13
+ with torch.no_grad():
14
+ outputs = model(**inputs)
15
+ logits = outputs.logits
16
+ predicted_class = logits.argmax(-1).item()
17
+ return model.config.id2label[predicted_class]
 
 
18
 
19
+ # Gradio interface
20
+ iface = gr.Interface(
21
  fn=detect_emotion,
22
  inputs=gr.Image(type="pil"),
23
+ outputs="text",
24
+ title="Facial Emotion Detection",
25
+ description="Upload a face image to detect emotion using dima806 model"
26
+ )
27
+
28
+ iface.launch(share=True)