Spaces:
Runtime error
Runtime error
VenkateshRoshan
commited on
Commit
·
f887b2e
1
Parent(s):
5aeaa5c
fine-tuning, infering, app codes added
Browse files- app.py +85 -15
- src/data.ipynb +0 -0
- src/infer.py +89 -0
- src/train.py +107 -0
app.py
CHANGED
|
@@ -1,22 +1,92 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
| 20 |
if __name__ == "__main__":
|
| 21 |
-
|
| 22 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
class CustomerSupportBot:
|
| 6 |
+
def __init__(self, model_path="models/customer_support_gpt"):
|
| 7 |
+
"""
|
| 8 |
+
Initialize the customer support bot with the fine-tuned model.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
model_path (str): Path to the saved model and tokenizer
|
| 12 |
+
"""
|
| 13 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 14 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 15 |
+
|
| 16 |
+
# Move model to GPU if available
|
| 17 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
self.model = self.model.to(self.device)
|
| 19 |
+
|
| 20 |
+
def generate_response(self, instruction, max_length=100, temperature=0.7):
|
| 21 |
+
"""
|
| 22 |
+
Generate a response for a given customer support instruction/query.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
instruction (str): Customer's query or instruction
|
| 26 |
+
max_length (int): Maximum length of the generated response
|
| 27 |
+
temperature (float): Controls randomness in generation (higher = more random)
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
str: Generated response
|
| 31 |
+
"""
|
| 32 |
+
# Format input text the same way as during training
|
| 33 |
+
input_text = f"Instruction: {instruction}\nResponse:"
|
| 34 |
+
|
| 35 |
+
# Tokenize input
|
| 36 |
+
inputs = self.tokenizer(input_text, return_tensors="pt")
|
| 37 |
+
inputs = inputs.to(self.device)
|
| 38 |
+
|
| 39 |
+
# Generate response
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
outputs = self.model.generate(
|
| 42 |
+
**inputs,
|
| 43 |
+
max_length=50,
|
| 44 |
+
temperature=temperature,
|
| 45 |
+
num_return_sequences=1,
|
| 46 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
| 47 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
top_p=0.95,
|
| 50 |
+
top_k=50
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Decode and format response
|
| 54 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 55 |
+
|
| 56 |
+
# Extract only the response part
|
| 57 |
+
response = response.split("Response:")[-1].strip()
|
| 58 |
+
|
| 59 |
+
return response
|
| 60 |
|
| 61 |
+
# Initialize the chatbot
|
| 62 |
+
bot = CustomerSupportBot()
|
| 63 |
|
| 64 |
+
# Define the Gradio interface function
|
| 65 |
+
def chatbot_response(message, history):
|
| 66 |
+
"""
|
| 67 |
+
Generate bot response for the Gradio interface.
|
| 68 |
+
|
| 69 |
+
Args:
|
| 70 |
+
message (str): User's input message
|
| 71 |
+
history (list): Chat history
|
| 72 |
+
"""
|
| 73 |
+
bot_response = bot.generate_response(message)
|
| 74 |
+
history.append((bot_response))
|
| 75 |
+
return history
|
| 76 |
|
| 77 |
+
# Create the Gradio interface
|
| 78 |
+
iface = gr.ChatInterface(
|
| 79 |
+
fn=chatbot_response,
|
| 80 |
+
title="Customer Support Chatbot",
|
| 81 |
+
description="Ask your questions to the customer support bot!",
|
| 82 |
+
examples=["How do I reset my password?",
|
| 83 |
+
"What are your shipping policies?",
|
| 84 |
+
"I want to return a product."],
|
| 85 |
+
# retry_btn=None,
|
| 86 |
+
# undo_btn="Remove Last",
|
| 87 |
+
# clear_btn="Clear",
|
| 88 |
+
)
|
| 89 |
|
| 90 |
+
# Launch the interface
|
| 91 |
if __name__ == "__main__":
|
| 92 |
+
iface.launch(share=False) # Set share=True if you want to create a public link
|
|
|
src/data.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
src/infer.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
class CustomerSupportBot:
|
| 5 |
+
def __init__(self, model_path="models/customer_support_gpt"):
|
| 6 |
+
"""
|
| 7 |
+
Initialize the customer support bot with the fine-tuned model.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
model_path (str): Path to the saved model and tokenizer
|
| 11 |
+
"""
|
| 12 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 13 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 14 |
+
|
| 15 |
+
# Move model to GPU if available
|
| 16 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
+
self.model = self.model.to(self.device)
|
| 18 |
+
|
| 19 |
+
def generate_response(self, instruction, max_length=100, temperature=0.7):
|
| 20 |
+
"""
|
| 21 |
+
Generate a response for a given customer support instruction/query.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
instruction (str): Customer's query or instruction
|
| 25 |
+
max_length (int): Maximum length of the generated response
|
| 26 |
+
temperature (float): Controls randomness in generation (higher = more random)
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
str: Generated response
|
| 30 |
+
"""
|
| 31 |
+
# Format input text the same way as during training
|
| 32 |
+
input_text = f"Instruction: {instruction}\nResponse:"
|
| 33 |
+
|
| 34 |
+
# Tokenize input
|
| 35 |
+
inputs = self.tokenizer(input_text, return_tensors="pt")
|
| 36 |
+
inputs = inputs.to(self.device)
|
| 37 |
+
|
| 38 |
+
# Generate response
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
outputs = self.model.generate(
|
| 41 |
+
**inputs,
|
| 42 |
+
max_length=max_length,
|
| 43 |
+
temperature=temperature,
|
| 44 |
+
num_return_sequences=1,
|
| 45 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
| 46 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
top_p=0.95,
|
| 49 |
+
top_k=50
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Decode and format response
|
| 53 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
# Extract only the response part
|
| 56 |
+
response = response.split("Response:")[-1].strip()
|
| 57 |
+
|
| 58 |
+
return response
|
| 59 |
+
|
| 60 |
+
def main():
|
| 61 |
+
# Initialize the bot
|
| 62 |
+
bot = CustomerSupportBot()
|
| 63 |
+
|
| 64 |
+
# Example queries
|
| 65 |
+
example_queries = [
|
| 66 |
+
"How do I reset my password?",
|
| 67 |
+
"What are your shipping policies?",
|
| 68 |
+
"I want to return a product.",
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
# Generate and print responses
|
| 72 |
+
print("Customer Support Bot Demo:\n")
|
| 73 |
+
for query in example_queries:
|
| 74 |
+
print(f"Customer: {query}")
|
| 75 |
+
response = bot.generate_response(query)
|
| 76 |
+
print(f"Bot: {response}\n")
|
| 77 |
+
|
| 78 |
+
# Interactive mode
|
| 79 |
+
print("Enter your questions (type 'quit' to exit):")
|
| 80 |
+
while True:
|
| 81 |
+
query = input("\nYour question: ")
|
| 82 |
+
if query.lower() == 'quit':
|
| 83 |
+
break
|
| 84 |
+
|
| 85 |
+
response = bot.generate_response(query)
|
| 86 |
+
print(f"Bot: {response}")
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
main()
|
src/train.py
CHANGED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import mlflow
|
| 2 |
+
from transformers import (
|
| 3 |
+
AutoModelForCausalLM,
|
| 4 |
+
AutoTokenizer,
|
| 5 |
+
Trainer,
|
| 6 |
+
TrainingArguments,
|
| 7 |
+
DataCollatorForLanguageModeling
|
| 8 |
+
)
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
|
| 11 |
+
def prepare_data(tokenizer, dataset):
|
| 12 |
+
"""Tokenize and format the dataset."""
|
| 13 |
+
def tokenize_function(examples):
|
| 14 |
+
# Combine instruction and response with a separator
|
| 15 |
+
text = [f"Instruction: {instr}\nResponse: {resp}"
|
| 16 |
+
for instr, resp in zip(examples['instruction'], examples['response'])]
|
| 17 |
+
|
| 18 |
+
return tokenizer(
|
| 19 |
+
text,
|
| 20 |
+
truncation=True,
|
| 21 |
+
max_length=256,
|
| 22 |
+
padding='max_length'
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
tokenized_datasets = dataset.map(
|
| 26 |
+
tokenize_function,
|
| 27 |
+
batched=True,
|
| 28 |
+
remove_columns=dataset['train'].column_names
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
return tokenized_datasets
|
| 32 |
+
|
| 33 |
+
def fine_tune_model():
|
| 34 |
+
"""
|
| 35 |
+
Fine-tune GPT-Neo on customer support data using instructions and responses.
|
| 36 |
+
"""
|
| 37 |
+
# Load dataset
|
| 38 |
+
dataset = load_dataset('csv', data_files='data/raw/customer_support.csv')
|
| 39 |
+
dataset = dataset['train'].train_test_split(test_size=0.2, seed=42)
|
| 40 |
+
|
| 41 |
+
# Load model and tokenizer
|
| 42 |
+
model_name = "EleutherAI/gpt-neo-125M"
|
| 43 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 44 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 45 |
+
|
| 46 |
+
# Add padding token if it doesn't exist
|
| 47 |
+
if tokenizer.pad_token is None:
|
| 48 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 49 |
+
model.config.pad_token_id = model.config.eos_token_id
|
| 50 |
+
|
| 51 |
+
# Prepare the dataset
|
| 52 |
+
tokenized_datasets = prepare_data(tokenizer, dataset)
|
| 53 |
+
|
| 54 |
+
# Create data collator
|
| 55 |
+
data_collator = DataCollatorForLanguageModeling(
|
| 56 |
+
tokenizer=tokenizer,
|
| 57 |
+
mlm=False # We're not doing masked language modeling
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
mlflow.start_run()
|
| 61 |
+
|
| 62 |
+
# Log hyperparameters
|
| 63 |
+
mlflow.log_param("model_name", model_name)
|
| 64 |
+
mlflow.log_param("epochs", 3)
|
| 65 |
+
mlflow.log_param("batch_size", 4)
|
| 66 |
+
mlflow.log_param("learning_rate", 2e-5)
|
| 67 |
+
|
| 68 |
+
training_args = TrainingArguments(
|
| 69 |
+
output_dir="models/",
|
| 70 |
+
evaluation_strategy="epoch",
|
| 71 |
+
learning_rate=2e-5,
|
| 72 |
+
per_device_train_batch_size=4,
|
| 73 |
+
per_device_eval_batch_size=4,
|
| 74 |
+
num_train_epochs=3,
|
| 75 |
+
weight_decay=0.01,
|
| 76 |
+
save_strategy="epoch",
|
| 77 |
+
save_total_limit=2,
|
| 78 |
+
load_best_model_at_end=True,
|
| 79 |
+
report_to="mlflow"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
trainer = Trainer(
|
| 83 |
+
model=model,
|
| 84 |
+
args=training_args,
|
| 85 |
+
train_dataset=tokenized_datasets['train'],
|
| 86 |
+
eval_dataset=tokenized_datasets['test'],
|
| 87 |
+
data_collator=data_collator,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
trainer.train()
|
| 91 |
+
|
| 92 |
+
# Save the model and tokenizer
|
| 93 |
+
model_path = "models/customer_support_gpt"
|
| 94 |
+
model.save_pretrained(model_path)
|
| 95 |
+
tokenizer.save_pretrained(model_path)
|
| 96 |
+
|
| 97 |
+
# Log model artifacts
|
| 98 |
+
mlflow.log_artifact(model_path)
|
| 99 |
+
|
| 100 |
+
# Log evaluation metrics
|
| 101 |
+
metrics = trainer.evaluate()
|
| 102 |
+
mlflow.log_metrics(metrics)
|
| 103 |
+
|
| 104 |
+
mlflow.end_run()
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
fine_tune_model()
|