File size: 1,890 Bytes
2201d3b
 
 
6c6da50
 
2201d3b
6c6da50
1b3da24
6c6da50
2201d3b
 
 
 
 
 
 
 
 
 
 
 
 
1b3da24
2201d3b
1b3da24
2201d3b
 
 
1b3da24
6c6da50
1b3da24
6c6da50
 
 
 
 
 
 
 
1b3da24
 
6c6da50
1b3da24
 
6c6da50
2201d3b
 
1b3da24
2201d3b
 
1b3da24
2201d3b
 
1b3da24
6c6da50
2201d3b
 
 
 
6c6da50
2201d3b
 
1b3da24
 
 
 
2201d3b
1b3da24
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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}")