surajit2839 commited on
Commit
06be892
Β·
verified Β·
1 Parent(s): 30a3d8b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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="Hugging Face Sentiment AI", page_icon="πŸ€—")
6
 
7
  # --- MODEL LOADING (Cached) ---
8
  @st.cache_resource
9
- def load_sentiment_model():
10
- # This uses the default 'distilbert-base-uncased-finetuned-sst-2-english'
11
- return pipeline("sentiment-analysis")
 
12
 
13
- classifier = load_sentiment_model()
 
 
 
 
 
 
 
14
 
15
  # --- UI ELEMENTS ---
16
- st.title("πŸ€— AI Sentiment Analyzer")
17
- st.write("This app uses a Hugging Face Transformer model to detect sentiment.")
 
 
 
 
 
 
18
 
19
- user_input = st.text_area("Enter text to analyze:", placeholder="I am so excited to build this app!")
 
 
 
 
 
20
 
21
  if st.button("Analyze Sentiment"):
22
  if user_input.strip():
23
- with st.spinner("Analyzing..."):
24
- # Run prediction
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
- if label == "POSITIVE":
34
- st.success(f"### {label} 😊")
 
 
 
 
35
  else:
36
- st.error(f"### {label} 😑")
37
 
38
- st.metric(label="Confidence Score", value=f"{score:.2%}")
39
  else:
40
- st.warning("Please enter some text first!")
41
 
42
- st.caption("Running on Hugging Face Spaces with Streamlit")
 
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.")