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
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
|
| 6 |
+
from peft import LoraConfig, prepare_model_for_kbit_training
|
| 7 |
+
from trl import SFTTrainer
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
+
def fine_tune_model():
|
| 11 |
+
"""Fine-tune model for personal assistant with progress updates"""
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
yield "π Starting fine-tuning process...\n"
|
| 15 |
+
|
| 16 |
+
# Use a manageable model for Spaces
|
| 17 |
+
model_name = "microsoft/DialoGPT-medium"
|
| 18 |
+
yield f"π₯ Loading model: {model_name}\n"
|
| 19 |
+
|
| 20 |
+
# Load tokenizer
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left")
|
| 22 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 23 |
+
yield "β
Tokenizer loaded successfully\n"
|
| 24 |
+
|
| 25 |
+
# Load model
|
| 26 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
+
model_name,
|
| 28 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
| 29 |
+
)
|
| 30 |
+
yield "β
Model loaded successfully\n"
|
| 31 |
+
|
| 32 |
+
# Load dataset
|
| 33 |
+
yield "π Loading training dataset...\n"
|
| 34 |
+
dataset = load_dataset("databricks/databricks-dolly-15k", split="train[:1000]")
|
| 35 |
+
yield f"β
Dataset loaded: {len(dataset)} examples\n"
|
| 36 |
+
|
| 37 |
+
# Format dataset
|
| 38 |
+
def format_example(example):
|
| 39 |
+
instruction = example["instruction"]
|
| 40 |
+
response = example["response"]
|
| 41 |
+
context = example.get("context", "")
|
| 42 |
+
|
| 43 |
+
if context:
|
| 44 |
+
text = f"Human: {instruction}\nContext: {context}\nAssistant: {response}<|endoftext|>"
|
| 45 |
+
else:
|
| 46 |
+
text = f"Human: {instruction}\nAssistant: {response}<|endoftext|>"
|
| 47 |
+
return {"text": text}
|
| 48 |
+
|
| 49 |
+
processed_dataset = dataset.map(format_example)
|
| 50 |
+
yield "β
Dataset formatted for training\n"
|
| 51 |
+
|
| 52 |
+
# LoRA configuration
|
| 53 |
+
peft_config = LoraConfig(
|
| 54 |
+
lora_alpha=16,
|
| 55 |
+
lora_dropout=0.1,
|
| 56 |
+
r=8,
|
| 57 |
+
bias="none",
|
| 58 |
+
task_type="CAUSAL_LM",
|
| 59 |
+
target_modules=["c_attn", "c_proj"],
|
| 60 |
+
)
|
| 61 |
+
yield "β
LoRA configuration set\n"
|
| 62 |
+
|
| 63 |
+
# Training arguments
|
| 64 |
+
training_arguments = TrainingArguments(
|
| 65 |
+
output_dir="./results",
|
| 66 |
+
per_device_train_batch_size=1,
|
| 67 |
+
gradient_accumulation_steps=4,
|
| 68 |
+
logging_steps=10,
|
| 69 |
+
save_steps=100,
|
| 70 |
+
learning_rate=5e-5,
|
| 71 |
+
num_train_epochs=1,
|
| 72 |
+
warmup_steps=50,
|
| 73 |
+
remove_unused_columns=False,
|
| 74 |
+
dataloader_pin_memory=False,
|
| 75 |
+
)
|
| 76 |
+
yield "β
Training configuration set\n"
|
| 77 |
+
|
| 78 |
+
# Create trainer
|
| 79 |
+
trainer = SFTTrainer(
|
| 80 |
+
model=model,
|
| 81 |
+
train_dataset=processed_dataset,
|
| 82 |
+
tokenizer=tokenizer,
|
| 83 |
+
args=training_arguments,
|
| 84 |
+
peft_config=peft_config,
|
| 85 |
+
dataset_text_field="text",
|
| 86 |
+
)
|
| 87 |
+
yield "π Starting model training...\n"
|
| 88 |
+
|
| 89 |
+
# Start training
|
| 90 |
+
trainer.train()
|
| 91 |
+
yield "β
Training completed successfully!\n"
|
| 92 |
+
|
| 93 |
+
# Save model
|
| 94 |
+
trainer.save_model("./fine_tuned_assistant")
|
| 95 |
+
tokenizer.save_pretrained("./fine_tuned_assistant")
|
| 96 |
+
yield "πΎ Model saved successfully!\n"
|
| 97 |
+
|
| 98 |
+
yield "π Fine-tuning process completed! Your personal assistant is ready!\n"
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
yield f"β Error during fine-tuning: {str(e)}\n"
|
| 102 |
|
| 103 |
+
def chat_with_assistant(message, history):
|
| 104 |
+
"""Simple chat interface for testing"""
|
| 105 |
+
if not message.strip():
|
| 106 |
+
return history, ""
|
| 107 |
+
|
| 108 |
+
# Placeholder response (you can implement actual model inference later)
|
| 109 |
+
responses = [
|
| 110 |
+
f"Thank you for your message: '{message}'. As your personal assistant, I'm here to help!",
|
| 111 |
+
f"I appreciate you reaching out about '{message}'. How can I assist you further?",
|
| 112 |
+
f"Regarding '{message}', I'm happy to help. What specific assistance do you need?",
|
| 113 |
+
f"I understand you mentioned '{message}'. As your supportive assistant, I'm here for you!"
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
import random
|
| 117 |
+
response = random.choice(responses)
|
| 118 |
+
|
| 119 |
+
history.append((message, response))
|
| 120 |
+
return history, ""
|
| 121 |
|
| 122 |
+
# Create Gradio interface
|
| 123 |
+
with gr.Blocks(title="Personal Assistant Fine-Tuning", theme=gr.themes.Soft()) as demo:
|
| 124 |
+
gr.Markdown("""
|
| 125 |
+
# π€ Personal Assistant Fine-Tuning on Hugging Face Spaces
|
| 126 |
+
|
| 127 |
+
Welcome to your personal AI assistant training platform! This space allows you to:
|
| 128 |
+
- Fine-tune a language model to be your personal assistant
|
| 129 |
+
- Test the assistant's responses
|
| 130 |
+
- Create a kind, supportive, and intelligent AI companion
|
| 131 |
+
""")
|
| 132 |
+
|
| 133 |
+
with gr.Tab("π§ Fine-Tune Model"):
|
| 134 |
+
gr.Markdown("""
|
| 135 |
+
### Train Your Personal Assistant
|
| 136 |
+
|
| 137 |
+
Click the button below to start fine-tuning your model. This process will:
|
| 138 |
+
1. Load a pre-trained conversational model
|
| 139 |
+
2. Train it on assistant-style conversations
|
| 140 |
+
3. Optimize it to be supportive and helpful
|
| 141 |
+
|
| 142 |
+
**Note:** Training may take 30-60 minutes depending on your hardware tier.
|
| 143 |
+
""")
|
| 144 |
+
|
| 145 |
+
tune_button = gr.Button("π Start Fine-Tuning", variant="primary", size="lg")
|
| 146 |
+
tune_output = gr.Textbox(
|
| 147 |
+
label="Training Progress",
|
| 148 |
+
lines=15,
|
| 149 |
+
max_lines=20,
|
| 150 |
+
show_copy_button=True,
|
| 151 |
+
interactive=False
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
tune_button.click(
|
| 155 |
+
fn=fine_tune_model,
|
| 156 |
+
outputs=tune_output
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
with gr.Tab("π¬ Test Assistant"):
|
| 160 |
+
gr.Markdown("""
|
| 161 |
+
### Chat with Your Assistant
|
| 162 |
+
|
| 163 |
+
Once fine-tuning is complete, use this interface to test your personal assistant.
|
| 164 |
+
Your assistant is designed to be kind, supportive, and intelligent.
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
chatbot = gr.Chatbot(
|
| 168 |
+
height=500,
|
| 169 |
+
show_copy_button=True,
|
| 170 |
+
bubble_full_width=False
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
with gr.Row():
|
| 174 |
+
msg = gr.Textbox(
|
| 175 |
+
label="Your message",
|
| 176 |
+
placeholder="Type your message here...",
|
| 177 |
+
lines=2,
|
| 178 |
+
scale=4
|
| 179 |
+
)
|
| 180 |
+
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 181 |
+
|
| 182 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
|
| 183 |
+
|
| 184 |
+
# Event handlers
|
| 185 |
+
msg.submit(chat_with_assistant, [msg, chatbot], [chatbot, msg])
|
| 186 |
+
send_btn.click(chat_with_assistant, [msg, chatbot], [chatbot, msg])
|
| 187 |
+
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
| 188 |
+
|
| 189 |
+
with gr.Tab("βΉοΈ Info"):
|
| 190 |
+
gr.Markdown("""
|
| 191 |
+
### About This Space
|
| 192 |
+
|
| 193 |
+
This Hugging Face Space is designed to help you create your own personal AI assistant through fine-tuning.
|
| 194 |
+
|
| 195 |
+
**Features:**
|
| 196 |
+
- π€ Fine-tune conversational AI models
|
| 197 |
+
- π¬ Interactive chat interface for testing
|
| 198 |
+
- π― Optimized for supportive, intelligent responses
|
| 199 |
+
- π Real-time training progress monitoring
|
| 200 |
+
|
| 201 |
+
**Hardware Recommendations:**
|
| 202 |
+
- **Free CPU:** Good for testing the interface
|
| 203 |
+
- **T4 Small GPU ($0.60/hr):** Recommended for actual training
|
| 204 |
+
- **A10G Small ($1.05/hr):** Faster training, better performance
|
| 205 |
+
|
| 206 |
+
**Tips for Success:**
|
| 207 |
+
1. Start with free tier to test everything works
|
| 208 |
+
2. Upgrade to GPU when ready for actual training
|
| 209 |
+
3. Monitor training progress in the Fine-Tune tab
|
| 210 |
+
4. Test your assistant in the Chat tab after training
|
| 211 |
+
|
| 212 |
+
Created with β€οΈ for building personal AI assistants!
|
| 213 |
+
""")
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
+
demo.queue()
|
| 217 |
demo.launch()
|