haisonle001 commited on
Commit
f6a1352
·
verified ·
1 Parent(s): efd9ca5

Upload infer.py

Browse files
Files changed (1) hide show
  1. infer.py +49 -0
infer.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unsloth import FastLanguageModel
2
+ import torch
3
+ max_seq_length = 2048
4
+ dtype = None
5
+ load_in_4bit = True
6
+
7
+ import requests
8
+ question= "Đi xe đè vạch màu vàng xử lý như thế nào?"
9
+ url = <retrieval_endpoint>
10
+ retrieve = {"query": [question]}
11
+ response = requests.post(url, json=retrieve)
12
+ context= response.json()['predict'][0][0][0]['top_relevant_chunks']
13
+
14
+ import random
15
+ def process_context(docs:list) -> list:
16
+ res=[]
17
+ for context in docs:
18
+ res.append(context.get("text",""))
19
+ return res[:10]
20
+ context= '\n'.join(process_context(context))
21
+
22
+ model, tokenizer = FastLanguageModel.from_pretrained(
23
+ model_name = 'NaverHustQA/LawVinaLlama',
24
+ max_seq_length = max_seq_length,
25
+ dtype = dtype,
26
+ load_in_4bit = load_in_4bit,
27
+ )
28
+ prompt_context = """Bạn là một tư vấn viên hữu ích về luật.
29
+ ### Instruction and Input:
30
+ Dựa vào ngữ cảnh/tài liệu sau:
31
+ {}
32
+ Hãy trả lời câu hỏi: {}
33
+
34
+ ### Câu trả lời:
35
+ {}
36
+ """
37
+
38
+ FastLanguageModel.for_inference(model) # Enable native 2x faster inference
39
+ inputs = tokenizer(
40
+ [
41
+ prompt_context.format(
42
+ context, # instruction
43
+ question, # input
44
+ "",
45
+ )
46
+ ], return_tensors = "pt").to("cuda")
47
+
48
+ outputs = model.generate(**inputs, max_new_tokens = 1024, use_cache = True)
49
+ print(tokenizer.batch_decode(outputs)[0].split("### Câu trả lời:")[1])