prasenjeet099 commited on
Commit
7b8a947
Β·
verified Β·
1 Parent(s): 75d2623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -5
app.py CHANGED
@@ -4,7 +4,8 @@ import time
4
  import os
5
  import pandas as pd
6
  from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments
7
- from datasets import load_dataset
 
8
 
9
  # Set up Streamlit page
10
  st.set_page_config(page_title="AutoTrain AI", page_icon="πŸš€", layout="wide")
@@ -17,7 +18,19 @@ hf_user = st.sidebar.selectbox("Hugging Face User", ["hennings1984"])
17
  task = st.sidebar.selectbox("Select Task", ["Text Classification", "Sentiment Analysis"])
18
  hardware = st.sidebar.selectbox("Hardware", ["CPU", "Single GPU", "Multi-GPU", "TPU"])
19
  model_choice = st.sidebar.selectbox("Choose Model", ["bert-base-uncased", "distilbert-base-uncased", "roberta-base"])
20
- dataset_source = st.sidebar.selectbox("Dataset Source", ["glue/sst2", "imdb", "ag_news", "Custom"])
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Training Parameters
23
  epochs = st.sidebar.slider("Number of Epochs", 1, 10, 3)
@@ -51,6 +64,44 @@ log_area = st.empty()
51
  # Live Training Metrics
52
  st.write("### Training Metrics πŸ“Š")
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Training Function
55
  def train_model():
56
  st.success(f"Training started for {task} with {model_choice} on {device.upper()}")
@@ -60,11 +111,21 @@ def train_model():
60
  model = AutoModelForSequenceClassification.from_pretrained(model_choice, num_labels=2)
61
 
62
  # Load dataset
63
- dataset = load_dataset(dataset_source)
64
-
 
 
 
 
 
 
 
 
 
65
  # Tokenization function
66
  def tokenize_function(examples):
67
- return tokenizer(examples["sentence"], truncation=True, padding="max_length")
 
68
 
69
  tokenized_datasets = dataset.map(tokenize_function, batched=True)
70
  train_dataset = tokenized_datasets["train"]
 
4
  import os
5
  import pandas as pd
6
  from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments
7
+ from datasets import load_dataset, Dataset, DatasetDict
8
+ from sklearn.model_selection import train_test_split
9
 
10
  # Set up Streamlit page
11
  st.set_page_config(page_title="AutoTrain AI", page_icon="πŸš€", layout="wide")
 
18
  task = st.sidebar.selectbox("Select Task", ["Text Classification", "Sentiment Analysis"])
19
  hardware = st.sidebar.selectbox("Hardware", ["CPU", "Single GPU", "Multi-GPU", "TPU"])
20
  model_choice = st.sidebar.selectbox("Choose Model", ["bert-base-uncased", "distilbert-base-uncased", "roberta-base"])
21
+
22
+ # Dataset Configuration
23
+ dataset_source = st.sidebar.selectbox("Dataset Source", ["Hugging Face", "Upload Your Dataset"])
24
+
25
+ if dataset_source == "Hugging Face":
26
+ # Choose Hugging Face dataset
27
+ dataset_name = st.sidebar.text_input("Enter Hugging Face Dataset Name", "imdb")
28
+ else:
29
+ # Upload Custom Dataset
30
+ uploaded_file = st.sidebar.file_uploader("Upload Your Dataset (CSV/TSV/JSON)", type=["csv", "json", "tsv"])
31
+ # Allow the user to map columns
32
+ text_column = st.sidebar.text_input("Text Column Name", "text")
33
+ label_column = st.sidebar.text_input("Label Column Name", "label")
34
 
35
  # Training Parameters
36
  epochs = st.sidebar.slider("Number of Epochs", 1, 10, 3)
 
64
  # Live Training Metrics
65
  st.write("### Training Metrics πŸ“Š")
66
 
67
+ # Dataset Loading Logic
68
+ def load_custom_dataset(uploaded_file, text_column, label_column):
69
+ if uploaded_file is not None:
70
+ file_type = uploaded_file.name.split('.')[-1]
71
+ if file_type == "csv":
72
+ df = pd.read_csv(uploaded_file)
73
+ elif file_type == "json":
74
+ df = pd.read_json(uploaded_file)
75
+ elif file_type == "tsv":
76
+ df = pd.read_csv(uploaded_file, sep="\t")
77
+ else:
78
+ st.error("Unsupported file type")
79
+ return None
80
+
81
+ # Map the columns based on user input
82
+ df = df[[text_column, label_column]]
83
+ return df
84
+ return None
85
+
86
+ def load_huggingface_dataset(dataset_name):
87
+ return load_dataset(dataset_name)
88
+
89
+ def split_dataset(df, text_column, label_column):
90
+ # Split the dataset into train, validation, and test (80/10/10 split)
91
+ train_df, temp_df = train_test_split(df, test_size=0.2, random_state=42)
92
+ val_df, test_df = train_test_split(temp_df, test_size=0.5, random_state=42)
93
+
94
+ # Convert to Hugging Face Dataset format
95
+ train_dataset = Dataset.from_pandas(train_df)
96
+ val_dataset = Dataset.from_pandas(val_df)
97
+ test_dataset = Dataset.from_pandas(test_df)
98
+
99
+ return DatasetDict({
100
+ "train": train_dataset,
101
+ "validation": val_dataset,
102
+ "test": test_dataset
103
+ })
104
+
105
  # Training Function
106
  def train_model():
107
  st.success(f"Training started for {task} with {model_choice} on {device.upper()}")
 
111
  model = AutoModelForSequenceClassification.from_pretrained(model_choice, num_labels=2)
112
 
113
  # Load dataset
114
+ if dataset_source == "Hugging Face":
115
+ dataset = load_huggingface_dataset(dataset_name)
116
+ else:
117
+ dataset_df = load_custom_dataset(uploaded_file, text_column, label_column)
118
+ if dataset_df is not None:
119
+ # Split dataset if it's not already split
120
+ dataset = split_dataset(dataset_df, text_column, label_column)
121
+
122
+ # Check the dataset structure to identify the correct column name
123
+ st.write(f"Dataset columns: {dataset['train'].column_names}")
124
+
125
  # Tokenization function
126
  def tokenize_function(examples):
127
+ # Adjust this based on the actual column name
128
+ return tokenizer(examples[text_column], truncation=True, padding="max_length")
129
 
130
  tokenized_datasets = dataset.map(tokenize_function, batched=True)
131
  train_dataset = tokenized_datasets["train"]