Update app.py
Browse files12345678910111213141539 unmodified lines555657585960616263646566676848 unmodified lines117118119120import gradio as grfrom transformers import AutoTokenizer, AutoModelForSequenceClassification, pipelinefrom torch.nn.functional import sigmoidimport torch# Load text emotion modelmodel_name = "SamLowe/roberta-base-go_emotions"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForSequenceClassification.from_pretrained(model_name)# Load image emotion classification pipelineimage_emotion_pipeline = pipeline("image-classification", model="nateraw/ferplus-emo-resnet34")# Emotion label to icon mapping (subset)emotion_icons = {39 unmodified linesreturn ", ".join(icons) if icons else "No strong emotion detected."# Analyze image emotiondef analyze_image_emotion(image):if image is None:return "No image provided."results = image_emotion_pipeline(image)top = results[0]return f"{top['label']} ({top['score']:.2f})"# Combined analysisdef analyze_combined(text, threshold, image):text_result = get_emotions(text, threshold)48 unmodified linescss=custom_css)demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434439 unmodified lines84858687888948 unmodified lines138139140141import gradio as grfrom transformers import AutoTokenizer, AutoModelForSequenceClassificationfrom torch.nn.functional import sigmoidimport torchfrom PIL import Imagefrom torchvision import transformsimport requests# Load text emotion modelmodel_name = "SamLowe/roberta-base-go_emotions"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForSequenceClassification.from_pretrained(model_name)# Load image emotion model (fine-tuned ResNet-50)image_model_name = "Celal11/resnet-50-finetuned-FER2013CKPlus-0.003"image_emotion_model = AutoModelForSequenceClassification.from_pretrained(image_model_name)image_tokenizer = AutoTokenizer.from_pretrained("microsoft/resnet-50")# Transform for image preprocessingimage_transform = transforms.Compose([transforms.Resize((224, 224)),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])# FER labelsimage_labels = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral", "Contempt"]# Analyze image emotiondef analyze_image_emotion(image_path):if image_path is None:return "No image provided."image = Image.open(image_path).convert("RGB")img_tensor = image_transform(image).unsqueeze(0)with torch.no_grad():output = image_emotion_model(img_tensor)probs = sigmoid(output.logits)[0]top_idx = torch.argmax(probs).item()return f"{image_labels[top_idx]} ({probs[top_idx]:.2f})"# Emotion label to icon mapping (subset)emotion_icons = {39 unmodified linesreturn ", ".join(icons) if icons else "No strong emotion detected."# Combined analysisdef analyze_combined(text, threshold, image):text_result = get_emotions(text, threshold)48 unmodified linescss=custom_css)demo.launch()
app.py+33-12