Spaces:
Build error
Build error
Upload 7 files
Browse files- Dockerfile +27 -20
- Procfile +1 -0
- README.md +70 -20
- app.py +108 -0
- requirements.txt +0 -0
- runtime.txt +1 -0
- setup.sh +8 -0
Dockerfile
CHANGED
|
@@ -1,20 +1,27 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
-
|
| 3 |
-
WORKDIR /app
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
COPY
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Set environment variables to fix permissions
|
| 6 |
+
ENV HOME=/tmp
|
| 7 |
+
ENV STREAMLIT_CONFIG_DIR=/tmp/.streamlit
|
| 8 |
+
ENV TRANSFORMERS_CACHE=/tmp/.cache
|
| 9 |
+
ENV HF_HOME=/tmp/.cache/huggingface
|
| 10 |
+
|
| 11 |
+
# Copy requirements and install dependencies
|
| 12 |
+
COPY requirements.txt .
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
# Copy application files
|
| 16 |
+
COPY . .
|
| 17 |
+
|
| 18 |
+
# Create necessary directories with proper permissions
|
| 19 |
+
RUN mkdir -p /tmp/.streamlit && \
|
| 20 |
+
mkdir -p /tmp/.cache && \
|
| 21 |
+
chmod -R 777 /tmp
|
| 22 |
+
|
| 23 |
+
# Expose port 7860 (Hugging Face Spaces default)
|
| 24 |
+
EXPOSE 7860
|
| 25 |
+
|
| 26 |
+
# Run the Streamlit app
|
| 27 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.headless=true"]
|
Procfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: streamlit run app.py --server.port $PORT --server.address 0.0.0.0 --server.headless true
|
README.md
CHANGED
|
@@ -1,20 +1,70 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Sentiment Analysis App
|
| 2 |
+
|
| 3 |
+

|
| 4 |
+

|
| 5 |
+

|
| 6 |
+
|
| 7 |
+
> A clean, production-minded Streamlit app that reads text and returns generic & finance-aware sentiment labels and confidence scores.
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
## Why this project
|
| 12 |
+
|
| 13 |
+
Market language is different: the same phrase can mean different things in finance compared to casual chat. This app is designed to:
|
| 14 |
+
- prioritize clarity (human-readable outputs),
|
| 15 |
+
- be resilient in deployment (tips to avoid first-run timeouts),
|
| 16 |
+
- and be easy to extend (swap the model, add dashboards).
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## Quick visual
|
| 21 |
+

