Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,145 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
## Overview
|
6 |
+
This example snipe code for running the VietCoMath-01 small model for mathematical Coding problem-solving and General Multi tasks.
|
7 |
+
This model is based from Qwen-2.5 14B.
|
8 |
+
|
9 |
+
#### Helper Functions
|
10 |
+
```python
|
11 |
+
import re
|
12 |
+
|
13 |
+
def check_patterns(response):
|
14 |
+
"""
|
15 |
+
Check if the response contains all required XML patterns.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
response (str): The model's generated response
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
str: Parsed response or 'Missing' if patterns are incomplete
|
22 |
+
"""
|
23 |
+
patterns = {
|
24 |
+
'answer': r'<answer>(.*?)</answer>',
|
25 |
+
'reflection': r'<reflection>(.*?)</reflection>',
|
26 |
+
'steps': r'<step>(.*?)</step>',
|
27 |
+
'count': r'<count>(.*?)</count>'
|
28 |
+
}
|
29 |
+
|
30 |
+
matches = {
|
31 |
+
'answer': re.search(patterns['answer'], response, re.DOTALL),
|
32 |
+
'reflection': re.search(patterns['reflection'], response, re.DOTALL),
|
33 |
+
'steps': re.findall(patterns['steps'], response, re.DOTALL),
|
34 |
+
'count': re.findall(patterns['count'], response, re.DOTALL)
|
35 |
+
}
|
36 |
+
|
37 |
+
return "Missing" if not all([matches['answer'], matches['reflection'], matches['steps'], matches['count']]) else response
|
38 |
+
|
39 |
+
def parse_response(response):
|
40 |
+
"""
|
41 |
+
Parse the model's response and extract key components.
|
42 |
+
|
43 |
+
Args:
|
44 |
+
response (str): The model's generated response
|
45 |
+
|
46 |
+
Returns:
|
47 |
+
tuple: Parsed answer, reflection, steps, and clarification
|
48 |
+
"""
|
49 |
+
response_check = check_patterns(response)
|
50 |
+
|
51 |
+
if response_check == "Missing":
|
52 |
+
clarification_match = re.search(r'<clarification>(.*?)</clarification>', response, re.DOTALL)
|
53 |
+
clarification = clarification_match.group(1).strip() if clarification_match else response
|
54 |
+
return "", "", [], clarification
|
55 |
+
else:
|
56 |
+
answer_match = re.search(r'<answer>(.*?)</answer>', response, re.DOTALL)
|
57 |
+
reflection_match = re.search(r'<reflection>(.*?)</reflection>', response, re.DOTALL)
|
58 |
+
|
59 |
+
answer = answer_match.group(1).strip() if answer_match else ""
|
60 |
+
reflection = reflection_match.group(1).strip() if reflection_match else ""
|
61 |
+
steps = re.findall(r'<step>(.*?)</step>', response, re.DOTALL)
|
62 |
+
|
63 |
+
return answer, reflection, steps, ""
|
64 |
+
```
|
65 |
+
|
66 |
+
|
67 |
+
## Usage
|
68 |
+
|
69 |
+
### Basic Text Generation
|
70 |
+
```python
|
71 |
+
import transformers
|
72 |
+
import torch
|
73 |
+
|
74 |
+
# Load the model
|
75 |
+
model_id = "VietnamAIHub/VietCoMath-o1-14B"
|
76 |
+
pipeline = transformers.pipeline(
|
77 |
+
"text-generation",
|
78 |
+
model=model_id,
|
79 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
80 |
+
device_map="auto",
|
81 |
+
)
|
82 |
+
|
83 |
+
|
84 |
+
# Example mathematical word problem
|
85 |
+
|
86 |
+
problem = "Có 100 sinh viên đỗ đại học. Trong số đó, có 55 sinh viên chọn âm nhạc, 44 sinh viên chọn thể thao, và 20 sinh viên chọn cả 2. Hỏi có bao nhiêu sinh viên không chọn âm nhạc, cũng không chọn thể thao?"
|
87 |
+
|
88 |
+
# Prepare messages
|
89 |
+
messages = [
|
90 |
+
{"role": "system", "content": ""},
|
91 |
+
{"role": "user", "content": f"{problem}"},
|
92 |
+
]
|
93 |
+
|
94 |
+
# Define terminators
|
95 |
+
terminators = [
|
96 |
+
pipeline.tokenizer.eos_token_id,
|
97 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|im_end|>")
|
98 |
+
]
|
99 |
+
|
100 |
+
# Generate text
|
101 |
+
outputs = pipeline(
|
102 |
+
messages,
|
103 |
+
max_new_tokens=256,
|
104 |
+
eos_token_id=terminators,
|
105 |
+
do_sample=True,
|
106 |
+
temperature=0.6,
|
107 |
+
top_p=0.9,
|
108 |
+
)
|
109 |
+
|
110 |
+
# Print generated text
|
111 |
+
generated_text=outputs[0]["generated_text"][-1]
|
112 |
+
|
113 |
+
answer, reflection, steps, clarification = parse_response(generated_text)
|
114 |
+
|
115 |
+
print(clarification)
|
116 |
+
print("------------Internal Thinking-------------")
|
117 |
+
print(steps)
|
118 |
+
print(reflection)
|
119 |
+
print("------------End of Internal Thinking-------------\n")
|
120 |
+
|
121 |
+
print("------------Final Answer-------------")
|
122 |
+
print(answer)
|
123 |
+
print("------------End of Answer-------------")
|
124 |
+
|
125 |
+
## Limitations
|
126 |
+
- The model is Small scale May Failed in Very difficult problems, Please check the result
|
127 |
+
|
128 |
+
|
129 |
+
## License
|
130 |
+
[Model is based LLama 3B]
|
131 |
+
|
132 |
+
## Citation
|
133 |
+
|
134 |
+
@misc {VietnamAIHub,
|
135 |
+
author = { {VietnamAIHub} },
|
136 |
+
title = { VietCoMath-o1-8B},
|
137 |
+
year = 2024,
|
138 |
+
url = { https://huggingface.co/VietnamAIHub/VietCoMath-o1-8B },
|
139 |
+
doi = { 10.57967/hf/3743 },
|
140 |
+
publisher = { Hugging Face }
|
141 |
+
}
|
142 |
+
|
143 |
+
## Collaboration & Contribution
|
144 |
+
Bạn có thể kết nối trực tiếp với Trần Nhiệm [email protected]
|
145 |
+
Hoặc có thể chat trực tiếp ở: LinkedIn Facebook. X. Zalo +886 934 311 751
|