Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
import os
|
| 4 |
-
import logging
|
| 5 |
from transformers import (
|
| 6 |
AutoModelForCausalLM,
|
| 7 |
AutoTokenizer,
|
|
@@ -10,12 +8,11 @@ from transformers import (
|
|
| 10 |
DataCollatorForLanguageModeling
|
| 11 |
)
|
| 12 |
from datasets import load_dataset
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 16 |
-
os.environ["BITSANDBYTES_NOWELCOME"] = "1"
|
| 17 |
-
|
| 18 |
-
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
|
| 21 |
def train():
|
|
@@ -26,17 +23,13 @@ def train():
|
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
model_name,
|
| 28 |
device_map="cpu",
|
| 29 |
-
trust_remote_code=True
|
| 30 |
-
load_in_4bit=False # Disable quantization
|
| 31 |
)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
-
|
| 36 |
-
# Load sample dataset
|
| 37 |
dataset = load_dataset("wikitext", "wikitext-2-raw-v1")
|
| 38 |
|
| 39 |
-
# Tokenization
|
| 40 |
def tokenize_function(examples):
|
| 41 |
return tokenizer(
|
| 42 |
examples["text"],
|
|
@@ -52,25 +45,21 @@ def train():
|
|
| 52 |
remove_columns=["text"]
|
| 53 |
)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
data_collator = DataCollatorForLanguageModeling(
|
| 57 |
tokenizer=tokenizer,
|
| 58 |
mlm=False
|
| 59 |
)
|
| 60 |
|
| 61 |
-
# Training arguments
|
| 62 |
training_args = TrainingArguments(
|
| 63 |
output_dir="./results",
|
| 64 |
per_device_train_batch_size=2,
|
| 65 |
-
|
| 66 |
-
num_train_epochs=1, # Reduced for testing
|
| 67 |
logging_dir="./logs",
|
| 68 |
fp16=False,
|
| 69 |
-
|
| 70 |
-
use_cpu=True # Explicit CPU usage
|
| 71 |
)
|
| 72 |
|
| 73 |
-
# Trainer
|
| 74 |
trainer = Trainer(
|
| 75 |
model=model,
|
| 76 |
args=training_args,
|
|
@@ -79,11 +68,11 @@ def train():
|
|
| 79 |
)
|
| 80 |
|
| 81 |
# Start training
|
| 82 |
-
logging.info("
|
| 83 |
trainer.train()
|
| 84 |
logging.info("Training completed!")
|
| 85 |
|
| 86 |
-
return "✅ Training successful
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
logging.error(f"Error: {str(e)}")
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from transformers import (
|
| 4 |
AutoModelForCausalLM,
|
| 5 |
AutoTokenizer,
|
|
|
|
| 8 |
DataCollatorForLanguageModeling
|
| 9 |
)
|
| 10 |
from datasets import load_dataset
|
| 11 |
+
import logging
|
| 12 |
+
import os
|
| 13 |
|
| 14 |
+
# Configure environment
|
| 15 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "" # Force CPU
|
|
|
|
|
|
|
|
|
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
| 17 |
|
| 18 |
def train():
|
|
|
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
model_name,
|
| 25 |
device_map="cpu",
|
| 26 |
+
trust_remote_code=True
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# Load dataset
|
|
|
|
|
|
|
|
|
|
| 30 |
dataset = load_dataset("wikitext", "wikitext-2-raw-v1")
|
| 31 |
|
| 32 |
+
# Tokenization
|
| 33 |
def tokenize_function(examples):
|
| 34 |
return tokenizer(
|
| 35 |
examples["text"],
|
|
|
|
| 45 |
remove_columns=["text"]
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Training setup
|
| 49 |
data_collator = DataCollatorForLanguageModeling(
|
| 50 |
tokenizer=tokenizer,
|
| 51 |
mlm=False
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
training_args = TrainingArguments(
|
| 55 |
output_dir="./results",
|
| 56 |
per_device_train_batch_size=2,
|
| 57 |
+
num_train_epochs=1,
|
|
|
|
| 58 |
logging_dir="./logs",
|
| 59 |
fp16=False,
|
| 60 |
+
report_to="none"
|
|
|
|
| 61 |
)
|
| 62 |
|
|
|
|
| 63 |
trainer = Trainer(
|
| 64 |
model=model,
|
| 65 |
args=training_args,
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
# Start training
|
| 71 |
+
logging.info("Training started...")
|
| 72 |
trainer.train()
|
| 73 |
logging.info("Training completed!")
|
| 74 |
|
| 75 |
+
return "✅ Training successful"
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
logging.error(f"Error: {str(e)}")
|