Create src/fine_tune_helpers.py
Browse files- src/fine_tune_helpers.py +39 -0
src/fine_tune_helpers.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from datasets import Dataset
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
def fine_tune_model(uploaded_file):
|
| 7 |
+
df = pd.read_csv(uploaded_file)
|
| 8 |
+
st.subheader("Dataset Preview")
|
| 9 |
+
st.write(df.head())
|
| 10 |
+
|
| 11 |
+
# Convert CSV to Hugging Face dataset format
|
| 12 |
+
dataset = Dataset.from_pandas(df)
|
| 13 |
+
|
| 14 |
+
model_name = st.selectbox("Select model for fine-tuning", ["distilbert-base-uncased"])
|
| 15 |
+
|
| 16 |
+
if st.button("Fine-tune Model"):
|
| 17 |
+
if model_name:
|
| 18 |
+
try:
|
| 19 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
+
|
| 22 |
+
def preprocess_function(examples):
|
| 23 |
+
return tokenizer(examples['text'], truncation=True, padding=True)
|
| 24 |
+
|
| 25 |
+
tokenized_datasets = dataset.map(preprocess_function, batched=True)
|
| 26 |
+
|
| 27 |
+
# Fine-tuning logic (example)
|
| 28 |
+
train_args = {
|
| 29 |
+
"output_dir": "./results",
|
| 30 |
+
"num_train_epochs": 3,
|
| 31 |
+
"per_device_train_batch_size": 16,
|
| 32 |
+
"logging_dir": "./logs",
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
st.success("Fine-tuning started (demo)!") # Fine-tuning process goes here
|
| 36 |
+
except Exception as e:
|
| 37 |
+
st.error(f"Error during fine-tuning: {e}")
|
| 38 |
+
else:
|
| 39 |
+
st.warning("Please select a model for fine-tuning.")
|