cs751 / app.py
fajarah's picture
Update app.py
20d158a verified
raw
history blame
1.63 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoImageProcessor, AutoModelForImageClassification
from torch.nn.functional import sigmoid
import torch
from PIL import Image
# Load text emotion model
model_name = "SamLowe/roberta-base-go_emotions"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Load image emotion model
image_model_name = "Celal11/resnet-50-finetuned-FER2013-0.001"
image_processor = AutoImageProcessor.from_pretrained(image_model_name)
image_model = AutoModelForImageClassification.from_pretrained(image_model_name)
# Analyze image emotion using processor and model
def analyze_image_emotion(image):
if image is None:
return "No image provided."
inputs = image_processor(images=image, return_tensors="pt")
with torch.no_grad():
logits = image_model(**inputs).logits
probs = torch.nn.functional.softmax(logits, dim=1)[0]
pred_idx = torch.argmax(probs).item()
label = image_model.config.id2label[pred_idx]
score = probs[pred_idx].item()
return f"{label} ({score:.2f})"
# Emotion label to icon mapping (subset)
emotion_icons = {
inputs=[
gr.Textbox(lines=5, placeholder="Write a sentence or a full paragraph...", label="Your Text"),
gr.Slider(minimum=0.1, maximum=0.9, value=0.3, step=0.05, label="Threshold"),
gr.Image(type="pil", label="Upload Face Photo")
],
outputs=[
gr.Textbox(label="Detected Text Emotions", elem_classes=["output-textbox"]),
css=custom_css
)
demo.launch()