Sentiment / app.py
buzzcraft's picture
Update app.py
4ef3a03 verified
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
import numpy as np
from scipy.special import softmax
import gradio as gr
torch.cuda.is_available()
model_path = "ltg/norbert3-base_sentence-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForSequenceClassification.from_pretrained(model_path, trust_remote_code=True)
def sentiment_analysis(text):
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
scores_ = output[0][0].detach().numpy()
scores_ = softmax(scores_)
labels = ['Negativ', 'Positiv', 'Nøytral']
scores = {l: float(s) for (l, s) in zip(labels, scores_)}
return scores
demo = gr.Interface(
theme=gr.themes.Base(),
fn=sentiment_analysis,
inputs=gr.Textbox(placeholder="Write your text here..."),
outputs="label",
examples=[
["Woho, jeg fikk meg ny jobb!"],
["Jeg skal jobbe med løver i den nye jobben min."],
["Oj, en løve spiste den ene armen min.. Snakk om HMS :("],
["På vei til sykehus.. Ønsk meg lykke til.."],
["Supert! De må pokkern meg amputere hele armen.."],
["Våkna opp fra operasjon, fått en robot arm. Im now terminator! Super opplevelse 10 av 10.."]
],
title='Sentiment Analysis App',
description='This app classifies a positive, neutral, or negative sentiment.'
)
demo.launch()