Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +41 -20
src/streamlit_app.py
CHANGED
|
@@ -1,42 +1,63 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
# --- PAGE SETUP ---
|
| 5 |
-
st.set_page_config(page_title="
|
| 6 |
|
| 7 |
# --- MODEL LOADING (Cached) ---
|
| 8 |
@st.cache_resource
|
| 9 |
-
def
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
classifier =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# --- UI ELEMENTS ---
|
| 16 |
-
st.title("
|
| 17 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
if st.button("Analyze Sentiment"):
|
| 22 |
if user_input.strip():
|
| 23 |
-
with st.spinner("Analyzing..."):
|
| 24 |
-
#
|
| 25 |
-
results = classifier(user_input)
|
| 26 |
-
|
| 27 |
-
# Extract data
|
| 28 |
label = results[0]['label']
|
| 29 |
score = results[0]['score']
|
| 30 |
|
| 31 |
-
# --- DISPLAY RESULTS ---
|
| 32 |
st.divider()
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
-
st.
|
| 37 |
|
| 38 |
-
st.metric(label="Confidence
|
| 39 |
else:
|
| 40 |
-
st.warning("Please
|
| 41 |
|
| 42 |
-
st.caption("
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from pypdf import PdfReader
|
| 4 |
|
| 5 |
# --- PAGE SETUP ---
|
| 6 |
+
st.set_page_config(page_title="Twitter Sentiment AI", page_icon="π¦")
|
| 7 |
|
| 8 |
# --- MODEL LOADING (Cached) ---
|
| 9 |
@st.cache_resource
|
| 10 |
+
def load_twitter_model():
|
| 11 |
+
# Using the specialized Twitter RoBERTa model
|
| 12 |
+
model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
| 13 |
+
return pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)
|
| 14 |
|
| 15 |
+
classifier = load_twitter_model()
|
| 16 |
+
|
| 17 |
+
def get_pdf_text(pdf_file):
|
| 18 |
+
reader = PdfReader(pdf_file)
|
| 19 |
+
text = ""
|
| 20 |
+
for page in reader.pages:
|
| 21 |
+
text += page.extract_text()
|
| 22 |
+
return text
|
| 23 |
|
| 24 |
# --- UI ELEMENTS ---
|
| 25 |
+
st.title("π¦ Twitter-RoBERTa Sentiment AI")
|
| 26 |
+
st.markdown("Analyze sentiment using a model trained on **124M+ tweets**.")
|
| 27 |
+
|
| 28 |
+
# Tabbed interface for Input
|
| 29 |
+
tab1, tab2 = st.tabs(["π¬ Text Input", "π PDF Upload"])
|
| 30 |
+
|
| 31 |
+
with tab1:
|
| 32 |
+
user_input = st.text_area("Paste your text or tweet:", height=150)
|
| 33 |
|
| 34 |
+
with tab2:
|
| 35 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
| 36 |
+
if uploaded_file:
|
| 37 |
+
with st.spinner("Extracting text from PDF..."):
|
| 38 |
+
user_input = get_pdf_text(uploaded_file)
|
| 39 |
+
st.info(f"Extracted {len(user_input)} characters from PDF.")
|
| 40 |
|
| 41 |
if st.button("Analyze Sentiment"):
|
| 42 |
if user_input.strip():
|
| 43 |
+
with st.spinner("Analyzing with RoBERTa..."):
|
| 44 |
+
# The Twitter model outputs labels like 'positive', 'neutral', 'negative'
|
| 45 |
+
results = classifier(user_input[:2000]) # Truncate to avoid errors on huge PDFs
|
|
|
|
|
|
|
| 46 |
label = results[0]['label']
|
| 47 |
score = results[0]['score']
|
| 48 |
|
|
|
|
| 49 |
st.divider()
|
| 50 |
+
|
| 51 |
+
# Custom formatting based on labels
|
| 52 |
+
if label.lower() == "positive":
|
| 53 |
+
st.success(f"### Result: {label.upper()} π")
|
| 54 |
+
elif label.lower() == "negative":
|
| 55 |
+
st.error(f"### Result: {label.upper()} π‘")
|
| 56 |
else:
|
| 57 |
+
st.warning(f"### Result: {label.upper()} π")
|
| 58 |
|
| 59 |
+
st.metric(label="Confidence Level", value=f"{score:.2%}")
|
| 60 |
else:
|
| 61 |
+
st.warning("Please provide some text or a PDF file.")
|
| 62 |
|
| 63 |
+
st.caption("Note: Large PDFs are truncated to the first 2000 characters for processing speed.")
|