Create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import BertTokenizer, BertModel, T5ForConditionalGeneration
|
3 |
+
from transformers import AutoModelForSequenceClassification
|
4 |
+
from utilities import preprocess_features, generate_recommendation
|
5 |
+
|
6 |
+
MODEL_PATH = "your-hf-username/deal-score-model" # Replace with your model path
|
7 |
+
SUMMARIZER_PATH = "t5-small" # Can use distilgpt2 or other summarizers
|
8 |
+
|
9 |
+
def load_model():
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
11 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
|
12 |
+
summarizer = T5ForConditionalGeneration.from_pretrained(SUMMARIZER_PATH)
|
13 |
+
return model, tokenizer, summarizer
|
14 |
+
|
15 |
+
def predict_deal_score(input_data, model, tokenizer, summarizer):
|
16 |
+
features = preprocess_features(input_data)
|
17 |
+
inputs = tokenizer(features["text"], return_tensors="pt")
|
18 |
+
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model(**inputs)
|
21 |
+
score = round(outputs.logits[0][0].item() * 100)
|
22 |
+
confidence = torch.softmax(outputs.logits[0], dim=-1).max().item()
|
23 |
+
|
24 |
+
risk = (
|
25 |
+
"Low" if score > 75 else
|
26 |
+
"Medium" if score > 50 else
|
27 |
+
"High"
|
28 |
+
)
|
29 |
+
|
30 |
+
recommendation = generate_recommendation(summarizer, input_data)
|
31 |
+
|
32 |
+
return {
|
33 |
+
"score": score,
|
34 |
+
"confidence": round(confidence, 2),
|
35 |
+
"risk": risk,
|
36 |
+
"recommendation": recommendation
|
37 |
+
}
|