|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## Project structure
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
- **app.py** - main Streamlit application script
|
| 29 |
+
- **requirements.txt** - Python dependencies
|
| 30 |
+
- **setup.sh** - shell script for environment setup
|
| 31 |
+
- **runtime.txt** - runtime configuration (for deployment, e.g. Heroku)
|
| 32 |
+
- **.devcontainer/** - config for VSCode / dev container setup
|
| 33 |
+
- **Data/** - datasets, corpora, lexicons etc.
|
| 34 |
+
- **finance/** - finance-specific modules, models, tools
|
| 35 |
+
- **notebooks/**- Jupyter notebooks used during experimentation / prototyping
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
## Installation & Setup
|
| 40 |
+
|
| 41 |
+
Below is a typical setup for development and running locally.
|
| 42 |
+
|
| 43 |
+
1. **Clone the repository**
|
| 44 |
+
|
| 45 |
+
```bash
|
| 46 |
+
git clone https://github.com/Ani-404/Sentiment-Analysis-App.git
|
| 47 |
+
cd Sentiment-Analysis-App
|
| 48 |
+
```
|
| 49 |
+
2. **(Optional) Create & activate a virtual environment**
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
python3 -m venv venv
|
| 53 |
+
source venv/bin/activate
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
3. **Install dependencies**
|
| 57 |
+
```bash
|
| 58 |
+
pip install -r requirements.txt
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
4. **Run the application locally**
|
| 62 |
+
```bash
|
| 63 |
+
streamlit run app.py
|
| 64 |
+
```
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
# Fix Streamlit permissions issue on HF Spaces
|
| 5 |
+
os.environ["STREAMLIT_CONFIG_DIR"] = tempfile.mkdtemp()
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import numpy as np
|
| 11 |
+
import plotly.express as px
|
| 12 |
+
import yfinance as yf
|
| 13 |
+
import warnings
|
| 14 |
+
warnings.filterwarnings('ignore')
|
| 15 |
+
|
| 16 |
+
# Configure page
|
| 17 |
+
st.set_page_config(
|
| 18 |
+
page_title="SentText - Advanced Analysis",
|
| 19 |
+
page_icon="π",
|
| 20 |
+
layout="wide",
|
| 21 |
+
initial_sidebar_state="expanded"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Initialize models
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def load_models():
|
| 27 |
+
models = {}
|
| 28 |
+
try:
|
| 29 |
+
# Remove use_auth_token, rely on environment login
|
| 30 |
+
models['emotion'] = pipeline(
|
| 31 |
+
"text-classification",
|
| 32 |
+
model="Ani-404/emotion-model",
|
| 33 |
+
tokenizer="Ani-404/emotion-model"
|
| 34 |
+
)
|
| 35 |
+
models['financial'] = pipeline(
|
| 36 |
+
"text-classification",
|
| 37 |
+
model="Ani-404/finbert-model",
|
| 38 |
+
tokenizer="Ani-404/finbert-model"
|
| 39 |
+
)
|
| 40 |
+
except Exception:
|
| 41 |
+
models = {}
|
| 42 |
+
return models
|
| 43 |
+
|
| 44 |
+
# Prediction functions
|
| 45 |
+
|
| 46 |
+
def predict_emotions_real(text, model):
|
| 47 |
+
results = model(text, return_all_scores=True)
|
| 48 |
+
scores_list = results[0]
|
| 49 |
+
top = max(scores_list, key=lambda x: x['score'])
|
| 50 |
+
return top['label'].lower(), top['score']
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def analyze_financial_real(text, model):
|
| 54 |
+
results = model(text)
|
| 55 |
+
res = results[0]
|
| 56 |
+
label = res['label'].lower()
|
| 57 |
+
confidence = res['score']
|
| 58 |
+
if 'positive' in label:
|
| 59 |
+
score = confidence
|
| 60 |
+
signal = 'BUY' if confidence>0.7 else 'HOLD'
|
| 61 |
+
elif 'negative' in label:
|
| 62 |
+
score = -confidence
|
| 63 |
+
signal = 'SELL' if confidence>0.7 else 'HOLD'
|
| 64 |
+
else:
|
| 65 |
+
score = 0; signal='HOLD'
|
| 66 |
+
return score, confidence, signal
|
| 67 |
+
|
| 68 |
+
# Main UI
|
| 69 |
+
|
| 70 |
+
def main():
|
| 71 |
+
st.title("SentText Analytics")
|
| 72 |
+
models = load_models()
|
| 73 |
+
tabs = st.tabs(["π Emotion Analysis", "π Financial Analysis"])
|
| 74 |
+
|
| 75 |
+
# Emotion
|
| 76 |
+
with tabs[0]:
|
| 77 |
+
text = st.text_area("Enter text:")
|
| 78 |
+
if st.button("Analyze Emotion") and text:
|
| 79 |
+
if 'emotion' in models:
|
| 80 |
+
label, conf = predict_emotions_real(text, models['emotion'])
|
| 81 |
+
st.write(f"**Emotion**: {label} | **Confidence**: {conf:.1%}")
|
| 82 |
+
else:
|
| 83 |
+
st.error("Emotion model not loaded.")
|
| 84 |
+
|
| 85 |
+
# Financial
|
| 86 |
+
with tabs[1]:
|
| 87 |
+
col1, col2 = st.columns([1,2])
|
| 88 |
+
with col1:
|
| 89 |
+
ticker = st.text_input("Ticker:", value='AAPL')
|
| 90 |
+
if st.button("Fetch Chart"):
|
| 91 |
+
df = yf.Ticker(ticker).history(period='5d')
|
| 92 |
+
if not df.empty:
|
| 93 |
+
fig = px.line(df, y='Close', title=f"{ticker} Closing Prices (5d)")
|
| 94 |
+
st.plotly_chart(fig)
|
| 95 |
+
else:
|
| 96 |
+
st.error("No data for ticker.")
|
| 97 |
+
with col2:
|
| 98 |
+
fin_text = st.text_area("Enter financial text:")
|
| 99 |
+
if st.button("Analyze Financial Sentiment") and fin_text:
|
| 100 |
+
if 'financial' in models:
|
| 101 |
+
score, conf, signal = analyze_financial_real(fin_text, models['financial'])
|
| 102 |
+
st.write(f"**Sentiment Score**: {score:.2f} | **Confidence**: {conf:.1%}")
|
| 103 |
+
st.write(f"**Signal**: {signal}")
|
| 104 |
+
else:
|
| 105 |
+
st.error("Financial model not loaded.")
|
| 106 |
+
|
| 107 |
+
if __name__ == '__main__':
|
| 108 |
+
main()
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
runtime.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python-3.11.9
|
setup.sh
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mkdir -p ~/.streamlit/
|
| 2 |
+
echo "\
|
| 3 |
+
[server]\n\
|
| 4 |
+
headless = true\n\
|
| 5 |
+
port = $PORT\n\
|
| 6 |
+
enableCORS = false\n\
|
| 7 |
+
\n\
|
| 8 |
+
" > ~/.streamlit/config.toml
|