Pclanglais commited on
Commit
4a1b022
1 Parent(s): 428b114

Upload inference_chatrag.py

Browse files
Files changed (1) hide show
  1. inference_chatrag.py +39 -0
inference_chatrag.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import torch
3
+ import numpy as np
4
+
5
+ from transformers import AutoModelForSequenceClassification
6
+ from transformers import AutoTokenizer
7
+
8
+ model = AutoModelForSequenceClassification.from_pretrained("deberta-classification-chatrag/checkpoint-6342")
9
+ tokenizer = AutoTokenizer.from_pretrained("deberta-classification-chatrag/checkpoint-6342")
10
+
11
+
12
+ result = ["Comment puis-je renouveler un passeport ?", "Combien font deux et deux ?", "Écris un début de lettre de recommandation pour la Dinum"]
13
+
14
+ result = pd.DataFrame(result, columns=['query'])
15
+
16
+ complete_probabilities = []
17
+
18
+ for text in result["query"].tolist():
19
+ encoding = tokenizer(text, return_tensors="pt")
20
+ encoding = {k: v.to(model.device) for k,v in encoding.items()}
21
+
22
+ outputs = model(**encoding)
23
+
24
+ logits = outputs.logits
25
+ logits.shape
26
+
27
+ # apply sigmoid + threshold
28
+ sigmoid = torch.nn.Sigmoid()
29
+ probs = sigmoid(logits.squeeze().cpu())
30
+ predictions = np.zeros(probs.shape)
31
+
32
+ # Extract the float value from the tensor
33
+ float_value = probs.item()
34
+
35
+ complete_probabilities.append(float_value)
36
+
37
+ result["prob"] = complete_probabilities
38
+
39
+ print(result)