Upload Duckbot.py
Browse files- Duckbot.py +60 -0
Duckbot.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, Trainer, TrainingArguments
|
| 3 |
+
from Features.chat_interface import start_chat_interface
|
| 4 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, Trainer, TrainingArguments
|
| 5 |
+
from Features.chat_interface import start_chat_interface
|
| 6 |
+
import start_chat_interface, generate_response # Import needed functions
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# --- Step 1: Prepare Your Data ---
|
| 10 |
+
data = pd.read_csv("your_chatbot_data.csv") # Replace with your dataset file
|
| 11 |
+
data = data[["user_input", "chatbot_response"]] # Adjust column names if needed
|
| 12 |
+
data_path = "data/chatbot_data.csv"
|
| 13 |
+
data = pd.read_csv(data_path)
|
| 14 |
+
|
| 15 |
+
# --- Step 2: Choose a Pre-trained Model ---
|
| 16 |
+
model_name = "microsoft/DialoGPT-medium" # A good starting point, experiment with others!
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 19 |
+
|
| 20 |
+
# --- Step 3: Tokenize the Data ---
|
| 21 |
+
def preprocess(examples):
|
| 22 |
+
inputs = [ex + tokenizer.eos_token for ex in examples["user_input"]]
|
| 23 |
+
targets = examples["chatbot_response"]
|
| 24 |
+
model_inputs = tokenizer(inputs, max_length=128, truncation=True, padding=True)
|
| 25 |
+
|
| 26 |
+
with tokenizer.as_target_tokenizer():
|
| 27 |
+
model_inputs["labels"] = tokenizer(targets, max_length=128, truncation=True, padding=True).input_ids
|
| 28 |
+
|
| 29 |
+
return model_inputs
|
| 30 |
+
|
| 31 |
+
tokenized_data = data.apply(preprocess, axis=1)
|
| 32 |
+
|
| 33 |
+
# --- Step 4: Fine-Tune the Model ---
|
| 34 |
+
training_args = TrainingArguments(
|
| 35 |
+
"my-chatbot", # Output folder name
|
| 36 |
+
evaluation_strategy="steps",
|
| 37 |
+
learning_rate=2e-5,
|
| 38 |
+
per_device_train_batch_size=8,
|
| 39 |
+
per_device_eval_batch_size=8,
|
| 40 |
+
num_train_epochs=3,
|
| 41 |
+
weight_decay=0.01,
|
| 42 |
+
save_steps=500, # Save model checkpoint every 500 steps
|
| 43 |
+
push_to_hub=True, # Push the model to your Hugging Face Hub
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
trainer = Trainer(
|
| 47 |
+
model=model,
|
| 48 |
+
args=training_args,
|
| 49 |
+
train_dataset=tokenized_data,
|
| 50 |
+
eval_dataset=tokenized_data,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
trainer.train()
|
| 54 |
+
|
| 55 |
+
# --- Step 5: Use Your Fine-Tuned Model (After Training) ---
|
| 56 |
+
def generate_response(user_input):
|
| 57 |
+
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 58 |
+
output_sequences = model.generate(input_ids=input_ids)
|
| 59 |
+
response = tokenizer.decode(output_sequences[0], skip_special_tokens=True)
|
| 60 |
+
return response
|