chinesemusk's picture
Update app.py
85343de verified
# import gradio as gr
# from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
# # Load model and tokenizer
# model_name = "chinesemusk/Deon_emotion-model-full"
# tokenizer_name = "chinesemusk/Deon-emotion-model-full"
# tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
# model = AutoModelForSequenceClassification.from_pretrained(model_name)
# # Map label IDs to readable names
# #label_names = [label for _, label in sorted(id2label.items(), key=lambda x: int(x[0]))]
# # Map label IDs to readable names
# id2label = model.config.id2label
# label_names = [id2label.get(str(i), id2label.get(i)) for i in range(len(id2label))]
# # Create inference pipeline
# classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=None)
# def classify_emotion(text):
# predictions = classifier(text)[0]
# # Sort by score descending
# sorted_preds = sorted(predictions, key=lambda x: x['score'], reverse=True)
# # Convert label IDs to names
# result = {id2label[x['label'].split('_')[-1]]: round(x['score'], 4) for x in sorted_preds}
# return result
# examples = [
# "I just got a new job and I'm so excited!",
# "I'm feeling really down today.",
# "I love spending time with my family.",
# "That movie was terrifying!",
# "I can't believe I won!",
# "He really pissed me off with his attitude."
# ]
# # Launch Gradio interface
# gr.Interface(
# fn=classify_emotion,
# inputs=gr.Textbox(lines=2, placeholder="Enter a sentence to analyze emotion..."),
# outputs=gr.Label(num_top_classes=6),
# examples=examples,
# title="Deon Emotion Classifier",
# description="Classify text into one of 6 emotions: joy, sadness, anger, fear, love, and surprise. Powered by DistilBERT fine-tuned on emotion data.",
# theme="default"
# ).launch()
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
# Load once globally
model_name = "chinesemusk/Deon_emotion-model-full"
tokenizer_name = "chinesemusk/Deon-emotion-model-full"
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Labels
labels = ["sadness", "joy", "love", "anger", "fear", "surprise"]
def classify_emotion(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
logits = model(**inputs).logits
probs = torch.nn.functional.softmax(logits, dim=1)[0]
return {labels[i]: float(probs[i]) for i in range(len(labels))}
# Gradio Interface
demo = gr.Interface(
fn=classify_emotion,
inputs=gr.Textbox(placeholder="Enter a sentence...", lines=2),
outputs=gr.Label(num_top_classes=6),
examples=[
["I feel amazing today!"],
["Why does this always happen to me?"],
["I'm terrified of what’s coming"],
["I’m deeply in love with her"],
["Nothing makes sense anymore"],
],
title="Deon Emotion Detector",
description="A fine-tuned DistilBERT model to classify text into six emotions: sadness, joy, love, anger, fear, and surprise.",
allow_flagging="never"
)
demo.launch()