Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel, Trainer, TrainingArguments, DataCollatorForLanguageModeling
|
| 3 |
+
from peft import LoraConfig, get_peft_model
|
| 4 |
+
from datasets import Dataset
|
| 5 |
+
import gradio as gr # Import Gradio for UI
|
| 6 |
+
|
| 7 |
+
# Define a dataset of 20 incorrect math memes with corrections and explanations.
|
| 8 |
+
data = [
|
| 9 |
+
{"text": "Incorrect: 8 ÷ 2(2+2) = 1? Correct: 8 ÷ 2(2+2) = 16. Explanation: Evaluate parentheses first then perform division and multiplication sequentially."},
|
| 10 |
+
{"text": "Incorrect: 5 + 5 = 20? Correct: 5 + 5 = 10. Explanation: Simple addition error."},
|
| 11 |
+
{"text": "Incorrect: 6 * 6 = 36 but 6 / 6 = 6? Correct: 6 / 6 = 1. Explanation: A number divided by itself equals 1."},
|
| 12 |
+
{"text": "Incorrect: 2^3 = 6? Correct: 2^3 = 8. Explanation: 2 cubed is 8."},
|
| 13 |
+
{"text": "Incorrect: √16 = 5? Correct: √16 = 4. Explanation: The square root of 16 is 4."},
|
| 14 |
+
{"text": "Incorrect: 9 - 3 = 3? Correct: 9 - 3 = 6. Explanation: Correct subtraction yields 6."},
|
| 15 |
+
{"text": "Incorrect: 4 * 4 = 8? Correct: 4 * 4 = 16. Explanation: Multiplication error."},
|
| 16 |
+
{"text": "Incorrect: 10 / 2 = 10? Correct: 10 / 2 = 5. Explanation: Division error."},
|
| 17 |
+
{"text": "Incorrect: 15% of 200 = 50? Correct: 15% of 200 = 30. Explanation: 15% of 200 equals 30."},
|
| 18 |
+
{"text": "Incorrect: 100 / 4 = 20? Correct: 100 / 4 = 25. Explanation: Division error."},
|
| 19 |
+
{"text": "Incorrect: 3 + 7 = 11? Correct: 3 + 7 = 10. Explanation: 3 plus 7 equals 10."},
|
| 20 |
+
{"text": "Incorrect: 2 * 3 + 4 = 14? Correct: 2 * 3 + 4 = 10. Explanation: Follow order of operations: multiply then add."},
|
| 21 |
+
{"text": "Incorrect: 12 / 3 * 2 = 10? Correct: 12 / 3 * 2 = 8. Explanation: 12 divided by 3 is 4; 4 times 2 is 8."},
|
| 22 |
+
{"text": "Incorrect: 7 * 7 = 42? Correct: 7 * 7 = 49. Explanation: Multiplication error."},
|
| 23 |
+
{"text": "Incorrect: 14 - 7 = 8? Correct: 14 - 7 = 7. Explanation: Subtraction error."},
|
| 24 |
+
{"text": "Incorrect: (3 + 2) * 2 = 12? Correct: (3 + 2) * 2 = 10. Explanation: Add first, then multiply."},
|
| 25 |
+
{"text": "Incorrect: 50% of 100 = 60? Correct: 50% of 100 = 50. Explanation: 50% is half of 100."},
|
| 26 |
+
{"text": "Incorrect: 9 + 9 = 18 then 18 / 2 = 10? Correct: 18 / 2 = 9. Explanation: Division error."},
|
| 27 |
+
{"text": "Incorrect: 5! = 100? Correct: 5! = 120. Explanation: 5 factorial is 120."},
|
| 28 |
+
{"text": "Incorrect: 3^2 + 4^2 = 14? Correct: 3^2 + 4^2 = 25. Explanation: 9 + 16 equals 25."}
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# Convert the list to a Hugging Face Dataset.
|
| 32 |
+
dataset = Dataset.from_list(data)
|
| 33 |
+
print("Dataset created with", len(dataset), "examples.")
|
| 34 |
+
|
| 35 |
+
# Load the GPT-2 tokenizer and model.
|
| 36 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 37 |
+
# GPT-2 does not have an official pad token; use the eos_token.
|
| 38 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 39 |
+
|
| 40 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 41 |
+
|
| 42 |
+
# Configure LoRA for efficient fine-tuning.
|
| 43 |
+
lora_config = LoraConfig(
|
| 44 |
+
task_type="CAUSAL_LM", # For language modeling.
|
| 45 |
+
r=8,
|
| 46 |
+
lora_alpha=32,
|
| 47 |
+
lora_dropout=0.1
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Wrap the model with LoRA.
|
| 51 |
+
model = get_peft_model(model, lora_config)
|
| 52 |
+
print("Model loaded and LoRA configured.")
|
| 53 |
+
|
| 54 |
+
# Tokenize each example.
|
| 55 |
+
def tokenize_function(example):
|
| 56 |
+
return tokenizer(example["text"], truncation=True, max_length=128, padding="max_length")
|
| 57 |
+
|
| 58 |
+
tokenized_dataset = dataset.map(tokenize_function, batched=False)
|
| 59 |
+
tokenized_dataset.set_format(type="torch", columns=["input_ids", "attention_mask"])
|
| 60 |
+
print("Dataset tokenized.")
|
| 61 |
+
|
| 62 |
+
training_args = TrainingArguments(
|
| 63 |
+
output_dir="output",
|
| 64 |
+
per_device_train_batch_size=1,
|
| 65 |
+
num_train_epochs=5, # Increase epochs to help the model learn from 20 examples.
|
| 66 |
+
logging_steps=1,
|
| 67 |
+
save_strategy="epoch",
|
| 68 |
+
learning_rate=3e-5, # Slightly lower learning rate.
|
| 69 |
+
weight_decay=0.01,
|
| 70 |
+
report_to="none"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
|
| 74 |
+
|
| 75 |
+
trainer = Trainer(
|
| 76 |
+
model=model,
|
| 77 |
+
args=training_args,
|
| 78 |
+
train_dataset=tokenized_dataset,
|
| 79 |
+
data_collator=data_collator
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
print("Starting training...")
|
| 83 |
+
trainer.train()
|
| 84 |
+
print("Training complete!")
|
| 85 |
+
|
| 86 |
+
# Gradio UI for testing the model
|
| 87 |
+
def correct_math(prompt):
|
| 88 |
+
model.eval() # Set model to evaluation mode.
|
| 89 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True)
|
| 90 |
+
input_ids = inputs.input_ids.to(model.device)
|
| 91 |
+
attention_mask = inputs.attention_mask.to(model.device)
|
| 92 |
+
|
| 93 |
+
outputs = model.generate(
|
| 94 |
+
input_ids,
|
| 95 |
+
attention_mask=attention_mask,
|
| 96 |
+
max_new_tokens=50,
|
| 97 |
+
do_sample=True,
|
| 98 |
+
temperature=0.7,
|
| 99 |
+
top_k=50,
|
| 100 |
+
top_p=0.90,
|
| 101 |
+
pad_token_id=tokenizer.eos_token_id
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 105 |
+
return result
|
| 106 |
+
|
| 107 |
+
# Create Gradio interface
|
| 108 |
+
gr.Interface(fn=correct_math, inputs="text", outputs="text", title="Math Correction Model", description="Enter an incorrect math statement to get the correct answer and explanation.").launch()
|
| 109 |
+
|