Osama-Ahmed-27 commited on
Commit
60dac59
·
verified ·
1 Parent(s): 1cc5250

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -8,36 +8,32 @@ from pydantic import BaseModel
8
  from transformers import pipeline, BertForSequenceClassification, BertTokenizer
9
  from nltk.sentiment.vader import SentimentIntensityAnalyzer
10
 
11
- # ---------------- INIT ----------------
 
 
 
 
 
12
 
 
 
13
 
14
- NLTK_DATA_PATH = os.path.join(os.path.dirname(__file__), "nltk_data")
15
- os.makedirs(NLTK_DATA_PATH, exist_ok=True)
16
-
17
- # Ensure VADER is available
18
  try:
19
  nltk.data.find("sentiment/vader_lexicon")
20
  except LookupError:
21
- nltk.download("vader_lexicon", download_dir=NLTK_DATA_PATH)
22
-
23
- # Add path manually so nltk can find it
24
- nltk.data.path.append(NLTK_DATA_PATH)
25
-
26
- nltk.data.path.append("./nltk_data")
27
-
28
 
29
  vader = SentimentIntensityAnalyzer()
30
 
31
- # Emotion model
32
  emotion_model = pipeline("sentiment-analysis", model="tabularisai/multilingual-sentiment-analysis")
33
-
34
- # FinBERT Tone
35
  finbert = BertForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone", num_labels=3)
36
  finbert_tokenizer = BertTokenizer.from_pretrained("yiyanghkust/finbert-tone")
37
  tone_labels = ["Neutral", "Positive", "Negative"]
38
 
39
- # FastAPI
40
- app = FastAPI(title="Sentiment • Emotion • Tone API", version="2.0.0")
41
 
42
 
43
  # ---------------- HELPERS ----------------
 
8
  from transformers import pipeline, BertForSequenceClassification, BertTokenizer
9
  from nltk.sentiment.vader import SentimentIntensityAnalyzer
10
 
11
+ # ---------- Force writable cache locations (must match Dockerfile) ----------
12
+ os.environ.setdefault("NLTK_DATA", "/data/nltk_data")
13
+ os.environ.setdefault("HF_HOME", "/data/huggingface")
14
+ os.environ.setdefault("TRANSFORMERS_CACHE", "/data/huggingface/transformers")
15
+ os.environ.setdefault("HF_DATASETS_CACHE", "/data/huggingface/datasets")
16
+ os.environ.setdefault("TMPDIR", "/data/tmp")
17
 
18
+ # Also ensure nltk uses this path immediately
19
+ nltk.data.path = [os.environ["NLTK_DATA"]] + nltk.data.path
20
 
21
+ # ---------- NLTK VADER ----------
 
 
 
22
  try:
23
  nltk.data.find("sentiment/vader_lexicon")
24
  except LookupError:
25
+ # download into /data/nltk_data (writable)
26
+ nltk.download("vader_lexicon", download_dir=os.environ["NLTK_DATA"])
 
 
 
 
 
27
 
28
  vader = SentimentIntensityAnalyzer()
29
 
30
+ # ---------- Models ----------
31
  emotion_model = pipeline("sentiment-analysis", model="tabularisai/multilingual-sentiment-analysis")
 
 
32
  finbert = BertForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone", num_labels=3)
33
  finbert_tokenizer = BertTokenizer.from_pretrained("yiyanghkust/finbert-tone")
34
  tone_labels = ["Neutral", "Positive", "Negative"]
35
 
36
+ app = FastAPI(title="Sentiment • Emotion • Tone API", version="2.0.1")
 
37
 
38
 
39
  # ---------------- HELPERS ----------------