Spaces:
Sleeping
Sleeping
File size: 1,920 Bytes
7a87de2 f1016de 7a87de2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
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 |