Pedrinho-Dev01 commited on
Commit
204268b
·
1 Parent(s): c7d60e2

DeBERTa Removal

Browse files
Files changed (1) hide show
  1. api.py +5 -16
api.py CHANGED
@@ -1,7 +1,7 @@
1
  """
2
  Spam Detection + Emotion Analysis API
3
  Ensemble of RoBERTa-Large + ELECTRA-Large classifiers for spam,
4
- and RoBERTa-Large + ELECTRA-Large + DeBERTa-v3-Large for emotion.
5
  Run with: uvicorn api:app --reload
6
  """
7
 
@@ -16,7 +16,6 @@ from fastapi.middleware.cors import CORSMiddleware
16
  from pydantic import BaseModel
17
  from transformers import (
18
  AutoTokenizer,
19
- DebertaV2ForSequenceClassification,
20
  ElectraForSequenceClassification,
21
  RobertaForSequenceClassification,
22
  )
@@ -27,7 +26,6 @@ ROBERTA_SPAM_REPO = "Dpedrinho01/trained_roberta_large"
27
  ELECTRA_SPAM_REPO = "Dpedrinho01/trained_electra_large"
28
  ROBERTA_EMOTION_REPO = "Dpedrinho01/trained_roberta_emotion"
29
  ELECTRA_EMOTION_REPO = "Dpedrinho01/trained_electra_emotion"
30
- DEBERTA_EMOTION_REPO = "Dpedrinho01/trained_deberta_v3_large_emotion"
31
 
32
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
33
 
@@ -127,19 +125,17 @@ roberta_spam_bundle: Optional[SpamModelBundle] = None
127
  electra_spam_bundle: Optional[SpamModelBundle] = None
128
  roberta_emotion_bundle: Optional[EmotionModelBundle] = None
129
  electra_emotion_bundle: Optional[EmotionModelBundle] = None
130
- deberta_emotion_bundle: Optional[EmotionModelBundle] = None
131
 
132
 
133
  @app.on_event("startup")
134
  def load_models():
135
  global roberta_spam_bundle, electra_spam_bundle
136
- global roberta_emotion_bundle, electra_emotion_bundle, deberta_emotion_bundle
137
 
138
  roberta_spam_bundle = SpamModelBundle(ROBERTA_SPAM_REPO, RobertaForSequenceClassification)
139
  electra_spam_bundle = SpamModelBundle(ELECTRA_SPAM_REPO, ElectraForSequenceClassification)
140
  roberta_emotion_bundle = EmotionModelBundle(ROBERTA_EMOTION_REPO, RobertaForSequenceClassification)
141
  electra_emotion_bundle = EmotionModelBundle(ELECTRA_EMOTION_REPO, ElectraForSequenceClassification)
142
- deberta_emotion_bundle = EmotionModelBundle(DEBERTA_EMOTION_REPO, DebertaV2ForSequenceClassification)
143
  print(f"All models ready on {DEVICE}.")
144
 
145
 
@@ -184,7 +180,6 @@ class EmotionPredictResponse(BaseModel):
184
  all_scores: list[EmotionScore] # ensemble averaged, sorted by probability
185
  roberta: Optional[EmotionModelResult] = None
186
  electra: Optional[EmotionModelResult] = None
187
- deberta: Optional[EmotionModelResult] = None
188
 
189
 
190
  class EmlRequest(BaseModel):
@@ -207,17 +202,15 @@ def classify_spam(proba: float, threshold: float) -> dict:
207
  def ensemble_emotions(
208
  roberta_probas: dict[str, float],
209
  electra_probas: dict[str, float],
210
- deberta_probas: dict[str, float],
211
  threshold_per_class: dict[str, float],
212
  ) -> tuple[list[str], list[EmotionScore]]:
213
- """Average all three models' probabilities and apply per-class thresholds."""
214
  all_scores: list[EmotionScore] = []
215
  detected: list[str] = []
216
 
217
  for emotion, r_prob in roberta_probas.items():
218
  e_prob = electra_probas.get(emotion, 0.0)
219
- d_prob = deberta_probas.get(emotion, 0.0)
220
- avg_prob = round((r_prob + e_prob + d_prob) / 3, 4)
221
  threshold = threshold_per_class.get(emotion, 0.4)
222
  is_detected = avg_prob >= threshold
