from transformers import AutoTokenizer, AutoModelForSequenceClassification import numpy as np import torch import gradio as gr labels = ['sadness', 'joy','love', 'anger','fear', 'surprise'] device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model_name = "abdulmatinomotoso/emotion_detection_finetuned_distilbert" model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device) tokenizer = AutoTokenizer.from_pretrained(model_name) def get_emotion(text): input_tensor = tokenizer.encode(text, return_tensors="pt").to("cuda") logits = model(input_tensor).logits softmax = torch.nn.Softmax(dim=1) probs = softmax(logits)[0] probs = probs.cpu().detach().numpy() max_index = np.argmax(probs) emotion = labels[max_index] return emotion demo = gr.Interface(get_emotion, inputs='text', outputs="text", title = "Emotion Detection") if __name__ == "__main__": demo.launch(debug=True)