Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -59,6 +59,81 @@ demo = gr.ChatInterface(
|
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
],
|
60 |
)
|
61 |
|
|
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
64 |
+
|
65 |
+
# Fine-Tuning GPT-2 on Hugging Face Spaces (Streaming 40GB Dataset, No Storage Issues)
|
66 |
+
|
67 |
+
# Install required libraries
|
68 |
+
!pip install transformers datasets peft accelerate bitsandbytes torch torchvision torchaudio gradio -q
|
69 |
+
|
70 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
71 |
+
from datasets import load_dataset
|
72 |
+
from peft import LoraConfig, get_peft_model
|
73 |
+
import torch
|
74 |
+
|
75 |
+
# Authenticate Hugging Face
|
76 |
+
from huggingface_hub import notebook_login
|
77 |
+
notebook_login()
|
78 |
+
|
79 |
+
# Load GPT-2 model and tokenizer
|
80 |
+
model_name = "gpt2"
|
81 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
82 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
83 |
+
|
84 |
+
# Load the OpenWebText dataset using streaming (No download required)
|
85 |
+
dataset = load_dataset("Skylion007/openwebtext", split="train[:5%]") # Load 5% to avoid streaming issues
|
86 |
+
|
87 |
+
# Tokenization function
|
88 |
+
def tokenize_function(examples):
|
89 |
+
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)
|
90 |
+
|
91 |
+
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
92 |
+
|
93 |
+
# Apply LoRA for efficient fine-tuning
|
94 |
+
lora_config = LoraConfig(
|
95 |
+
r=8, lora_alpha=32, lora_dropout=0.05, bias="none",
|
96 |
+
target_modules=["c_attn", "c_proj"] # Apply LoRA to attention layers
|
97 |
+
)
|
98 |
+
|
99 |
+
model = get_peft_model(model, lora_config)
|
100 |
+
|
101 |
+
# Enable gradient checkpointing to reduce memory usage
|
102 |
+
model.gradient_checkpointing_enable()
|
103 |
+
|
104 |
+
# Training arguments
|
105 |
+
training_args = TrainingArguments(
|
106 |
+
output_dir="gpt2_finetuned",
|
107 |
+
auto_find_batch_size=True,
|
108 |
+
gradient_accumulation_steps=4,
|
109 |
+
learning_rate=5e-5,
|
110 |
+
num_train_epochs=3,
|
111 |
+
save_strategy="epoch",
|
112 |
+
logging_dir="logs",
|
113 |
+
bf16=True,
|
114 |
+
push_to_hub=True
|
115 |
+
)
|
116 |
+
|
117 |
+
# Trainer setup
|
118 |
+
trainer = Trainer(
|
119 |
+
model=model,
|
120 |
+
args=training_args,
|
121 |
+
train_dataset=tokenized_datasets
|
122 |
+
)
|
123 |
+
|
124 |
+
# Start fine-tuning
|
125 |
+
trainer.train()
|
126 |
+
|
127 |
+
# Save and push the model to Hugging Face Hub
|
128 |
+
trainer.save_model("gpt2_finetuned")
|
129 |
+
tokenizer.save_pretrained("gpt2_finetuned")
|
130 |
+
trainer.push_to_hub()
|
131 |
+
|
132 |
+
# Deploy as Gradio Interface
|
133 |
+
def generate_response(prompt):
|
134 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
135 |
+
outputs = model.generate(**inputs, max_length=100)
|
136 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
137 |
+
|
138 |
+
demo = gr.Interface(fn=generate_response, inputs="text", outputs="text")
|
139 |
+
demo.launch()
|