File size: 4,299 Bytes
225a4fc |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import streamlit as st
from transformers import pipeline
from streamlit_echarts import st_echarts
def project_ui():
# Load the pre-trained sentiment analysis model
model_name = "saved_model"
classifier = pipeline("sentiment-analysis", model=model_name)
# App title and description
st.title("Transformer-Based Text Classification")
st.write("""
This app uses a pre-trained Transformer model to classify text. Enter your text below to get the classification result.
""")
# User input
user_input = st.text_area("Enter your text here", height=150)
# Prediction button
if st.button("Predict"):
if user_input.strip():
try:
# Perform text classification
predictions = classifier(user_input)
# Extract label and score
label = predictions[0]['label']
score = predictions[0]['score']
# Calculate positive and negative scores
if label == 'LABEL_0':
negative_score = score
positive_score = 1 - score
else:
positive_score = score
negative_score = 1 - score
# Display sentiment prediction and scores
if label == 'LABEL_0':
st.error("Prediction: π Negative")
else:
st.success("Prediction: π Positive")
st.write("### Sentiment Scores")
st.write(f"Positive Score: {positive_score * 100:.2f}%")
st.write(f"Negative Score: {negative_score * 100:.2f}%")
# Display interactive sentiment analysis indicator
options = {
"series": [
{
"type": "gauge",
"startAngle": 180,
"endAngle": 0,
"radius": "100%",
"pointer": {"show": True, "length": "60%", "width": 5},
"progress": {
"show": True,
"overlap": False,
"roundCap": True,
"clip": False
},
"axisLine": {
"lineStyle": {
"width": 10,
"color": [
[0.5, "#FF6F61"], # Negative (Red)
[1, "#6AA84F"] # Positive (Green)
]
}
},
"axisTick": {"show": False},
"splitLine": {"show": False},
"axisLabel": {"distance": 15, "fontSize": 10},
"data": [
{"value": positive_score * 100, "name": "Positive"},
],
"title": {"fontSize": 14},
"detail": {
"valueAnimation": True,
"formatter": "{value}%",
"fontSize": 12
},
"animation": True, # Enable animation
"animationDuration": 2000, # Duration in ms
"animationEasing": "cubicOut" # Smooth animation
}
]
}
st.write("### Interactive Sentiment Analysis Indicator")
st_echarts(options, height="300px")
# Warning if confidence is below 60%
if score < 0.6:
st.warning("The confidence level of the prediction is below 60%. The result may not be reliable.")
except Exception as e:
st.error(f"An error occurred during prediction: {e}")
else:
st.warning("Please enter some text for prediction.")
# Run the Streamlit app
if __name__ == "__main__":
project_ui()
|