Spaces:
Build error
Build error
| from transformers import AutoModelForImageClassification, AutoProcessor | |
| from PIL import Image | |
| import torch | |
| import numpy as np | |
| import os | |
| import cv2 | |
| from textblob import TextBlob | |
| model_name = "gerhardien/face-emotion" | |
| # Load the model and processor | |
| model = AutoModelForImageClassification.from_pretrained(model_name) | |
| processor = AutoProcessor.from_pretrained(model_name) | |
| def detect_emotion(image_path): | |
| img = cv2.imread(image_path) | |
| inputs = processor(images=img, return_tensors="pt") | |
| # Run inference | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| predicted_class_idx = torch.argmax(logits, dim=1).item() | |
| label = model.config.id2label[predicted_class_idx] | |
| return label | |
| def detect_sentiment(text): | |
| analysis = TextBlob(text) | |
| return "Positive" if analysis.sentiment.polarity > 0 else "Negative" | |