Update gen_ai.py
Browse files
gen_ai.py
CHANGED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
2 |
+
import torch
|
3 |
+
|
4 |
+
model_path = "finetuned_model_backup"
|
5 |
+
|
6 |
+
# Move the model to the appropriate device (GPU if available)
|
7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
+
# Set the model to evaluation mode
|
9 |
+
label_dict = {
|
10 |
+
"bank_service": 0,
|
11 |
+
"credit_card": 1,
|
12 |
+
"credit_reporting": 2,
|
13 |
+
"debt_collection": 3,
|
14 |
+
"loan": 4,
|
15 |
+
"money_transfers": 5,
|
16 |
+
"mortgage": 6
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
class traditional_model:
|
22 |
+
def __init__(self, query):
|
23 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
24 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
25 |
+
|
26 |
+
def predict(self):
|
27 |
+
self.model.to(device)
|
28 |
+
self.model.eval()
|
29 |
+
inputs = self.tokenizer(self.query, return_tensors="pt", truncation=True, padding=True).to(device) # Move input to device
|
30 |
+
with torch.no_grad():
|
31 |
+
outputs = self.model(**inputs)
|
32 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
33 |
+
prediction = list(label_dict.keys())[predicted_class]
|
34 |
+
return prediction
|
35 |
+
class ResultB:
|
36 |
+
def __init__(self, query):
|
37 |
+
self.response = f"Result from Function B for query: {query}"
|
38 |
+
|
39 |
+
class ResultC:
|
40 |
+
def __init__(self, query):
|
41 |
+
self.response = f"Result from Function C for query: {query}"
|