Update app.py
Browse files2 unmodified lines345678910111213141516171819202122232425262728293031323 unmodified lines363738394041424344451 unmodified line47484950515253542 unmodified linesfrom transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArgumentsimport torch# Load and preprocess datasetdef load_and_preprocess_data():# Load the dataset from the uploaded JSON filedataset = load_dataset('json', data_files='data.json') # Update path to your JSON file# Extract "prompt" and "chosen"/"rejected" as input-output pairsdef process_row(example):# Access the 'rows' keyrows = example['rows']# Initialize lists for prompts, chosen, and rejected pairsprompts = []chosen_responses = []rejected_responses = []# Process each row within 'rows'for row_data in rows:row = row_data['row'] # Access the 'row' inside each row_dataprompts.append(row['prompt'])chosen_responses.append(row['chosen'])rejected_responses.append(row['rejected'])# Return the processed result in the correct formatreturn {'prompt': prompts,'chosen': chosen_responses,3 unmodified lines# Apply the processing function to the datasetdataset = dataset.map(process_row, batched=True)# Load the tokenizer (You can choose any model, here we use BERT)tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')# Preprocess the dataset: tokenize inputs and labelsdef preprocess_function(examples):return tokenizer(examples['prompt'], truncation=True, padding='max_length', max_length=512)1 unmodified linedataset = dataset.map(preprocess_function, batched=True)return dataset, tokenizer# Define and train the modeldef train_model(dataset):# Load the model (BERT-based for sequence classification)model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2) # Change num_labels if needed2 unmodified lines345678910111213141516171819202122233 unmodified lines272829303132333435361 unmodified line3839404142432 unmodified linesfrom transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArgumentsimport torchdef load_and_preprocess_data():# Load the dataset from the uploaded JSON filedataset = load_dataset('json', data_files='data.json')# Extract "prompt" and "chosen"/"rejected" as input-output pairsdef process_row(example):prompts = []chosen_responses = []rejected_responses = []for row in example['rows']:prompts.append(row['row']['prompt'])chosen_responses.append(row['row']['chosen'])rejected_responses.append(row['row']['rejected'])return {'prompt': prompts,'chosen': chosen_responses,3 unmodified lines# Apply the processing function to the datasetdataset = dataset.map(process_row, batched=True)# Load the tokenizertokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')# Preprocess the dataset: tokenize inputsdef preprocess_function(examples):return tokenizer(examples['prompt'], truncation=True, padding='max_length', max_length=512)1 unmodified linedataset = dataset.map(preprocess_function, batched=True)return dataset, tokenizerdef train_model(dataset):# Load the model (BERT-based for sequence classification)model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2) # Change num_labels if needed
app.py+7-18