Spaces:
Sleeping
Sleeping
Commit Β·
fdc91d2
1
Parent(s): af3acf6
Lightweight remote model loader for successful Vercel deployment
Browse files- app.py +16 -9
- requirements.txt +5 -40
- src/twitter_sentiment/model_loader.py +15 -0
app.py
CHANGED
|
@@ -27,18 +27,20 @@ try:
|
|
| 27 |
if str(SRC_PATH) not in sys.path:
|
| 28 |
sys.path.insert(0, str(SRC_PATH))
|
| 29 |
|
| 30 |
-
#
|
| 31 |
from twitter_sentiment.config import load_config
|
| 32 |
-
from twitter_sentiment.
|
|
|
|
| 33 |
|
| 34 |
# -------------------------------------------------------------------------
|
| 35 |
-
# Cached dependencies
|
| 36 |
# -------------------------------------------------------------------------
|
| 37 |
@st.cache_resource(show_spinner=False)
|
| 38 |
def _load_dependencies():
|
| 39 |
-
"""Load configuration
|
| 40 |
config = load_config()
|
| 41 |
-
pipeline
|
|
|
|
| 42 |
return config, pipeline, metrics
|
| 43 |
|
| 44 |
# -------------------------------------------------------------------------
|
|
@@ -62,12 +64,17 @@ try:
|
|
| 62 |
# ---------------------- Sidebar ----------------------
|
| 63 |
with st.sidebar:
|
| 64 |
st.header("π Model Snapshot")
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if metrics:
|
| 67 |
st.metric("Macro F1", f"{metrics.get('f1_macro', 0.0):.2f}")
|
| 68 |
st.metric("Accuracy", f"{metrics.get('accuracy', 0.0):.2f}")
|
| 69 |
else:
|
| 70 |
-
st.info("Run
|
|
|
|
| 71 |
st.download_button(
|
| 72 |
label="β¬οΈ Download Metrics JSON",
|
| 73 |
data=json.dumps(metrics or {}, indent=2).encode("utf-8"),
|
|
@@ -75,7 +82,7 @@ try:
|
|
| 75 |
mime="application/json",
|
| 76 |
)
|
| 77 |
st.info(
|
| 78 |
-
"π Tip:
|
| 79 |
)
|
| 80 |
|
| 81 |
# ---------------------- Tabs ----------------------
|
|
@@ -103,7 +110,7 @@ try:
|
|
| 103 |
)
|
| 104 |
st.dataframe(metrics_df, use_container_width=True)
|
| 105 |
else:
|
| 106 |
-
st.info("Metrics will appear after
|
| 107 |
|
| 108 |
# ---------------------- Footer ----------------------
|
| 109 |
st.markdown("---")
|
|
|
|
| 27 |
if str(SRC_PATH) not in sys.path:
|
| 28 |
sys.path.insert(0, str(SRC_PATH))
|
| 29 |
|
| 30 |
+
# β
Updated imports to use lightweight remote model loader
|
| 31 |
from twitter_sentiment.config import load_config
|
| 32 |
+
from twitter_sentiment.model_loader import get_model
|
| 33 |
+
from twitter_sentiment.predictor import predict_with_threshold
|
| 34 |
|
| 35 |
# -------------------------------------------------------------------------
|
| 36 |
+
# Cached dependencies (β
lightweight version)
|
| 37 |
# -------------------------------------------------------------------------
|
| 38 |
@st.cache_resource(show_spinner=False)
|
| 39 |
def _load_dependencies():
|
| 40 |
+
"""Load configuration and lightweight remote model."""
|
| 41 |
config = load_config()
|
| 42 |
+
pipeline = get_model()
|
| 43 |
+
metrics = {} # Placeholder (not bundled to reduce size)
|
| 44 |
return config, pipeline, metrics
|
| 45 |
|
| 46 |
# -------------------------------------------------------------------------
|
|
|
|
| 64 |
# ---------------------- Sidebar ----------------------
|
| 65 |
with st.sidebar:
|
| 66 |
st.header("π Model Snapshot")
|
| 67 |
+
if hasattr(pipeline, "classes_"):
|
| 68 |
+
st.write("**Classes:**", ", ".join(pipeline.classes_))
|
| 69 |
+
else:
|
| 70 |
+
st.write("**Model:** Loaded remotely")
|
| 71 |
+
|
| 72 |
if metrics:
|
| 73 |
st.metric("Macro F1", f"{metrics.get('f1_macro', 0.0):.2f}")
|
| 74 |
st.metric("Accuracy", f"{metrics.get('accuracy', 0.0):.2f}")
|
| 75 |
else:
|
| 76 |
+
st.info("Run training locally to generate metrics (`scripts/train.py`).")
|
| 77 |
+
|
| 78 |
st.download_button(
|
| 79 |
label="β¬οΈ Download Metrics JSON",
|
| 80 |
data=json.dumps(metrics or {}, indent=2).encode("utf-8"),
|
|
|
|
| 82 |
mime="application/json",
|
| 83 |
)
|
| 84 |
st.info(
|
| 85 |
+
"π Tip: Integrate Oracle Autonomous Database by updating `config/settings.yaml`."
|
| 86 |
)
|
| 87 |
|
| 88 |
# ---------------------- Tabs ----------------------
|
|
|
|
| 110 |
)
|
| 111 |
st.dataframe(metrics_df, use_container_width=True)
|
| 112 |
else:
|
| 113 |
+
st.info("Metrics will appear after local training run (see `scripts/train.py`).")
|
| 114 |
|
| 115 |
# ---------------------- Footer ----------------------
|
| 116 |
st.markdown("---")
|
requirements.txt
CHANGED
|
@@ -1,41 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
cachetools==6.2.0
|
| 4 |
-
certifi==2025.8.3
|
| 5 |
-
charset-normalizer==3.4.3
|
| 6 |
-
click==8.3.0
|
| 7 |
-
colorama==0.4.6
|
| 8 |
-
gitdb==4.0.12
|
| 9 |
-
idna==3.10
|
| 10 |
-
iniconfig==2.1.0
|
| 11 |
-
Jinja2==3.1.6
|
| 12 |
-
joblib==1.5.2
|
| 13 |
-
MarkupSafe==3.0.3
|
| 14 |
-
narwhals==2.6.0
|
| 15 |
-
numpy==2.3.3
|
| 16 |
-
packaging==25.0
|
| 17 |
-
pandas==2.3.3
|
| 18 |
-
pillow==11.3.0
|
| 19 |
-
pluggy==1.6.0
|
| 20 |
-
protobuf==6.32.1
|
| 21 |
-
pyarrow==21.0.0
|
| 22 |
-
pydeck==0.9.1
|
| 23 |
-
Pygments==2.19.2
|
| 24 |
-
pytest==8.4.2
|
| 25 |
-
python-dateutil==2.9.0.post0
|
| 26 |
-
pytz==2025.2
|
| 27 |
-
referencing==0.36.2
|
| 28 |
-
requests==2.32.5
|
| 29 |
-
rpds-py==0.27.1
|
| 30 |
scikit-learn==1.6.1
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
tenacity==9.1.2
|
| 35 |
-
threadpoolctl==3.6.0
|
| 36 |
-
toml==0.10.2
|
| 37 |
-
tornado==6.5.2
|
| 38 |
-
typing_extensions==4.15.0
|
| 39 |
-
tzdata==2025.2
|
| 40 |
-
urllib3==2.5.0
|
| 41 |
-
watchdog==6.0.0
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
scikit-learn==1.6.1
|
| 4 |
+
joblib
|
| 5 |
+
|
| 6 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/twitter_sentiment/model_loader.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import joblib
|
| 3 |
+
import urllib.request
|
| 4 |
+
|
| 5 |
+
# βοΈ Public raw URL of your uploaded model on GitHub
|
| 6 |
+
MODEL_URL = "https://raw.githubusercontent.com/Youranalyst-coder/twitter-sentiment-analysis/main/artifacts/sentiment_pipeline.joblib"
|
| 7 |
+
MODEL_PATH = "artifacts/sentiment_pipeline.joblib"
|
| 8 |
+
|
| 9 |
+
def get_model():
|
| 10 |
+
"""Download and cache the model if not available."""
|
| 11 |
+
os.makedirs(os.path.dirname(MODEL_PATH), exist_ok=True)
|
| 12 |
+
if not os.path.exists(MODEL_PATH):
|
| 13 |
+
print("Downloading model from remote repository...")
|
| 14 |
+
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 15 |
+
return joblib.load(MODEL_PATH)
|