nileshhanotia commited on
Commit
0a83766
1 Parent(s): e61ae4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -53
app.py CHANGED
@@ -1,62 +1,73 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
3
  from datasets import load_dataset
 
 
 
4
 
5
- # Streamlit App title
6
- st.title("Fine-Tune Mixtral 8x7B Model")
7
-
8
- # Model name input field
9
- model_name = "mistral/mixtral-8x7b"
10
-
11
- # Access the Hugging Face token from Streamlit secrets
12
- token = st.secrets["HF_TOKEN"]
13
-
14
- # Load the tokenizer and model
15
- try:
16
- tokenizer = AutoTokenizer.from_pretrained(model_name, token=token)
17
- model = AutoModelForCausalLM.from_pretrained(model_name, token=token)
18
- st.write("Model and tokenizer loaded successfully!")
19
- except Exception as e:
20
- st.error(f"An error occurred while loading the model: {e}")
21
-
22
- # Load the dataset from the existing file
23
- dataset_path = "training_data.json"
24
-
25
- try:
26
- dataset = load_dataset('json', data_files={'train': dataset_path})
27
- except Exception as e:
28
- st.error(f"An error occurred while loading the dataset: {e}")
29
-
30
- # Tokenize the dataset
31
  def preprocess_function(examples):
 
 
32
  return tokenizer(examples['prompt'], truncation=True, padding="max_length", max_length=128)
33
 
34
- tokenized_dataset = dataset['train'].map(preprocess_function, batched=True)
 
 
 
 
 
 
 
35
 
36
- # Training arguments for fine-tuning
37
- training_args = TrainingArguments(
38
- output_dir="./results",
39
- eval_strategy="epoch", # Use `eval_strategy` instead of `evaluation_strategy`
40
- learning_rate=2e-5,
41
- per_device_train_batch_size=1,
42
- num_train_epochs=3,
43
- weight_decay=0.01,
44
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # Initialize the Trainer for fine-tuning
47
- trainer = Trainer(
48
- model=model,
49
- args=training_args,
50
- train_dataset=tokenized_dataset,
51
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- # Button to start fine-tuning
54
- if st.button("Start Fine-Tuning"):
55
- with st.spinner("Fine-tuning in progress..."):
56
- try:
57
- trainer.train()
58
- st.success("Fine-tuning completed!")
59
- model.save_pretrained("./fine_tuned_model")
60
- st.write("Fine-tuned model saved!")
61
- except Exception as e:
62
- st.error(f"An error occurred during fine-tuning: {e}")
 
1
  import streamlit as st
2
+ import os
3
  from datasets import load_dataset
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
5
+ import pandas as pd
6
+ from io import StringIO
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def preprocess_function(examples):
9
+ if 'prompt' not in examples:
10
+ raise ValueError("Key 'prompt' not found in examples. Please check your dataset fields.")
11
  return tokenizer(examples['prompt'], truncation=True, padding="max_length", max_length=128)
12
 
13
+ def train_model(training_data):
14
+ # Load the dataset
15
+ dataset = load_dataset('json', data_files={'train': training_data})
16
+
17
+ # Initialize the tokenizer and model
18
+ model_name = 'mistral/Mixtral-8x7B' # Replace with the correct model name
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
20
+ model = AutoModelForCausalLM.from_pretrained(model_name)
21
 
22
+ # Tokenize the dataset
23
+ tokenized_dataset = dataset['train'].map(preprocess_function, batched=True)
24
+
25
+ # Define training arguments
26
+ training_args = TrainingArguments(
27
+ output_dir='./results', # Output directory
28
+ evaluation_strategy='epoch', # Evaluation strategy
29
+ learning_rate=2e-5, # Learning rate
30
+ per_device_train_batch_size=4, # Batch size for training
31
+ per_device_eval_batch_size=4, # Batch size for evaluation
32
+ num_train_epochs=3, # Number of training epochs
33
+ weight_decay=0.01, # Strength of weight decay
34
+ logging_dir='./logs', # Directory for storing logs
35
+ logging_steps=10, # Log every 10 steps
36
+ )
37
+
38
+ # Initialize the Trainer
39
+ trainer = Trainer(
40
+ model=model, # The model to train
41
+ args=training_args, # Training arguments
42
+ train_dataset=tokenized_dataset, # Training dataset
43
+ )
44
+
45
+ # Start training
46
+ trainer.train()
47
 
48
+ def main():
49
+ st.title("Model Training with Streamlit")
50
+
51
+ st.write("Upload your training data in JSON format:")
52
+ uploaded_file = st.file_uploader("Choose a file", type="json")
53
+
54
+ if uploaded_file is not None:
55
+ st.write("File uploaded successfully!")
56
+
57
+ # Read the file into a pandas DataFrame
58
+ file_contents = uploaded_file.read().decode("utf-8")
59
+ st.write("Preview of uploaded data:")
60
+ st.text(file_contents[:1000]) # Display first 1000 characters for preview
61
+
62
+ # Save the file to a temporary location
63
+ temp_file_path = 'training_data.json'
64
+ with open(temp_file_path, 'w') as f:
65
+ f.write(file_contents)
66
+
67
+ # Call the train_model function
68
+ st.write("Training the model...")
69
+ train_model(temp_file_path)
70
+ st.write("Training completed!")
71
 
72
+ if __name__ == "__main__":
73
+ main()