import json import streamlit as st from pyodide.code import run_js from js import JSON # ONNX copy of cardiffnlp/twitter-roberta-base-sentiment-latest MODEL = "Xenova/twitter-roberta-base-sentiment-latest" EMOJI = {"POSITIVE": "😊", "NEUTRAL": "😐", "NEGATIVE": "😠"} st.set_page_config(page_title="Sentiment Analyzer", page_icon="😊") st.title("Sentiment Analyzer") st.caption("Paste a sentence or paragraph and the model will score its sentiment.") if "clf" not in st.session_state: msg = st.empty() msg.info("Loading model (first run only, about 126 MB)... please wait.") 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 model: {e}") st.stop() clf = st.session_state["clf"] text = st.text_area("Your text:", height=150, max_chars=2000) if st.button("Analyze"): if not text.strip(): st.warning("Please type something first.") else: try: raw = await clf(text) result = json.loads(JSON.stringify(raw))[0] label = result["label"].upper() score = result["score"] st.subheader(f"{EMOJI.get(label, '')} {label}") st.progress(float(score)) st.write(f"Confidence: *{score:.0%}*") except Exception as e: st.error(f"Something went wrong: {e}")