mohamedemam commited on
Commit
f35281e
1 Parent(s): 45def9f

Create pipline

Browse files
Files changed (1) hide show
  1. pipline +36 -0
pipline ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import Pipeline
2
+
3
+
4
+ class MyPipeline:
5
+
6
+ def __init__(self,model,tokenizer):
7
+ self.model=model
8
+ self.tokenizer=tokenizer
9
+
10
+ def chat_Format(self,context, quetion, answer):
11
+ return "Instruction:/n check answer is true or false of next quetion using context below:\n" + "#context: " + context + f".\n#quetion: " + quetion + f".\n#student answer: " + answer + ".\n#response:"
12
+
13
+
14
+ def __call__(self, context, quetion, answer,generate=1,max_new_tokens=4, num_beams=2, do_sample=False,num_return_sequences=1):
15
+ inp=self.chat_Format(context, quetion, answer)
16
+ w = self.tokenizer(inp, add_special_tokens=True,
17
+ pad_to_max_length=True,
18
+ return_attention_mask=True,
19
+ return_tensors='pt')
20
+ response=""
21
+ if(generate):
22
+ outputs = self.tokenizer.batch_decode(self.model.generate(input_ids=w['input_ids'].cuda(), attention_mask=w['attention_mask'].cuda(), max_new_tokens=max_new_tokens, num_beams=num_beams, do_sample=do_sample, num_return_sequences=num_return_sequences), skip_special_tokens=True)
23
+ response = outputs
24
+
25
+ s =self.model(input_ids=w['input_ids'].cuda(), attention_mask=w['attention_mask'].cuda())['logits'][0][-1]
26
+ s = F.softmax(s, dim=-1)
27
+ yes_token_id = self.tokenizer.convert_tokens_to_ids("صØŃÙĬØŃ")
28
+ no_token_id = self.tokenizer.convert_tokens_to_ids("خط")
29
+ print(yes_token_id,no_token_id)
30
+ for i in ["Yes", "yes", "True", "true","صحيح"]:
31
+ s[yes_token_id] += s[self.tokenizer.convert_tokens_to_ids(i)]
32
+ for i in ["No", "no", "False", "false","خطأ"]:
33
+ s[no_token_id] += s[self.tokenizer.convert_tokens_to_ids(i)]
34
+ true = (s[yes_token_id] / (s[no_token_id] + s[yes_token_id])).item()
35
+ return {"response": response, "true": true}
36
+