Spaces:
Sleeping
Sleeping
YashaswiAbhinav commited on
Commit ·
9951a33
1
Parent(s): 2af28b1
Move LSTMClassifier to models.py to fix loading issue
Browse files98 unmodified lines9910010110210310410510610710810911011111211311411511611711811912098 unmodified linesdef predict_distilbert_prob(tokenizer, model, text):if tokenizer is None or model is None:return Noneinputs = tokenizer(preprocess_text(text),return_tensors="pt",truncation=True,padding='max_length',max_length=128)with torch.no_grad():return torch.softmax(model(**inputs).logits, dim=1)[0][1].item()def predict_combined(model_components, text):tfidf, svd, lr_model, rf_model, tokenizer_lstm, lstm_model, max_len, distilbert_model, tokenizer_distilbert = model_componentsp_lr = predict_lr_prob(tfidf, lr_model, text)p_rf = predict_rf_prob(tfidf, svd, rf_model, text)p_lstm = predict_lstm_prob(tokenizer_lstm, lstm_model, max_len, text)p_distilbert = predict_distilbert_prob(tokenizer_distilbert, distilbert_model, text)if p_distilbert is not None:avg_prob = (p_lr + p_rf + p_lstm + p_distilbert) / 4else:98 unmodified lines9910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013198 unmodified linesdef predict_distilbert_prob(tokenizer, model, text):if tokenizer is None or model is None:return Nonetry:inputs = tokenizer(preprocess_text(text),return_tensors="pt",truncation=True,padding='max_length',max_length=128)with torch.no_grad():return torch.softmax(model(**inputs).logits, dim=1)[0][1].item()except AttributeError:# Handle the missing _pad_token attribute errorprint("Warning: DistilBERT tokenizer issue encountered. Skipping DistilBERT predictions.")return Nonedef predict_combined(model_components, text):tfidf, svd, lr_model, rf_model, tokenizer_lstm, lstm_model, max_len, distilbert_model, tokenizer_distilbert = model_componentsp_lr = predict_lr_prob(tfidf, lr_model, text)p_rf = predict_rf_prob(tfidf, svd, rf_model, text)p_lstm = predict_lstm_prob(tokenizer_lstm, lstm_model, max_len, text)try:p_distilbert = predict_distilbert_prob(tokenizer_distilbert, distilbert_model, text)except Exception as e:print(f"DistilBERT error: {e}")p_distilbert = Noneif p_distilbert is not None:avg_prob = (p_lr + p_rf + p_lstm + p_distilbert) / 4else:
app.py+22-11