Fanny1366's picture
Update app.py
1b3da24 verified
raw
history blame contribute delete
1.89 kB
import streamlit as st
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
from keybert import KeyBERT
import numpy as np
import torch
from evaluate import load
# Load sentiment analysis model and word divider
try:
model = AutoModelForSequenceClassification.from_pretrained(
"tabularisai/multilingual-sentiment-analysis",
num_labels=5
)
tokenizer = AutoTokenizer.from_pretrained(
"tabularisai/multilingual-sentiment-analysis"
)
sentiment_pipeline = pipeline(
"text-classification",
model=model,
tokenizer=tokenizer
)
except Exception as e:
st.error(f"Error loading sentiment analysis model: {e}")
# Load keyword extraction model
try:
kw_model = KeyBERT()
except Exception as e:
st.error(f"Error loading sentiment analysis model: {e}")
# Define tag mapping
label_map = {
"Very Negative": 0,
"Negative": 1,
"Neutral": 2,
"Positive": 3,
"Very Positive": 4
}
# Streamlit
st.title("Text emotion and keyword analysis")
# Create a text entry box
input_text = st.text_area("Please enter user feedback", "")
if input_text:
try:
# sentiment analysis
result = sentiment_pipeline(input_text)
predicted_label = label_map[result[0]['label']]
rating = predicted_label + 1
confidence = result[0]['score']
# ketwords extraction
keywords = kw_model.extract_keywords(
input_text,
keyphrase_ngram_range=(1, 2),
stop_words="english",
top_n=3
)
keyword_text = [kw[0] for kw in keywords]
# result showing
st.write(f"Rating: {rating}/5")
st.write(f"Confidence: {confidence:.2f}")
st.write(f"Key words: {', '.join(keyword_text)}")
except Exception as e:
st.error(f"Error while analyzing text: {e}")