File size: 1,100 Bytes
cdbeac9
30a3d8b
06be892
30a3d8b
2015eff
30a3d8b
2015eff
 
 
06be892
2015eff
30a3d8b
2015eff
06be892
 
2015eff
30a3d8b
2015eff
 
 
 
 
 
 
30a3d8b
2015eff
 
 
 
 
06be892
2015eff
 
 
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
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}")