import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments from datasets import load_dataset, Dataset from peft import LoraConfig, get_peft_model import torch # Load GPT-2 model and tokenizer model_name = "gpt2" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) # Custom Dataset (Improved format) custom_data = [ {"text": "Prompt: Who are you?\nResponse: I am Eva, a virtual voice assistant."}, {"text": "Prompt: What is your name?\nResponse: I am Eva, how can I help you?"}, {"text": "Prompt: What can you do?\nResponse: I can assist with answering questions, searching the web, and much more!"}, {"text": "Prompt: Who invented the computer?\nResponse: Charles Babbage is known as the father of the computer."}, {"text": "Prompt: Tell me a joke.\nResponse: Why don’t scientists trust atoms? Because they make up everything!"}, {"text": "Prompt: Who is the Prime Minister of India?\nResponse: The current Prime Minister of India is Narendra Modi."}, {"text": "Prompt: Who created you?\nResponse: I was created by an expert team specializing in AI fine-tuning and web development."}, {"text": "Prompt: Can you introduce yourself?\nResponse: I am Eva, your AI assistant, designed to assist and provide information."} ] # Convert custom data to a Dataset dataset_custom = Dataset.from_list(custom_data) def tokenize_function(examples): return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512) tokenized_datasets = dataset_custom.map(tokenize_function, batched=True) # Apply LoRA for efficient fine-tuning lora_config = LoraConfig( r=4, # Reduced r for stability lora_alpha=16, lora_dropout=0.1, bias="none", target_modules=["c_attn", "c_proj"] # LoRA targets attention layers ) model = get_peft_model(model, lora_config) model.gradient_checkpointing_enable() # Training arguments training_args = TrainingArguments( output_dir="gpt2_finetuned", auto_find_batch_size=True, gradient_accumulation_steps=4, learning_rate=3e-5, # Lowered learning rate for improved stability num_train_epochs=5, # Increased epochs for better training save_strategy="epoch", logging_dir="logs", bf16=True, push_to_hub=True ) # Trainer setup trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_datasets ) trainer.train() # Save and push the model trainer.save_model("gpt2_finetuned") tokenizer.save_pretrained("gpt2_finetuned") trainer.push_to_hub() # Gradio Interface for Responses def generate_response(prompt): inputs = tokenizer(f"Prompt: {prompt}\nResponse:", return_tensors="pt") outputs = model.generate(**inputs, max_length=150, num_return_sequences=1, temperature=0.7, top_p=0.9) return tokenizer.decode(outputs[0], skip_special_tokens=True).split("Response:")[-1].strip() demo = gr.Interface(fn=generate_response, inputs="text", outputs="text") if __name__ == "__main__": demo.launch()