anhvv200053 commited on
Commit
e7e26d2
·
verified ·
1 Parent(s): fa9bd04

Upload 3 files

Browse files
Files changed (3) hide show
  1. model_setup.py +25 -0
  2. requirments.txt +11 -0
  3. response_generator.py +81 -0
model_setup.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3
+
4
+ # Cấu hình BitsAndBytes để tải mô hình 4-bit
5
+ bnb_config = BitsAndBytesConfig(
6
+ load_in_4bit=True,
7
+ bnb_4bit_quant_type='nf4',
8
+ bnb_4bit_compute_dtype="float16",
9
+ bnb_4bit_use_double_quant=False,
10
+ )
11
+
12
+ # Thiết lập mô hình và tokenizer
13
+ def load_model():
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ "anhvv200053/Vinallama-2-7B-updated1-instruction-v2",
16
+ quantization_config=bnb_config,
17
+ device_map={"": 0},
18
+ use_auth_token=True
19
+ )
20
+ model.config.pretraining_tp = 1
21
+
22
+ tokenizer = AutoTokenizer.from_pretrained('anhvv200053/Vinallama-2-7B-updated1-instruction-v2', trust_remote_code=True, use_fast=True)
23
+ tokenizer.pad_token = tokenizer.eos_token
24
+
25
+ return model, tokenizer
requirments.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ datasets
4
+ bitsandbytes
5
+ accelerate
6
+ sentence-transformers
7
+ peft
8
+ trl
9
+ rouge-score
10
+ sentencepiece
11
+ gradio
response_generator.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ def remove_duplicates(text):
4
+ lines = text.splitlines(keepends=True)
5
+ seen = set()
6
+ cleaned_lines = []
7
+
8
+ for line in lines:
9
+ stripped_line = line.strip()
10
+ if stripped_line not in seen or stripped_line == "":
11
+ if stripped_line != "":
12
+ seen.add(stripped_line)
13
+ cleaned_lines.append(line)
14
+
15
+ final_lines = []
16
+ previous_line_was_blank = False
17
+
18
+ for line in cleaned_lines:
19
+ if line.strip() == "":
20
+ if not previous_line_was_blank:
21
+ final_lines.append(line)
22
+ previous_line_was_blank = True
23
+ else:
24
+ final_lines.append(line)
25
+ previous_line_was_blank = False
26
+
27
+ return ''.join(final_lines)
28
+
29
+ def clean_output_new(generated_text):
30
+ match = re.search(r"assistant:(.*)", generated_text, re.DOTALL)
31
+ if match:
32
+ cleaned_text = match.group(1).strip()
33
+ cleaned_text = re.sub(r'<\|im_end\|>|<\|im_start\|>', '', cleaned_text).strip()
34
+
35
+ question_match = re.search(r"Câu hỏi", cleaned_text)
36
+ if question_match:
37
+ cleaned_text = cleaned_text[:question_match.start()].strip()
38
+
39
+ first_answer_match = re.search(r"Trả lời:", cleaned_text)
40
+ if first_answer_match:
41
+ second_answer_match = re.search(r"Trả lời:", cleaned_text[first_answer_match.end():])
42
+ if second_answer_match:
43
+ cleaned_text = cleaned_text[:first_answer_match.start() + second_answer_match.start()].strip()
44
+
45
+ cleaned_text = remove_duplicates(cleaned_text)
46
+
47
+ if cleaned_text.strip() == "" or cleaned_text.strip() == ".":
48
+ return "Rất tiếc tôi không có thông tin về câu hỏi của bạn."
49
+
50
+ return cleaned_text
51
+
52
+ return "Xin lỗi, tôi không thể trả lời câu hỏi của bạn. Bạn có thể cung cấp thêm thông tin để tôi có thể giúp đỡ."
53
+
54
+ def process_output(text):
55
+ sentences = re.split(r'(?<=[.!?]) +', text.strip())
56
+
57
+ if not re.match(r'[.!?]$', sentences[-1]):
58
+ sentences = sentences[:-1]
59
+
60
+ return ' '.join(sentences)
61
+
62
+ def generate_response(user_input, model, tokenizer):
63
+ from transformers import pipeline
64
+
65
+ pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer,
66
+ max_length=350,
67
+ num_beams=2,
68
+ repetition_penalty=1.2,
69
+ top_p=0.85,
70
+ top_k=35,
71
+ truncation=True,
72
+ eos_token_id=tokenizer.eos_token_id)
73
+
74
+ result = pipe(f"""<|im_start|>system: Bạn là một trợ lí AI hữu ích. Hãy trả lời người dùng một cách ngắn gọn và chính xác. Không lặp lại thông tin và không thêm thông tin không cần thiết<|im_end|>
75
+ <|im_start|>user: {user_input}<|im_end|>
76
+ <|im_start|>assistant:""")[0]['generated_text']
77
+
78
+ final_output = clean_output_new(result)
79
+ final_output = process_output(final_output)
80
+
81
+ return final_output