Spaces:
Runtime error
Runtime error
| from datasets import load_dataset | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer | |
| import torch | |
| # Load dataset from Hugging Face | |
| dataset = load_dataset("Soundaryasos/Verdictclassifications") | |
| # Load tokenizer and model | |
| model_name = "bert-base-uncased" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| # Tokenization function | |
| def tokenize_function(example): | |
| return tokenizer(example["case_description"], padding="max_length", truncation=True) | |
| # Tokenize dataset | |
| tokenized_datasets = dataset.map(tokenize_function, batched=True) | |
| # Load model for binary classification (Guilty/Not Guilty) | |
| model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2) | |
| # Training arguments | |
| training_args = TrainingArguments( | |
| output_dir="criminal_case_model", | |
| evaluation_strategy="epoch", | |
| save_strategy="epoch", | |
| per_device_train_batch_size=8, | |
| per_device_eval_batch_size=8, | |
| num_train_epochs=3, | |
| weight_decay=0.01, | |
| push_to_hub=True, # Push model to Hugging Face | |
| logging_dir="./logs", | |
| ) | |
| # Trainer | |
| trainer = Trainer( | |
| model=model, | |
| args=training_args, | |
| train_dataset=tokenized_datasets["train"], | |
| eval_dataset=tokenized_datasets["test"], | |
| ) | |
| # Train the model | |
| trainer.train() | |
| trainer.push_to_hub("Soundaryasos/criminal_case_model") | |