vishnu-coder commited on
Commit
3473e65
Β·
2 Parent(s): c5f206def10737

Merge pull request #1 from Youranalyst-coder/codex/apply-changes-from-referenced-repository

Browse files
Files changed (2) hide show
  1. README.md +17 -7
  2. app/app.py +78 -16
README.md CHANGED
@@ -1,27 +1,37 @@
1
  # πŸ’¬ Twitter Sentiment Analyzer
2
 
3
- A machine learning project that predicts the sentiment (Positive, Neutral, Negative) of tweets using NLP and a trained model.
4
 
5
  ## πŸ”§ Tech Stack
6
 
7
  - Python, Pandas, Scikit-learn, NLTK
8
  - TF-IDF Vectorization
9
- - Logistic Regression
10
- - Streamlit (Web App Deployment)
11
 
12
  ## πŸš€ Features
13
 
14
- - Text preprocessing + visualization
15
- - Sentiment classification
16
- - Web interface using Streamlit
17
 
18
  ## βœ… Try it
19
 
20
- [Live App Link](https://<your-streamlit-url>)
21
 
22
  ## πŸ“‚ How to Run
23
 
24
  ```bash
25
  pip install -r requirements.txt
26
  streamlit run app/app.py
 
27
 
 
 
 
 
 
 
 
 
 
 
1
  # πŸ’¬ Twitter Sentiment Analyzer
2
 
3
+ A machine learning project that predicts whether a tweet is **positive**, **neutral**, or **negative** using natural language processing and logistic regression.
4
 
5
  ## πŸ”§ Tech Stack
6
 
7
  - Python, Pandas, Scikit-learn, NLTK
8
  - TF-IDF Vectorization
9
+ - Multiclass Logistic Regression
10
+ - Streamlit (Web App UI)
11
 
12
  ## πŸš€ Features
13
 
14
+ - Text preprocessing and model training notebooks
15
+ - Trained logistic regression pipeline for three-way sentiment detection
16
+ - Streamlit UI that displays the predicted class and class probabilities
17
 
18
  ## βœ… Try it
19
 
20
+ Deploy the `app/app.py` Streamlit interface or use the provided pickle files directly in your own project.
21
 
22
  ## πŸ“‚ How to Run
23
 
24
  ```bash
25
  pip install -r requirements.txt
26
  streamlit run app/app.py
27
+ ```
28
 
29
+ ## 🧠 Training Artifacts
30
+
31
+ The repository contains pretrained artifacts inside the `app/` and root directories:
32
+
33
+ - `app/logistic_model.pkl` – Multiclass logistic regression estimator
34
+ - `app/tfidf_vectorizer.pkl` – Fitted TF-IDF vectorizer compatible with the model
35
+ - `sentiment_model.pkl` – End-to-end Scikit-learn pipeline (vectorizer + classifier)
36
+
37
+ These are generated from the notebook in `notebooks/sentiment_model.ipynb`.
app/app.py CHANGED
@@ -1,25 +1,87 @@
1
- import streamlit as st
 
 
 
 
2
  import joblib
3
- import os
 
 
4
 
5
- # Load model and vectorizer once
6
- model_path = os.path.join(os.getcwd(), "app", "logistic_model.pkl")
7
- vectorizer_path = os.path.join(os.getcwd(), "app", "tfidf_vectorizer.pkl")
 
 
 
 
8
 
9
- model = joblib.load(model_path)
10
- vectorizer = joblib.load(vectorizer_path)
11
 
12
- st.title("Twitter Sentiment Analysis")
 
 
 
 
 
 
 
 
 
13
 
14
- user_input = st.text_area("Enter your tweet or sentence here:")
15
 
16
- if st.button("Predict Sentiment"):
17
- if user_input.strip() == "":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  st.warning("Please enter some text to analyze.")
19
  else:
20
- # Transform the input text
21
- X = vectorizer.transform([user_input])
22
- prediction = model.predict(X)[0]
 
 
 
 
 
23
 
24
- sentiment = "Positive" if prediction == 1 else "Negative"
25
- st.write(f"Predicted sentiment: **{sentiment}**")
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import Dict, Tuple
5
+
6
  import joblib
7
+ import pandas as pd
8
+ import streamlit as st
9
+
10
 
11
+ @st.cache_resource
12
+ def load_artifacts() -> Tuple[object, object]:
13
+ """Load the trained sentiment model and TF-IDF vectorizer."""
14
+ artifacts_dir = Path(__file__).resolve().parent
15
+ model = joblib.load(artifacts_dir / "logistic_model.pkl")
16
+ vectorizer = joblib.load(artifacts_dir / "tfidf_vectorizer.pkl")
17
+ return model, vectorizer
18
 
 
 
19
 
20
+ def predict_sentiment(text: str) -> Tuple[str, float, Dict[str, float]]:
21
+ """Predict the sentiment label and probability distribution for the text."""
22
+ model, vectorizer = load_artifacts()
23
+ features = vectorizer.transform([text])
24
+ probabilities = model.predict_proba(features)[0]
25
+ classes = model.classes_
26
+ class_probabilities = {label: prob for label, prob in zip(classes, probabilities)}
27
+ top_label = max(class_probabilities, key=class_probabilities.get)
28
+ top_score = class_probabilities[top_label]
29
+ return top_label, top_score, class_probabilities
30
 
 
31
 
32
+ SENTIMENT_DISPLAY = {
33
+ "positive": {
34
+ "title": "Positive",
35
+ "emoji": "😊",
36
+ "description": "The tweet expresses a favourable or uplifting sentiment.",
37
+ },
38
+ "neutral": {
39
+ "title": "Neutral",
40
+ "emoji": "😐",
41
+ "description": "The tweet is balanced without a clear positive or negative leaning.",
42
+ },
43
+ "negative": {
44
+ "title": "Negative",
45
+ "emoji": "😞",
46
+ "description": "The tweet carries criticism, frustration, or an unfavourable view.",
47
+ },
48
+ }
49
+
50
+
51
+ st.set_page_config(page_title="Twitter Sentiment Analysis", page_icon="πŸ’¬")
52
+ st.title("πŸ’¬ Twitter Sentiment Analysis")
53
+ st.write(
54
+ "Enter a tweet below and the model will classify it as positive, neutral, or negative."
55
+ )
56
+
57
+ user_input = st.text_area(
58
+ "Tweet text",
59
+ placeholder="I love how friendly everyone is on Twitter today!",
60
+ height=150,
61
+ )
62
+
63
+ if st.button("Predict Sentiment", type="primary"):
64
+ if not user_input.strip():
65
  st.warning("Please enter some text to analyze.")
66
  else:
67
+ label, score, probabilities = predict_sentiment(user_input)
68
+ details = SENTIMENT_DISPLAY.get(label, {"title": label.title(), "emoji": "", "description": ""})
69
+
70
+ st.success(
71
+ f"{details['emoji']} **{details['title']}** sentiment detected with a confidence of {score:.1%}."
72
+ )
73
+ if details.get("description"):
74
+ st.caption(details["description"])
75
 
76
+ sorted_probabilities = sorted(probabilities.items(), key=lambda item: item[1], reverse=True)
77
+ probability_frame = pd.DataFrame(
78
+ {
79
+ "Sentiment": [SENTIMENT_DISPLAY[label]["title"] for label, _ in sorted_probabilities],
80
+ "Probability": [score for _, score in sorted_probabilities],
81
+ }
82
+ )
83
+ probability_frame["Probability"] = probability_frame["Probability"].map(lambda p: f"{p:.1%}")
84
+ st.subheader("Class Probabilities")
85
+ st.dataframe(probability_frame, hide_index=True, width='stretch')
86
+ else:
87
+ st.info("Click **Predict Sentiment** to get a prediction.")