SeeknnDestroy
commited on
update models
Browse files- .gitignore +1 -0
- __pycache__/config.cpython-310.pyc +0 -0
- app.py +95 -70
- config.py +52 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
__pycache__/config.cpython-310.pyc
ADDED
Binary file (2.04 kB). View file
|
|
app.py
CHANGED
@@ -8,11 +8,10 @@ import time
|
|
8 |
from transformers import AutoTokenizer, AutoModel
|
9 |
import torch.nn.functional as F
|
10 |
from openai import AzureOpenAI
|
11 |
-
from
|
|
|
12 |
|
13 |
-
|
14 |
-
model_path_fasttext_raw = hf_hub_download(repo_id="talhasarit41/fasttext", filename="fasttext_raw.bin")
|
15 |
-
model_path_fasttext_preprocessed = hf_hub_download(repo_id="talhasarit41/fasttext", filename="fasttext_preprocessed.bin")
|
16 |
|
17 |
# Azure OpenAI Configuration
|
18 |
AZURE_API_VERSION = "2024-02-01"
|
@@ -92,25 +91,33 @@ def get_azure_embedding(text):
|
|
92 |
def load_models():
|
93 |
models = {}
|
94 |
|
95 |
-
# Load pickle models
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
models['Azure Classifier'] = pickle.load(f)
|
104 |
-
|
105 |
-
with open(os.path.join(MODEL_DIR, 'azure_knn_classifier.pkl'), 'rb') as f:
|
106 |
-
models['Azure KNN Classifier'] = pickle.load(f)
|
107 |
-
|
108 |
-
with open(os.path.join(MODEL_DIR, 'gte_classifier.pkl'), 'rb') as f:
|
109 |
-
models['GTE Classifier'] = pickle.load(f)
|
110 |
|
111 |
# Load FastText models
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
return models
|
116 |
|
@@ -185,13 +192,20 @@ def predict_text_streaming(text):
|
|
185 |
models = load_models()
|
186 |
results = []
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
# First yield empty table and progress bar
|
189 |
-
yield format_progress(
|
190 |
|
191 |
-
# Process FastText models first (they're fastest
|
192 |
for model_name, model in models.items():
|
193 |
if isinstance(model, fasttext.FastText._FastText):
|
194 |
-
yield format_progress(
|
195 |
start_time = time.time()
|
196 |
prediction = model.predict(text)
|
197 |
label = prediction[0][0].replace('__label__', '')
|
@@ -204,65 +218,76 @@ def predict_text_streaming(text):
|
|
204 |
'confidence': confidence,
|
205 |
'time': inference_time
|
206 |
})
|
207 |
-
|
|
|
208 |
|
209 |
-
# Process E5 models
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
228 |
|
229 |
-
# Process Azure models
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
prediction = model.predict(embedding_2d)[0]
|
237 |
probabilities = model.predict_proba(embedding_2d)[0]
|
238 |
confidence = max(probabilities)
|
239 |
inference_time = time.time() - start_time
|
240 |
|
241 |
results.append({
|
242 |
-
'model':
|
243 |
'prediction': prediction,
|
244 |
'confidence': confidence,
|
245 |
'time': inference_time + embed_time
|
246 |
})
|
247 |
-
|
248 |
-
|
249 |
-
# Process GTE model
|
250 |
-
yield format_progress(90, "Processing GTE Classifier..."), format_results(results)
|
251 |
-
gte_embedding, embed_time = generate_gte_embedding(text)
|
252 |
-
model = models['GTE Classifier']
|
253 |
-
embedding_2d = gte_embedding.reshape(1, -1)
|
254 |
-
prediction = model.predict(embedding_2d)[0]
|
255 |
-
probabilities = model.predict_proba(embedding_2d)[0]
|
256 |
-
confidence = max(probabilities)
|
257 |
-
inference_time = time.time() - start_time
|
258 |
-
|
259 |
-
results.append({
|
260 |
-
'model': 'GTE Classifier',
|
261 |
-
'prediction': prediction,
|
262 |
-
'confidence': confidence,
|
263 |
-
'time': inference_time + embed_time
|
264 |
-
})
|
265 |
-
yield format_progress(100, "Completed!"), format_results(results)
|
266 |
|
267 |
except Exception as e:
|
268 |
yield "", f"<div style='color: red; padding: 20px;'>Error occurred: {str(e)}</div>"
|
|
|
8 |
from transformers import AutoTokenizer, AutoModel
|
9 |
import torch.nn.functional as F
|
10 |
from openai import AzureOpenAI
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
from config import get_fasttext_path, is_model_enabled
|
13 |
|
14 |
+
load_dotenv()
|
|
|
|
|
15 |
|
16 |
# Azure OpenAI Configuration
|
17 |
AZURE_API_VERSION = "2024-02-01"
|
|
|
91 |
def load_models():
|
92 |
models = {}
|
93 |
|
94 |
+
# Load pickle models only if enabled
|
95 |
+
pickle_models = {
|
96 |
+
'E5 Classifier': 'e5_classifier.pkl',
|
97 |
+
'E5-Instruct Classifier': 'e5_large_instruct_classifier.pkl',
|
98 |
+
'Azure Classifier': 'azure_classifier.pkl',
|
99 |
+
'Azure KNN Classifier': 'azure_knn_classifier.pkl',
|
100 |
+
'GTE Classifier': 'gte_classifier.pkl'
|
101 |
+
}
|
102 |
|
103 |
+
for model_name, filename in pickle_models.items():
|
104 |
+
if is_model_enabled(model_name):
|
105 |
+
with open(os.path.join(MODEL_DIR, filename), 'rb') as f:
|
106 |
+
models[model_name] = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
# Load FastText models
|
109 |
+
if is_model_enabled('FastText Default'):
|
110 |
+
models['FastText Default'] = fasttext.load_model(get_fasttext_path('fasttext_default'))
|
111 |
+
if is_model_enabled('FastText Preprocessed'):
|
112 |
+
models['FastText Preprocessed'] = fasttext.load_model(get_fasttext_path('fasttext_preprocessed'))
|
113 |
+
if is_model_enabled('Fasttext WordnNGram 1'):
|
114 |
+
models['Fasttext WordnNGram 1'] = fasttext.load_model(get_fasttext_path('word_n_gram_1'))
|
115 |
+
if is_model_enabled('Fasttext WordnNGram 2'):
|
116 |
+
models['Fasttext WordnNGram 2'] = fasttext.load_model(get_fasttext_path('word_n_gram_2'))
|
117 |
+
if is_model_enabled('Fasttext WordnNGram 3'):
|
118 |
+
models['Fasttext WordnNGram 3'] = fasttext.load_model(get_fasttext_path('word_n_gram_3'))
|
119 |
+
if is_model_enabled('Fasttext Low Overfit'):
|
120 |
+
models['Fasttext Low Overfit'] = fasttext.load_model(get_fasttext_path('low_overfit'))
|
121 |
|
122 |
return models
|
123 |
|
|
|
192 |
models = load_models()
|
193 |
results = []
|
194 |
|
195 |
+
if not models:
|
196 |
+
return "", "<div style='color: red; padding: 20px;'>No models are enabled in the configuration.</div>"
|
197 |
+
|
198 |
+
# Calculate progress step based on number of enabled models
|
199 |
+
progress_step = 100.0 / len(models)
|
200 |
+
current_progress = 0
|
201 |
+
|
202 |
# First yield empty table and progress bar
|
203 |
+
yield format_progress(current_progress, "Loading models..."), format_results(results)
|
204 |
|
205 |
+
# Process FastText models first (they're fastest)
|
206 |
for model_name, model in models.items():
|
207 |
if isinstance(model, fasttext.FastText._FastText):
|
208 |
+
yield format_progress(current_progress, f"Processing {model_name}..."), format_results(results)
|
209 |
start_time = time.time()
|
210 |
prediction = model.predict(text)
|
211 |
label = prediction[0][0].replace('__label__', '')
|
|
|
218 |
'confidence': confidence,
|
219 |
'time': inference_time
|
220 |
})
|
221 |
+
current_progress += progress_step
|
222 |
+
yield format_progress(current_progress, f"Completed {model_name}"), format_results(results)
|
223 |
|
224 |
+
# Process E5-based models
|
225 |
+
e5_embedding = None
|
226 |
+
for model_name, model in models.items():
|
227 |
+
if model_name in ['E5 Classifier', 'E5-Instruct Classifier']:
|
228 |
+
if e5_embedding is None: # Generate embedding only once
|
229 |
+
yield format_progress(current_progress, f"Generating E5 embeddings..."), format_results(results)
|
230 |
+
e5_embedding, embed_time = generate_e5_embedding(text)
|
231 |
+
|
232 |
+
start_time = time.time()
|
233 |
+
embedding_2d = e5_embedding.reshape(1, -1)
|
234 |
+
prediction = model.predict(embedding_2d)[0]
|
235 |
+
probabilities = model.predict_proba(embedding_2d)[0]
|
236 |
+
confidence = max(probabilities)
|
237 |
+
inference_time = time.time() - start_time
|
238 |
+
|
239 |
+
results.append({
|
240 |
+
'model': model_name,
|
241 |
+
'prediction': prediction,
|
242 |
+
'confidence': confidence,
|
243 |
+
'time': inference_time + embed_time
|
244 |
+
})
|
245 |
+
current_progress += progress_step
|
246 |
+
yield format_progress(current_progress, f"Completed {model_name}"), format_results(results)
|
247 |
|
248 |
+
# Process Azure-based models
|
249 |
+
azure_embedding = None
|
250 |
+
for model_name, model in models.items():
|
251 |
+
if model_name in ['Azure Classifier', 'Azure KNN Classifier']:
|
252 |
+
if azure_embedding is None: # Generate embedding only once
|
253 |
+
yield format_progress(current_progress, "Generating Azure embeddings..."), format_results(results)
|
254 |
+
azure_embedding, embed_time = get_azure_embedding(text)
|
255 |
+
|
256 |
+
start_time = time.time()
|
257 |
+
embedding_2d = azure_embedding.reshape(1, -1)
|
258 |
+
prediction = model.predict(embedding_2d)[0]
|
259 |
+
probabilities = model.predict_proba(embedding_2d)[0]
|
260 |
+
confidence = max(probabilities)
|
261 |
+
inference_time = time.time() - start_time
|
262 |
+
|
263 |
+
results.append({
|
264 |
+
'model': model_name,
|
265 |
+
'prediction': prediction,
|
266 |
+
'confidence': confidence,
|
267 |
+
'time': inference_time + embed_time
|
268 |
+
})
|
269 |
+
current_progress += progress_step
|
270 |
+
yield format_progress(current_progress, f"Completed {model_name}"), format_results(results)
|
271 |
+
|
272 |
+
# Process GTE model
|
273 |
+
if 'GTE Classifier' in models:
|
274 |
+
yield format_progress(current_progress, "Processing GTE Classifier..."), format_results(results)
|
275 |
+
gte_embedding, embed_time = generate_gte_embedding(text)
|
276 |
+
model = models['GTE Classifier']
|
277 |
+
embedding_2d = gte_embedding.reshape(1, -1)
|
278 |
prediction = model.predict(embedding_2d)[0]
|
279 |
probabilities = model.predict_proba(embedding_2d)[0]
|
280 |
confidence = max(probabilities)
|
281 |
inference_time = time.time() - start_time
|
282 |
|
283 |
results.append({
|
284 |
+
'model': 'GTE Classifier',
|
285 |
'prediction': prediction,
|
286 |
'confidence': confidence,
|
287 |
'time': inference_time + embed_time
|
288 |
})
|
289 |
+
current_progress = 100
|
290 |
+
yield format_progress(current_progress, "Completed!"), format_results(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
291 |
|
292 |
except Exception as e:
|
293 |
yield "", f"<div style='color: red; padding: 20px;'>Error occurred: {str(e)}</div>"
|
config.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
+
|
3 |
+
# FastText model paths configuration
|
4 |
+
FASTTEXT_CONFIG = {
|
5 |
+
"use_huggingface": True, # Set to True in production
|
6 |
+
"repo_id": "talhasarit41/fasttext", # HuggingFace repository ID
|
7 |
+
"huggingface_paths": {
|
8 |
+
"fasttext_default": "fasttext_raw.bin",
|
9 |
+
"fasttext_preprocessed": "fasttext_preprocessed.bin",
|
10 |
+
"word_n_gram_1": "word_n_gram_1.bin",
|
11 |
+
"word_n_gram_2": "word_n_gram_2.bin",
|
12 |
+
"word_n_gram_3": "word_n_gram_3.bin",
|
13 |
+
"low_overfit": "low_overfit.bin"
|
14 |
+
},
|
15 |
+
"local_paths": {
|
16 |
+
"fasttext_default": "/home/seeknndestroy/jetlink/bitbucket/fasttext_related/saved_models/fasttext_raw.bin",
|
17 |
+
"fasttext_preprocessed": "/home/seeknndestroy/jetlink/bitbucket/fasttext_related/saved_models/fasttext_preprocessed.bin",
|
18 |
+
"word_n_gram_1": "/home/seeknndestroy/jetlink/bitbucket/fasttext_related/saved_models/manual_configs/word_n_gram_1.bin",
|
19 |
+
"word_n_gram_2": "/home/seeknndestroy/jetlink/bitbucket/fasttext_related/saved_models/manual_configs/word_n_gram_2.bin",
|
20 |
+
"word_n_gram_3": "/home/seeknndestroy/jetlink/bitbucket/fasttext_related/saved_models/manual_configs/word_n_gram_3.bin",
|
21 |
+
"low_overfit": "/home/seeknndestroy/jetlink/bitbucket/fasttext_related/saved_models/manual_configs/low_overfit.bin"
|
22 |
+
}
|
23 |
+
}
|
24 |
+
|
25 |
+
# Model enablement configuration
|
26 |
+
MODEL_CONFIG = {
|
27 |
+
"FastText Default": True,
|
28 |
+
"Fasttext Low Overfit": True,
|
29 |
+
"Fasttext WordnNGram 1": True,
|
30 |
+
"Fasttext WordnNGram 2": True,
|
31 |
+
"Fasttext WordnNGram 3": True,
|
32 |
+
"E5 Classifier": False,
|
33 |
+
"E5-Instruct Classifier": False,
|
34 |
+
"Azure Classifier": False,
|
35 |
+
"Azure KNN Classifier": False,
|
36 |
+
"GTE Classifier": False
|
37 |
+
}
|
38 |
+
|
39 |
+
def get_fasttext_path(model_name: str) -> str:
|
40 |
+
"""Get the appropriate FastText model path based on configuration."""
|
41 |
+
if FASTTEXT_CONFIG["use_huggingface"]:
|
42 |
+
from huggingface_hub import hf_hub_download
|
43 |
+
return hf_hub_download(
|
44 |
+
repo_id=FASTTEXT_CONFIG["repo_id"],
|
45 |
+
filename=FASTTEXT_CONFIG["huggingface_paths"][model_name]
|
46 |
+
)
|
47 |
+
else:
|
48 |
+
return FASTTEXT_CONFIG["local_paths"][model_name]
|
49 |
+
|
50 |
+
def is_model_enabled(model_name: str) -> bool:
|
51 |
+
"""Check if a model is enabled in the configuration."""
|
52 |
+
return MODEL_CONFIG.get(model_name, False)
|