firstappsc / src /streamlit_app.py
surajit2839's picture
Update src/streamlit_app.py
2015eff verified
import streamlit as st
from transformers import pipeline
from pypdf import PdfReader
# --- MODEL LOADING ---
@st.cache_resource
def load_model():
# This is the specialized Twitter model we discussed
return pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
classifier = load_model()
# --- UI ---
st.title("🐦 Twitter-RoBERTa Sentiment AI")
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
if uploaded_file is not None:
try:
# Extract text from PDF
reader = PdfReader(uploaded_file)
text = ""
for page in reader.pages:
text += page.extract_text()
if st.button("Analyze PDF Sentiment"):
# Truncating text to stay within model limits (approx 512 tokens)
prediction = classifier(text[:1500])
label = prediction[0]['label']
score = prediction[0]['score']
st.write(f"**Result:** {label.upper()} (Confidence: {score:.2%})")
except Exception as e:
st.error(f"Error reading PDF: {e}")