Created app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datasets import load_dataset, Dataset
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load and preprocess dataset
|
| 7 |
+
def load_and_preprocess_data():
|
| 8 |
+
# Load the dataset from the uploaded JSON file
|
| 9 |
+
dataset = load_dataset('json', data_files='data/your_dataset.json') # Update path to your JSON file
|
| 10 |
+
|
| 11 |
+
# Extract "prompt" and "chosen"/"rejected" as input-output pairs
|
| 12 |
+
def process_row(example):
|
| 13 |
+
return {
|
| 14 |
+
'prompt': example['row']['prompt'],
|
| 15 |
+
'response': example['row']['chosen'], # You can change this to 'rejected' if needed
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
dataset = dataset.map(process_row)
|
| 19 |
+
|
| 20 |
+
# Load the tokenizer (You can choose any model, here we use BERT)
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 22 |
+
|
| 23 |
+
# Preprocess the dataset: tokenize inputs and labels
|
| 24 |
+
def preprocess_function(examples):
|
| 25 |
+
return tokenizer(examples['prompt'], truncation=True, padding='max_length', max_length=512)
|
| 26 |
+
|
| 27 |
+
# Apply preprocessing to the dataset
|
| 28 |
+
dataset = dataset.map(preprocess_function, batched=True)
|
| 29 |
+
|
| 30 |
+
return dataset, tokenizer
|
| 31 |
+
|
| 32 |
+
# Define and train the model
|
| 33 |
+
def train_model(dataset):
|
| 34 |
+
# Load the model (BERT-based for sequence classification)
|
| 35 |
+
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2) # Change num_labels if needed
|
| 36 |
+
|
| 37 |
+
# Define training arguments
|
| 38 |
+
training_args = TrainingArguments(
|
| 39 |
+
output_dir='./results', # Output directory for model checkpoints
|
| 40 |
+
evaluation_strategy="epoch", # Evaluation at the end of each epoch
|
| 41 |
+
learning_rate=2e-5, # Learning rate
|
| 42 |
+
per_device_train_batch_size=8, # Batch size for training
|
| 43 |
+
per_device_eval_batch_size=8, # Batch size for evaluation
|
| 44 |
+
num_train_epochs=3, # Number of training epochs
|
| 45 |
+
weight_decay=0.01, # Strength of weight decay
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Initialize the Trainer
|
| 49 |
+
trainer = Trainer(
|
| 50 |
+
model=model,
|
| 51 |
+
args=training_args,
|
| 52 |
+
train_dataset=dataset['train'],
|
| 53 |
+
eval_dataset=dataset['test'], # If you have a test split, otherwise just use train for both
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Train the model
|
| 57 |
+
trainer.train()
|
| 58 |
+
|
| 59 |
+
return model
|
| 60 |
+
|
| 61 |
+
# Gradio Interface function to predict
|
| 62 |
+
def predict(text):
|
| 63 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 64 |
+
outputs = model(**inputs)
|
| 65 |
+
prediction = torch.argmax(outputs.logits, dim=-1).item() # Get the class with the highest score
|
| 66 |
+
return f"Prediction: {prediction}"
|
| 67 |
+
|
| 68 |
+
# Main script to run training
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
# Load and preprocess the data
|
| 71 |
+
dataset, tokenizer = load_and_preprocess_data()
|
| 72 |
+
|
| 73 |
+
# Train the model
|
| 74 |
+
model = train_model(dataset)
|
| 75 |
+
|
| 76 |
+
# Create a Gradio interface for prediction
|
| 77 |
+
gr.Interface(fn=predict, inputs=gr.Textbox(), outputs="text").launch()
|