Spaces:
Sleeping
Sleeping
File size: 2,489 Bytes
8686317 |
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 46 47 48 49 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import gradio as gr
import numpy as np
# Specify the name of the pretrained sentiment analysis model
MODEL_NAME = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
# Load the tokenizer associated with the model (converts text to tokens)
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
# Load the pretrained model for sequence classification (sentiment analysis)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
# Labels corresponding to the model's output classes in Georgian language
labels = ['ααααα’αα£α α', 'αααα’α ααα£α α', 'ααααα’αα£α α']
# Define a function to classify sentiment of the input text
def classify_sentiment(text):
# Tokenize input text and convert to PyTorch tensors, truncate if too long
inputs = tokenizer(text, return_tensors="pt", truncation=True)
# Disable gradient calculations for inference
with torch.no_grad():
# Pass the tokens through the model to get output logits
outputs = model(**inputs)
logits = outputs.logits
# Convert logits to probabilities using softmax function
probs = torch.nn.functional.softmax(logits, dim=1).numpy()[0]
# Find the label with the highest probability
top_label = labels[np.argmax(probs)]
confidence = np.max(probs)
# Return a dictionary with all labels and their probabilities for display
return {labels[i]: float(probs[i]) for i in range(len(labels))}
# Set up a Gradio interface to interact with the classification function
iface = gr.Interface(
fn=classify_sentiment, # function to run when input is given
inputs=gr.Textbox(lines=3, placeholder="α¨ααα§ααααα α’ααα’α ..."), # multi-line textbox for input text
outputs=gr.Label(num_top_classes=3), # output: show all three sentiment labels with probabilities
title="Twitter-αα‘ αααα¬α§αααα‘ αααα‘αα€αααα’αα α", # title of the interface in Georgian
description="αα§ααααα‘ CardiffNLP-αα‘ αα αααααααααα RoBERTa αααααα‘ α’ααα’αααα‘ ααααααα, αααα’α ααα£α αα α£αα α§αα€αααα αααα‘αα€ααͺαα αααα‘αααα‘." # description in Georgian
)
# Launch the Gradio app with public sharing enabled
iface.launch(share=True)
|