-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_models.py
More file actions
113 lines (100 loc) · 4.19 KB
/
Copy pathsetup_models.py
File metadata and controls
113 lines (100 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
"""
Setup script to download all NLP models locally
Run this before starting the application
"""
import os
import sys
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM
import subprocess
def download_models():
"""Download all required models"""
models_dir = os.path.join(os.path.dirname(__file__), "models")
os.makedirs(models_dir, exist_ok=True)
print("=" * 60)
print("Downloading NLP Models")
print("=" * 60)
# 1. Sentiment Analysis Model
print("\n[1/4] Downloading Sentiment Analysis Model...")
print("Model: distilbert-base-uncased-finetuned-sst-2-english")
try:
tokenizer = AutoTokenizer.from_pretrained(
"distilbert-base-uncased-finetuned-sst-2-english",
cache_dir=models_dir
)
model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased-finetuned-sst-2-english",
cache_dir=models_dir
)
print("✅ Sentiment model downloaded successfully!")
except Exception as e:
print(f"❌ Error downloading sentiment model: {e}")
# 2. NER Model (spaCy)
print("\n[2/4] Downloading NER Model (spaCy)...")
print("Model: en_core_web_sm")
try:
result = subprocess.run(
[sys.executable, "-m", "spacy", "download", "en_core_web_sm"],
capture_output=True,
text=True
)
if result.returncode == 0:
print("✅ spaCy NER model downloaded successfully!")
else:
print(f"⚠️ spaCy download output: {result.stdout}")
print(f"⚠️ Error: {result.stderr}")
except Exception as e:
print(f"❌ Error downloading spaCy model: {e}")
# 3. Classification Model (Zero-shot) - Try multiple options
print("\n[3/4] Downloading Text Classification Model (Zero-shot)...")
print("Trying models in order: BART-large (fastest) -> DistilBERT (lightweight) -> DeBERTa-v3 (best accuracy)")
models_to_try = [
("typeform/distilbert-base-uncased-mnli", "DistilBERT (~250MB, fastest, good accuracy)"),
("MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli", "DeBERTa-v3 (~500MB, best accuracy)"),
("facebook/bart-large-mnli", "BART-large (~1.6GB, very good accuracy)")
]
downloaded = False
for model_name, description in models_to_try:
try:
print(f"\n Trying: {model_name}")
print(f" {description}")
from transformers import pipeline
# Pre-download the zero-shot classification model
classification_pipeline = pipeline(
"zero-shot-classification",
model=model_name,
cache_dir=models_dir,
device=-1 # CPU
)
print(f" ✅ {model_name} downloaded successfully!")
downloaded = True
break # Success, stop trying other models
except Exception as e:
print(f" ⚠️ Error downloading {model_name}: {e}")
continue
if not downloaded:
print(" ❌ All classification models failed to download")
print(" Will use keyword-based classification as fallback")
# 4. Summarization Model
print("\n[4/4] Downloading Summarization Model...")
print("Model: sshleifer/distilbart-cnn-12-6")
try:
tokenizer = AutoTokenizer.from_pretrained(
"sshleifer/distilbart-cnn-12-6",
cache_dir=models_dir
)
model = AutoModelForSeq2SeqLM.from_pretrained(
"sshleifer/distilbart-cnn-12-6",
cache_dir=models_dir
)
print("✅ Summarization model downloaded successfully!")
except Exception as e:
print(f"❌ Error downloading summarization model: {e}")
print("\n" + "=" * 60)
print("✅ All models downloaded! You can now start the application.")
print("=" * 60)
print(f"\nModels are cached in: {models_dir}")
print("\n💡 Tip: Models are cached locally, so they won't download again on next startup.")
print(" The first request may still take a moment to load models into memory.")
if __name__ == "__main__":
download_models()