import json
import streamlit as st
from pyodide.code import run_js
from js import JSON
MODEL = "Xenova/twitter-roberta-base-sentiment-latest"
EMOJI = {
"POSITIVE": "😊",
"NEUTRAL": "😐",
"NEGATIVE": "😠"
}
st.set_page_config(
page_title="Semantic Analyzer",
page_icon="😊"
)
st.title("Semantic Analyzer")
st.caption(
"Enter a sentence or paragraph to analyze its sentiment."
)
if "clf" not in st.session_state:
msg = st.empty()
msg.info("Loading the AI model. This may take a few minutes.")
try:
mod = await run_js(
"import('https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1')"
)
st.session_state["clf"] = await mod.pipeline(
"sentiment-analysis",
MODEL
)
msg.empty()
except Exception as e:
msg.error(f"Could not load the model: {e}")
st.stop()
clf = st.session_state["clf"]
text = st.text_area(
"Enter your text:",
height=150,
max_chars=2000,
placeholder="Example: This application is useful and easy to understand."
)
if st.button("Analyze Sentiment", type="primary"):
if not text.strip():
st.warning("Please enter some text first.")
else:
try:
with st.spinner("Analyzing your text..."):
raw = await clf(text)
result = json.loads(JSON.stringify(raw))[0]
label = result["label"].upper()
score = float(result["score"])
st.subheader(
f"{EMOJI.get(label, '')} {label}"
)
st.progress(score)
st.write(f"Confidence: {score:.1%}")
st.caption(
"The prediction indicates sentiment, not the complete "
"meaning or intent of the text."
)
except Exception as e:
st.error(f"Analysis failed: {e}")