223
  all_scores.append(EmotionScore(
@@ -296,7 +289,6 @@ def health():
296
  "emotion_models_loaded": (
297
  roberta_emotion_bundle is not None
298
  and electra_emotion_bundle is not None
299
- and deberta_emotion_bundle is not None
300
  ),
301
  }
302
 
@@ -356,13 +348,11 @@ def predict_emotion(req: EmotionPredictRequest):
356
 
357
  roberta_probas = roberta_emotion_bundle.predict_proba(req.text)
358
  electra_probas = electra_emotion_bundle.predict_proba(req.text)
359
- deberta_probas = deberta_emotion_bundle.predict_proba(req.text)
360
 
361
- # Use roberta's per-class thresholds (all models share the same config structure)
362
  detected, all_scores = ensemble_emotions(
363
  roberta_probas,
364
  electra_probas,
365
- deberta_probas,
366
  roberta_emotion_bundle.threshold_per_class,
367
  )
368
 
@@ -372,7 +362,6 @@ def predict_emotion(req: EmotionPredictRequest):
372
  all_scores=all_scores,
373
  roberta=_emotion_model_result(roberta_emotion_bundle, roberta_probas),
374
  electra=_emotion_model_result(electra_emotion_bundle, electra_probas),
375
- deberta=_emotion_model_result(deberta_emotion_bundle, deberta_probas),
376
  )
377
 
378
 
 
1
  """
2
  Spam Detection + Emotion Analysis API
3
  Ensemble of RoBERTa-Large + ELECTRA-Large classifiers for spam,
4
+ and RoBERTa-Large + ELECTRA-Large for emotion.
5
  Run with: uvicorn api:app --reload
6
  """
7
 
 
16
  from pydantic import BaseModel
17
  from transformers import (
18
  AutoTokenizer,
 
19
  ElectraForSequenceClassification,
20
  RobertaForSequenceClassification,
21
  )
 
26
  ELECTRA_SPAM_REPO = "Dpedrinho01/trained_electra_large"
27
  ROBERTA_EMOTION_REPO = "Dpedrinho01/trained_roberta_emotion"
28
  ELECTRA_EMOTION_REPO = "Dpedrinho01/trained_electra_emotion"
 
29
 
30
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
31
 
 
125
  electra_spam_bundle: Optional[SpamModelBundle] = None
126
  roberta_emotion_bundle: Optional[EmotionModelBundle] = None
127
  electra_emotion_bundle: Optional[EmotionModelBundle] = None
 
128
 
129
 
130
  @app.on_event("startup")
131
  def load_models():
132
  global roberta_spam_bundle, electra_spam_bundle
133
+ global roberta_emotion_bundle, electra_emotion_bundle
134
 
135
  roberta_spam_bundle = SpamModelBundle(ROBERTA_SPAM_REPO, RobertaForSequenceClassification)
136
  electra_spam_bundle = SpamModelBundle(ELECTRA_SPAM_REPO, ElectraForSequenceClassification)
137
  roberta_emotion_bundle = EmotionModelBundle(ROBERTA_EMOTION_REPO, RobertaForSequenceClassification)
138
  electra_emotion_bundle = EmotionModelBundle(ELECTRA_EMOTION_REPO, ElectraForSequenceClassification)
 
139
  print(f"All models ready on {DEVICE}.")
140
 
141
 
 
180
  all_scores: list[EmotionScore] # ensemble averaged, sorted by probability
181
  roberta: Optional[EmotionModelResult] = None
182
  electra: Optional[EmotionModelResult] = None
 
183
 
184
 
185
  class EmlRequest(BaseModel):
 
202
  def ensemble_emotions(
203
  roberta_probas: dict[str, float],
204
  electra_probas: dict[str, float],
 
205
  threshold_per_class: dict[str, float],
206
  ) -> tuple[list[str], list[EmotionScore]]:
207
+ """Average both models' probabilities and apply per-class thresholds."""
208
  all_scores: list[EmotionScore] = []
209
  detected: list[str] = []
210
 
211
  for emotion, r_prob in roberta_probas.items():
212
  e_prob = electra_probas.get(emotion, 0.0)
213
+ avg_prob = round((r_prob + e_prob) / 2, 4)
 
214
  threshold = threshold_per_class.get(emotion, 0.4)
215
  is_detected = avg_prob >= threshold
216
  all_scores.append(EmotionScore(
 
289
  "emotion_models_loaded": (
290
  roberta_emotion_bundle is not None
291
  and electra_emotion_bundle is not None
 
292
  ),
293
  }
294
 
 
348
 
349
  roberta_probas = roberta_emotion_bundle.predict_proba(req.text)
350
  electra_probas = electra_emotion_bundle.predict_proba(req.text)
 
351
 
352
+ # Use roberta's per-class thresholds (both models share the same config structure)
353
  detected, all_scores = ensemble_emotions(
354
  roberta_probas,
355
  electra_probas,
 
356
  roberta_emotion_bundle.threshold_per_class,
357
  )
358
 
 
362
  all_scores=all_scores,
363
  roberta=_emotion_model_result(roberta_emotion_bundle, roberta_probas),
364
  electra=_emotion_model_result(electra_emotion_bundle, electra_probas),
 
365
  )
366
 
367