Spaces:
Sleeping
Sleeping
import gradio as gr | |
#Other Imports | |
import os | |
import pandas as pd | |
import numpy as np | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import accuracy_score | |
import matplotlib.pyplot as plt | |
from sklearn.preprocessing import StandardScaler | |
def train_model(data, target): | |
# dependent and independent variables | |
X = data.drop(columns=target) | |
y = data[target] | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) | |
#standardize the data | |
sc = StandardScaler() | |
X_train = sc.fit_transform(X_train) | |
X_test = sc.transform(X_test) | |
#train the model | |
model = LogisticRegression(random_state=0, solver='lbfgs', multi_class='auto') | |
model.fit(X_train, y_train) | |
#print the accuracy score | |
y_pred = model.predict(X_test) | |
accuracy = accuracy_score(y_test, y_pred) | |
return accuracy | |
# Upload csv file and train the model | |
def upload_csv(Input_CSV, Target_Variable): | |
columns = list(pd.read_csv('./' + Input_CSV).columns) | |
if Target_Variable not in columns: | |
Target_Variable = columns[-1] | |
data = pd.read_csv('./' + Input_CSV) | |
accuracy = train_model(data, Target_Variable) | |
return (data.head(4)), Target_Variable, accuracy | |
#list the csv files in current working directory | |
files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('csv')] | |
#set the inputs and corresponding outputs | |
inputs = [gr.Dropdown(files, chioces=True), gr.Textbox()] | |
outputs = ['dataframe', gr.Textbox(label="Target Variable"), gr.Textbox(label="Accuracy Score")] | |
#launch the dashboard | |
demo = gr.Interface(upload_csv, inputs, outputs) | |
demo.launch() | |
#in some cases this line might produce an error | |
# in case the above block of code throws error | |
# an argument needs to be passed in launch() | |
# demo.launch(share=True) | |
# the above line when run, solves the error |