File size: 1,627 Bytes
23e46f5 49238a6 89891d1 23e46f5 4aa2d44 23e46f5 8942940 20d158a 8d812a8 23e46f5 20d158a 49238a6 4aa2d44 20d158a 69f23c0 89891d1 20d158a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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() |