Update train.py
Browse files
train.py
CHANGED
|
@@ -21,7 +21,6 @@ BOOK_TITLES = [
|
|
| 21 |
"The Road to Wigan Pier", "Burmese Days"
|
| 22 |
]
|
| 23 |
|
| 24 |
-
# Maps for conversion
|
| 25 |
spelled_to_digit = {"one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6,
|
| 26 |
"seven": 7, "eight": 8, "nine": 9, "ten": 10, "eleven": 11, "twelve": 12,
|
| 27 |
"thirteen": 13, "fourteen": 14, "fifteen": 15, "sixteen": 16, "seventeen": 17,
|
|
@@ -29,11 +28,10 @@ spelled_to_digit = {"one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six":
|
|
| 29 |
|
| 30 |
digit_to_spelled = {v: k for k, v in spelled_to_digit.items()}
|
| 31 |
|
| 32 |
-
digit_to_roman = {
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
}
|
| 37 |
|
| 38 |
ordinal_to_digit = {
|
| 39 |
"first": 1, "second": 2, "third": 3, "fourth": 4, "fifth": 5, "sixth": 6,
|
|
@@ -44,7 +42,6 @@ ordinal_to_digit = {
|
|
| 44 |
|
| 45 |
roman_to_int = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
|
| 46 |
|
| 47 |
-
# ChromaDB setup
|
| 48 |
print("Connecting to ChromaDB...")
|
| 49 |
chroma_client = HttpClient(host="34.126.200.250", port=8000)
|
| 50 |
collection = chroma_client.get_collection(name="orwell_books")
|
|
@@ -88,7 +85,6 @@ def retrieve_context(prompt, top_k=1):
|
|
| 88 |
)
|
| 89 |
if not results['documents'] or not results['documents'][0]:
|
| 90 |
return [{"content": "No relevant context found.", "meta": {}}]
|
| 91 |
-
|
| 92 |
return [
|
| 93 |
{"content": doc, "meta": meta}
|
| 94 |
for doc, meta in zip(results['documents'][0], results['metadatas'][0])
|
|
@@ -117,7 +113,6 @@ def get_part_number(prompt):
|
|
| 117 |
part_keywords = {"chapter", "part", "section"}
|
| 118 |
number_pattern = re.compile(r"^\\d+$")
|
| 119 |
roman_pattern = re.compile(r"^[IVXLCDM]+$", re.IGNORECASE)
|
| 120 |
-
|
| 121 |
for i, token in enumerate(doc):
|
| 122 |
token_lower = token.text.lower()
|
| 123 |
if token_lower in spelled_to_digit or token_lower in ordinal_to_digit or roman_pattern.match(token_lower) or number_pattern.match(token_lower):
|
|
@@ -138,9 +133,19 @@ def get_prompt(contexts, user_prompt=""):
|
|
| 138 |
- Each inline citation must correspond to a **source reference at the end** of the response, using this exact format:
|
| 139 |
[1] Book Title, Chapter X
|
| 140 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
output_format = f"""
|
| 142 |
Question
|
| 143 |
-
{
|
| 144 |
Supporting context
|
| 145 |
|
| 146 |
Sources:
|
|
@@ -186,7 +191,6 @@ def add_reward(example):
|
|
| 186 |
def reward_calculate(completions, **kwargs):
|
| 187 |
return kwargs["reward"]
|
| 188 |
|
| 189 |
-
# Load Dataset
|
| 190 |
print("Loading raw dataset...")
|
| 191 |
raw_dataset = load_dataset(DATA_ID, split="train")
|
| 192 |
train_dataset = raw_dataset.map(make_format).map(add_reward).remove_columns([
|
|
@@ -239,4 +243,4 @@ trainer.train()
|
|
| 239 |
print("Saving and pushing model...")
|
| 240 |
trainer.save_model(training_args.output_dir)
|
| 241 |
trainer.push_to_hub(dataset_name=DATA_ID)
|
| 242 |
-
print("Training complete and model uploaded!")
|
|
|
|
| 21 |
"The Road to Wigan Pier", "Burmese Days"
|
| 22 |
]
|
| 23 |
|
|
|
|
| 24 |
spelled_to_digit = {"one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6,
|
| 25 |
"seven": 7, "eight": 8, "nine": 9, "ten": 10, "eleven": 11, "twelve": 12,
|
| 26 |
"thirteen": 13, "fourteen": 14, "fifteen": 15, "sixteen": 16, "seventeen": 17,
|
|
|
|
| 28 |
|
| 29 |
digit_to_spelled = {v: k for k, v in spelled_to_digit.items()}
|
| 30 |
|
| 31 |
+
digit_to_roman = {v: r for v, r in enumerate(
|
| 32 |
+
["i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii", "xiii", "xiv", "xv", "xvi", "xvii", "xviii", "xix", "xx"],
|
| 33 |
+
start=1
|
| 34 |
+
)}
|
|
|
|
| 35 |
|
| 36 |
ordinal_to_digit = {
|
| 37 |
"first": 1, "second": 2, "third": 3, "fourth": 4, "fifth": 5, "sixth": 6,
|
|
|
|
| 42 |
|
| 43 |
roman_to_int = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
|
| 44 |
|
|
|
|
| 45 |
print("Connecting to ChromaDB...")
|
| 46 |
chroma_client = HttpClient(host="34.126.200.250", port=8000)
|
| 47 |
collection = chroma_client.get_collection(name="orwell_books")
|
|
|
|
| 85 |
)
|
| 86 |
if not results['documents'] or not results['documents'][0]:
|
| 87 |
return [{"content": "No relevant context found.", "meta": {}}]
|
|
|
|
| 88 |
return [
|
| 89 |
{"content": doc, "meta": meta}
|
| 90 |
for doc, meta in zip(results['documents'][0], results['metadatas'][0])
|
|
|
|
| 113 |
part_keywords = {"chapter", "part", "section"}
|
| 114 |
number_pattern = re.compile(r"^\\d+$")
|
| 115 |
roman_pattern = re.compile(r"^[IVXLCDM]+$", re.IGNORECASE)
|
|
|
|
| 116 |
for i, token in enumerate(doc):
|
| 117 |
token_lower = token.text.lower()
|
| 118 |
if token_lower in spelled_to_digit or token_lower in ordinal_to_digit or roman_pattern.match(token_lower) or number_pattern.match(token_lower):
|
|
|
|
| 133 |
- Each inline citation must correspond to a **source reference at the end** of the response, using this exact format:
|
| 134 |
[1] Book Title, Chapter X
|
| 135 |
"""
|
| 136 |
+
if question_type == "MCQs":
|
| 137 |
+
answer_block = (
|
| 138 |
+
"Answer options:\n"
|
| 139 |
+
" A. Option A\n"
|
| 140 |
+
" B. Option B\n"
|
| 141 |
+
" C. Option C\n"
|
| 142 |
+
" D. Option D\n"
|
| 143 |
+
)
|
| 144 |
+
else:
|
| 145 |
+
answer_block = ""
|
| 146 |
output_format = f"""
|
| 147 |
Question
|
| 148 |
+
{answer_block}
|
| 149 |
Supporting context
|
| 150 |
|
| 151 |
Sources:
|
|
|
|
| 191 |
def reward_calculate(completions, **kwargs):
|
| 192 |
return kwargs["reward"]
|
| 193 |
|
|
|
|
| 194 |
print("Loading raw dataset...")
|
| 195 |
raw_dataset = load_dataset(DATA_ID, split="train")
|
| 196 |
train_dataset = raw_dataset.map(make_format).map(add_reward).remove_columns([
|
|
|
|
| 243 |
print("Saving and pushing model...")
|
| 244 |
trainer.save_model(training_args.output_dir)
|
| 245 |
trainer.push_to_hub(dataset_name=DATA_ID)
|
| 246 |
+
print("Training complete and model uploaded!")
|