Spaces:
Runtime error
Runtime error
Merge branch 'main' into codex/enhance-and-deploy-project-for-interview-c6c16v
Browse files- README.md +108 -104
- app/app.py +27 -52
README.md
CHANGED
|
@@ -1,138 +1,137 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
A production-grade sentiment analysis accelerator designed to showcase the end-to-end delivery skills expected from a Deloitte India Oracle Analyst.
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
- TF-IDF vectorization paired with multiclass logistic regression for explainable predictions across positive, neutral, and negative classes.
|
| 9 |
-
- Streamlit web UI (`app/app.py`) that surfaces class probabilities and narrative insights for stakeholders.
|
| 10 |
-
- Notebook-driven exploration (`notebooks/`) to trace the lineage of the trained pipeline and retrain artefacts when required.
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
-
|
| 16 |
-
- **Consulting-grade engineering** – modular Python package, logging, configuration management, and CI/CD automation.
|
| 17 |
-
- **Business storytelling** – dashboards, metrics, and documentation that translate technical outcomes into measurable value for stakeholders.
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
| --- | --- | --- |
|
| 23 |
-
| **Live Streamlit demo** | Shareable dashboard URL for interviews (update after deployment) | [`https://<your-app>.streamlit.app`](https://<your-app>.streamlit.app) |
|
| 24 |
-
| **Architecture blueprint** | Deep dive on data, model, and DevOps layers | [`docs/architecture.md`](docs/architecture.md) |
|
| 25 |
-
| **OCI deployment playbook** | Container-first rollout with Oracle services | [`deployment/oracle_cloud.md`](deployment/oracle_cloud.md) |
|
| 26 |
-
| **Streamlit Community Cloud guide** | Launch a public URL in minutes | [`deployment/streamlit_cloud.md`](deployment/streamlit_cloud.md) |
|
| 27 |
-
| **Vercel redirect playbook** | Turn your Vercel domain into a Streamlit launcher | [`deployment/vercel_redirect.md`](deployment/vercel_redirect.md) |
|
| 28 |
-
| **GitHub connection & live check** | Step-by-step repo linking plus URL validation | [`deployment/github_live_validation.md`](deployment/github_live_validation.md) |
|
| 29 |
-
| **Training data sample** | Jump-start retraining conversations | [`data/twitter_training.csv`](data/twitter_training.csv) |
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
flowchart LR
|
| 37 |
-
subgraph
|
| 38 |
A[Oracle Autonomous DB] -- optional --> B[(CSV Data)]
|
| 39 |
end
|
| 40 |
-
subgraph
|
| 41 |
B --> C[Data Loader]
|
| 42 |
C --> D[Text Preprocessor]
|
| 43 |
D --> E[Scikit-learn Pipeline]
|
| 44 |
E --> F[Model Metrics]
|
| 45 |
end
|
| 46 |
-
subgraph
|
| 47 |
E --> G[Streamlit Dashboard]
|
| 48 |
E --> H[CLI Automation]
|
| 49 |
F --> G
|
| 50 |
F --> I[Reporting / GitHub Pages]
|
| 51 |
end
|
| 52 |
-
subgraph
|
| 53 |
J[GitHub Actions CI]
|
| 54 |
J --> E
|
| 55 |
J --> G
|
| 56 |
end
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
| `.github/workflows/` | CI workflow executing tests and lightweight linting |
|
| 73 |
-
|
| 74 |
-
## 🚀 Quick start
|
| 75 |
-
|
| 76 |
-
```bash
|
| 77 |
-
# 1. Create a virtual environment (recommended)
|
| 78 |
python -m venv .venv
|
| 79 |
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
| 80 |
|
| 81 |
-
# 2
|
| 82 |
pip install --upgrade pip
|
| 83 |
pip install -r requirements.txt
|
| 84 |
|
| 85 |
-
# 3
|
| 86 |
python scripts/train.py
|
| 87 |
|
| 88 |
-
# 4
|
| 89 |
streamlit run app/app.py
|
| 90 |
-
```
|
| 91 |
|
| 92 |
-
> **Note:** Model artifacts are deliberately excluded from source control. Run `python scripts/train.py` (or `make train`) any time you clone the repo or reset the workspace so that `artifacts/sentiment_pipeline.joblib` is available for the app, CLI, and tests.
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
| --- | --- |
|
| 100 |
-
| Unit tests | `pytest -q` |
|
| 101 |
-
| Package sanity | `python -m compileall src` |
|
| 102 |
-
| Model retrain | `python scripts/train.py` |
|
| 103 |
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
-
- Reusable preprocessing component with whitespace, URL, mention, and punctuation handling.
|
| 110 |
-
- Scikit-learn pipeline persisted with evaluation metrics for auditability.
|
| 111 |
-
- Streamlit dashboard with business-friendly narrative, download-ready metrics, and governance tab.
|
| 112 |
-
- CLI utilities for automation workflows (`scripts/train.py`, `scripts/predict.py`).
|
| 113 |
-
- Unit tests and CI pipeline to demonstrate engineering rigor.
|
| 114 |
|
| 115 |
-
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
2. Automating retraining via OCI Data Science jobs or GitHub Actions.
|
| 121 |
-
3. Wiring up Autonomous Database sources using wallet-based connectivity.
|
| 122 |
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
oracle_integration:
|
| 137 |
enabled: true
|
| 138 |
wallet_location: /path/to/wallet
|
|
@@ -142,40 +141,45 @@ oracle_integration:
|
|
| 142 |
SELECT text, sentiment
|
| 143 |
FROM analytics.twitter_training_data
|
| 144 |
WHERE created_at >= SYSDATE - 30
|
| 145 |
-
```
|
| 146 |
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
2. **Oracle expertise** – discuss wallet-based connectivity, SQL data modelling, and how OCI services plug into the pipeline.
|
| 151 |
-
3. **Engineering excellence** – highlight modular package design, automated tests, and CI/CD.
|
| 152 |
-
4. **Consulting mindset** – walk through the Streamlit dashboard as an executive-ready deliverable with actionable insights.
|
| 153 |
|
| 154 |
-
|
| 155 |
|
| 156 |
-
|
| 157 |
-
- Raise Pull Requests with CI status checks and screenshots of the Streamlit UI.
|
| 158 |
-
- Leverage GitHub Projects to track backlog items (e.g., drift monitoring, Oracle APEX reporting).
|
| 159 |
-
- Tag releases (e.g., `v1.0.0`) once the model is retrained and deployment is live.
|
| 160 |
|
| 161 |
-
|
| 162 |
|
| 163 |
-
|
| 164 |
-
- Capture dashboard screenshots (`docs/screenshots/`) for the README and interview slide deck.
|
| 165 |
-
- Prepare a 3-minute walkthrough focusing on client scenario, data sources, and automation roadmap.
|
| 166 |
|
| 167 |
-
|
| 168 |
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
| 176 |
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
| 180 |
|
| 181 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
💼 Deloitte-Ready Twitter Sentiment Intelligence
|
| 2 |
|
| 3 |
+
A production-grade sentiment analysis accelerator designed to showcase the end-to-end delivery skills expected from a Deloitte India Oracle Analyst.
|
| 4 |
+
The solution spans data ingestion, preprocessing, model training, governance, deployment readiness, and business storytelling — all within a lightweight, reproducible ML pipeline.
|
| 5 |
|
| 6 |
+
🧰 Technology Stack & Analytics Capabilities
|
| 7 |
|
| 8 |
+
Python, Pandas, Scikit-learn, NLTK – rapid experimentation & production-ready sentiment pipelines
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
TF-IDF Vectorization + Multiclass Logistic Regression – explainable predictions across positive, neutral, negative classes
|
| 11 |
|
| 12 |
+
Streamlit UI (app/app.py) – displays class probabilities & narrative insights for stakeholders
|
| 13 |
|
| 14 |
+
Notebook-driven exploration (notebooks/) – retraining lineage and model interpretability
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
Container-ready CI/CD pipeline – automates tests, linting, and retraining
|
| 17 |
|
| 18 |
+
🧭 Why This Project Stands Out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
Oracle ecosystem alignment – optional ingestion from Oracle Autonomous Database with OCI deployment playbooks
|
| 21 |
|
| 22 |
+
Consulting-grade engineering – modular Python package, logging, configuration management, GitHub Actions CI
|
| 23 |
|
| 24 |
+
Business storytelling – dashboards, KPIs, and executive-friendly insights translating data into measurable ROI
|
| 25 |
+
|
| 26 |
+
🔗 Live Resources & Quick Links
|
| 27 |
+
Asset Purpose Link
|
| 28 |
+
Live Streamlit Demo Interview-ready interactive dashboard https://<your-app>.streamlit.app
|
| 29 |
+
|
| 30 |
+
Architecture Blueprint Deep dive on data, model & DevOps layers docs/architecture.md
|
| 31 |
+
|
| 32 |
+
OCI Deployment Playbook Containerized rollout on Oracle Cloud deployment/oracle_cloud.md
|
| 33 |
+
|
| 34 |
+
Streamlit Cloud Guide Launch public URL in minutes deployment/streamlit_cloud.md
|
| 35 |
+
|
| 36 |
+
Vercel Redirect Playbook Vanity domain / QR-friendly link deployment/vercel_redirect.md
|
| 37 |
+
|
| 38 |
+
GitHub Live Validation Verify live deployment health deployment/github_live_validation.md
|
| 39 |
+
|
| 40 |
+
Training Data Sample Quick-start dataset for retraining data/twitter_training.csv
|
| 41 |
+
|
| 42 |
+
✅ After publishing, replace the placeholder URL above and embed it directly in your resume / interview slides.
|
| 43 |
+
|
| 44 |
+
🏗️ Architecture Overview
|
| 45 |
flowchart LR
|
| 46 |
+
subgraph Data_Layer
|
| 47 |
A[Oracle Autonomous DB] -- optional --> B[(CSV Data)]
|
| 48 |
end
|
| 49 |
+
subgraph Processing_Layer
|
| 50 |
B --> C[Data Loader]
|
| 51 |
C --> D[Text Preprocessor]
|
| 52 |
D --> E[Scikit-learn Pipeline]
|
| 53 |
E --> F[Model Metrics]
|
| 54 |
end
|
| 55 |
+
subgraph Experience_Layer
|
| 56 |
E --> G[Streamlit Dashboard]
|
| 57 |
E --> H[CLI Automation]
|
| 58 |
F --> G
|
| 59 |
F --> I[Reporting / GitHub Pages]
|
| 60 |
end
|
| 61 |
+
subgraph DevOps_Layer
|
| 62 |
J[GitHub Actions CI]
|
| 63 |
J --> E
|
| 64 |
J --> G
|
| 65 |
end
|
| 66 |
+
|
| 67 |
+
📁 Repository Structure
|
| 68 |
+
Path Description
|
| 69 |
+
app/ Streamlit app for demos
|
| 70 |
+
artifacts/ Generated model artifacts (gitignored; created via scripts/train.py)
|
| 71 |
+
config/ Central YAML settings controlling ingestion & deployment toggles
|
| 72 |
+
data/ Sample labelled tweets
|
| 73 |
+
deployment/ Multi-cloud deployment guides
|
| 74 |
+
docs/ Architecture diagrams & KPI catalogue
|
| 75 |
+
scripts/ Automation scripts for training & inference
|
| 76 |
+
src/twitter_sentiment/ Core reusable Python package
|
| 77 |
+
tests/ Pytest-based unit tests
|
| 78 |
+
.github/workflows/ CI pipeline (lint + tests)
|
| 79 |
+
🚀 Quick Start
|
| 80 |
+
# 1️⃣ Create virtual environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
python -m venv .venv
|
| 82 |
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
| 83 |
|
| 84 |
+
# 2️⃣ Install dependencies
|
| 85 |
pip install --upgrade pip
|
| 86 |
pip install -r requirements.txt
|
| 87 |
|
| 88 |
+
# 3️⃣ Train pipeline & generate artifacts
|
| 89 |
python scripts/train.py
|
| 90 |
|
| 91 |
+
# 4️⃣ Launch Deloitte Storytelling Dashboard
|
| 92 |
streamlit run app/app.py
|
|
|
|
| 93 |
|
|
|
|
| 94 |
|
| 95 |
+
Note: Artifacts are excluded from source control.
|
| 96 |
+
Run python scripts/train.py whenever cloning the repo to recreate artifacts/sentiment_pipeline.joblib.
|
| 97 |
+
|
| 98 |
+
🧪 Quality Gates
|
| 99 |
+
Check Command
|
| 100 |
+
Unit tests pytest -q
|
| 101 |
+
Package compile check python -m compileall src
|
| 102 |
+
Model retrain python scripts/train.py
|
| 103 |
|
| 104 |
+
CI runs automatically on every push or PR to main.
|
| 105 |
|
| 106 |
+
🧩 Feature Highlights
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
Config-driven ingestion switch (CSV ⇄ Oracle DB)
|
| 109 |
|
| 110 |
+
Reusable text-cleaning module (URL, mention, punctuation handling)
|
| 111 |
|
| 112 |
+
Auditable Scikit-learn pipeline with persisted metrics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
Streamlit dashboard → probabilities + governance tab
|
| 115 |
|
| 116 |
+
CLI utilities for automated workflows
|
| 117 |
|
| 118 |
+
Pytest suite + GitHub Actions CI/CD
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
☁️ Cloud Deployment
|
| 121 |
|
| 122 |
+
See deployment/oracle_cloud.md
|
| 123 |
+
for the Oracle Cloud Infrastructure (OCI) guide:
|
| 124 |
|
| 125 |
+
Containerize Streamlit app using OCI Container Instances
|
| 126 |
|
| 127 |
+
Automate retraining via OCI Data Science jobs / GitHub Actions
|
| 128 |
|
| 129 |
+
Connect to Autonomous DB with wallet credentials
|
| 130 |
|
| 131 |
+
Or follow deployment/streamlit_cloud.md
|
| 132 |
+
for a free Streamlit Cloud public link—perfect for interviews.
|
| 133 |
|
| 134 |
+
🗃️ Oracle Database Integration (Optional)
|
| 135 |
oracle_integration:
|
| 136 |
enabled: true
|
| 137 |
wallet_location: /path/to/wallet
|
|
|
|
| 141 |
SELECT text, sentiment
|
| 142 |
FROM analytics.twitter_training_data
|
| 143 |
WHERE created_at >= SYSDATE - 30
|
|
|
|
| 144 |
|
| 145 |
+
📊 Storytelling in Interviews
|
| 146 |
+
|
| 147 |
+
Business Impact – sentiment tracking → improved ROI & retention
|
| 148 |
+
|
| 149 |
+
Oracle Expertise – wallet connectivity, SQL modeling, OCI integration
|
| 150 |
+
|
| 151 |
+
Engineering Rigor – modular design, testing, CI/CD
|
| 152 |
+
|
| 153 |
+
Consulting Mindset – executive-ready Streamlit deliverable
|
| 154 |
+
|
| 155 |
+
📦 GitHub Best Practices
|
| 156 |
+
|
| 157 |
+
Use feature branches (e.g., feature/oracle-ingestion)
|
| 158 |
|
| 159 |
+
Raise PRs with CI status + screenshots
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
Track backlog via GitHub Projects
|
| 162 |
|
| 163 |
+
Tag releases (e.g., v1.0.0) post-deployment
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
🧠 Training Artifacts
|
| 166 |
|
| 167 |
+
Generated after running scripts/train.py:
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
sentiment_pipeline.joblib – Full TF-IDF + Logistic Regression pipeline
|
| 170 |
|
| 171 |
+
Legacy (compatible) files:
|
| 172 |
|
| 173 |
+
app/logistic_model.pkl
|
| 174 |
|
| 175 |
+
app/tfidf_vectorizer.pkl
|
| 176 |
|
| 177 |
+
sentiment_model.pkl
|
| 178 |
|
| 179 |
+
Use these for quick demos or backward comparisons.
|
| 180 |
|
| 181 |
+
🤝 Contributing
|
| 182 |
|
| 183 |
+
Pull requests welcome!
|
| 184 |
+
Please open an issue describing any enhancement or bugfix before major changes.
|
| 185 |
+
|
app/app.py
CHANGED
|
@@ -1,15 +1,18 @@
|
|
| 1 |
-
"""Streamlit front-end for the Deloitte-ready Twitter
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import json
|
| 6 |
-
from pathlib import Path
|
| 7 |
import sys
|
|
|
|
| 8 |
from typing import Dict
|
| 9 |
|
| 10 |
import pandas as pd
|
| 11 |
import streamlit as st
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
ROOT = Path(__file__).resolve().parents[1]
|
| 14 |
SRC_PATH = ROOT / "src"
|
| 15 |
if str(SRC_PATH) not in sys.path:
|
|
@@ -18,16 +21,31 @@ if str(SRC_PATH) not in sys.path:
|
|
| 18 |
from twitter_sentiment.config import load_config
|
| 19 |
from twitter_sentiment.predictor import load_artifacts, predict_with_threshold
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@st.cache_resource(show_spinner=False)
|
| 24 |
def _load_dependencies():
|
|
|
|
| 25 |
config = load_config()
|
| 26 |
pipeline, metrics = load_artifacts(config)
|
| 27 |
return config, pipeline, metrics
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
def format_probabilities(probabilities: Dict[str, float]) -> pd.DataFrame:
|
|
|
|
| 31 |
return (
|
| 32 |
pd.DataFrame.from_dict(probabilities, orient="index", columns=["confidence"])
|
| 33 |
.sort_values("confidence", ascending=False)
|
|
@@ -35,54 +53,11 @@ def format_probabilities(probabilities: Dict[str, float]) -> pd.DataFrame:
|
|
| 35 |
)
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
def main() -> None:
|
|
|
|
| 39 |
config, pipeline, metrics = _load_dependencies()
|
| 40 |
|
| 41 |
-
|
| 42 |
-
st.caption(
|
| 43 |
-
"Built to demonstrate Deloitte India Oracle Analyst competencies across data engineering, analytics, and automation."
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
with st.sidebar:
|
| 47 |
-
st.header("Model Snapshot")
|
| 48 |
-
st.write("**Classes:**", ", ".join(pipeline.classes_))
|
| 49 |
-
if metrics:
|
| 50 |
-
st.metric("Macro F1", f"{metrics.get('f1_macro', 0.0):.2f}")
|
| 51 |
-
st.metric("Accuracy", f"{metrics.get('accuracy', 0.0):.2f}")
|
| 52 |
-
st.download_button(
|
| 53 |
-
"Download Metrics JSON",
|
| 54 |
-
data=json.dumps(metrics or {}, indent=2).encode("utf-8"),
|
| 55 |
-
file_name="metrics.json",
|
| 56 |
-
mime="application/json",
|
| 57 |
-
)
|
| 58 |
-
st.info(
|
| 59 |
-
"🚀 Tip: integrate with Oracle Autonomous Database by updating `config/settings.yaml`."
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
tab_predict, tab_metrics = st.tabs(["Predict", "Model Governance"])
|
| 63 |
-
|
| 64 |
-
with tab_predict:
|
| 65 |
-
st.subheader("Real-time Sentiment Assessment")
|
| 66 |
-
user_input = st.text_area("Enter a tweet or customer verbatim", height=150)
|
| 67 |
-
if st.button("Run Analysis", type="primary"):
|
| 68 |
-
if not user_input.strip():
|
| 69 |
-
st.warning("Please enter text to analyse.")
|
| 70 |
-
else:
|
| 71 |
-
label, probabilities = predict_with_threshold(user_input, config)
|
| 72 |
-
st.success(f"Predicted sentiment: **{label.title()}**")
|
| 73 |
-
st.dataframe(format_probabilities(probabilities), use_container_width=True)
|
| 74 |
-
|
| 75 |
-
with tab_metrics:
|
| 76 |
-
st.subheader("Operational Metrics")
|
| 77 |
-
if metrics:
|
| 78 |
-
metrics_df = pd.DataFrame(metrics, index=["score"]).T.rename(columns={"score": "value"})
|
| 79 |
-
st.dataframe(metrics_df, use_container_width=True)
|
| 80 |
-
else:
|
| 81 |
-
st.info("Metrics will appear after the first training run (see `scripts/train.py`).")
|
| 82 |
-
|
| 83 |
-
st.markdown("---")
|
| 84 |
-
st.caption("© 2024 Deloitte-aligned Sentiment Analytics Accelerator")
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
if __name__ == "__main__":
|
| 88 |
-
main()
|
|
|
|
| 1 |
+
"""Streamlit front-end for the Deloitte-ready Twitter Sentiment Intelligence dashboard."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import json
|
|
|
|
| 6 |
import sys
|
| 7 |
+
from pathlib import Path
|
| 8 |
from typing import Dict
|
| 9 |
|
| 10 |
import pandas as pd
|
| 11 |
import streamlit as st
|
| 12 |
|
| 13 |
+
# -------------------------------------------------------------------------
|
| 14 |
+
# Path setup to include the local src/ package for imports
|
| 15 |
+
# -------------------------------------------------------------------------
|
| 16 |
ROOT = Path(__file__).resolve().parents[1]
|
| 17 |
SRC_PATH = ROOT / "src"
|
| 18 |
if str(SRC_PATH) not in sys.path:
|
|
|
|
| 21 |
from twitter_sentiment.config import load_config
|
| 22 |
from twitter_sentiment.predictor import load_artifacts, predict_with_threshold
|
| 23 |
|
| 24 |
+
# -------------------------------------------------------------------------
|
| 25 |
+
# Streamlit App Configuration
|
| 26 |
+
# -------------------------------------------------------------------------
|
| 27 |
+
st.set_page_config(
|
| 28 |
+
page_title="Twitter Sentiment Intelligence",
|
| 29 |
+
page_icon="💼",
|
| 30 |
+
layout="wide",
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# -------------------------------------------------------------------------
|
| 34 |
+
# Cached resource loading (config, pipeline, metrics)
|
| 35 |
+
# -------------------------------------------------------------------------
|
| 36 |
@st.cache_resource(show_spinner=False)
|
| 37 |
def _load_dependencies():
|
| 38 |
+
"""Load configuration, trained pipeline, and metrics from artifacts."""
|
| 39 |
config = load_config()
|
| 40 |
pipeline, metrics = load_artifacts(config)
|
| 41 |
return config, pipeline, metrics
|
| 42 |
|
| 43 |
|
| 44 |
+
# -------------------------------------------------------------------------
|
| 45 |
+
# Helper function to format prediction probabilities
|
| 46 |
+
# -------------------------------------------------------------------------
|
| 47 |
def format_probabilities(probabilities: Dict[str, float]) -> pd.DataFrame:
|
| 48 |
+
"""Convert prediction probabilities to a styled DataFrame for display."""
|
| 49 |
return (
|
| 50 |
pd.DataFrame.from_dict(probabilities, orient="index", columns=["confidence"])
|
| 51 |
.sort_values("confidence", ascending=False)
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
|
| 56 |
+
# -------------------------------------------------------------------------
|
| 57 |
+
# Main Streamlit Application
|
| 58 |
+
# -------------------------------------------------------------------------
|
| 59 |
def main() -> None:
|
| 60 |
+
"""Render the Deloitte-ready Twitter Sentiment Intelligence Dashboard."""
|
| 61 |
config, pipeline, metrics = _load_dependencies()
|
| 62 |
|
| 63 |
+
# -----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|