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