Spaces:
Sleeping
Sleeping
vjtulsiyan
commited on
Commit
•
7a87de2
1
Parent(s):
4c5722d
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""gradio_test.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1nr6ieHBAcOKjo04y5MYF-ndbKN7nK2mr
|
8 |
+
"""
|
9 |
+
|
10 |
+
#!pip install gradio
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
+
#Other Imports
|
15 |
+
import os
|
16 |
+
import pandas as pd
|
17 |
+
import numpy as np
|
18 |
+
from sklearn.linear_model import LogisticRegression
|
19 |
+
from sklearn.model_selection import train_test_split
|
20 |
+
from sklearn.metrics import accuracy_score
|
21 |
+
|
22 |
+
import matplotlib.pyplot as plt
|
23 |
+
from sklearn.preprocessing import StandardScaler
|
24 |
+
|
25 |
+
def train_model(data, target):
|
26 |
+
# dependent and independent variables
|
27 |
+
X = data.drop(columns=target)
|
28 |
+
y = data[target]
|
29 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
|
30 |
+
|
31 |
+
#standardize the data
|
32 |
+
sc = StandardScaler()
|
33 |
+
X_train = sc.fit_transform(X_train)
|
34 |
+
X_test = sc.transform(X_test)
|
35 |
+
|
36 |
+
#train the model
|
37 |
+
model = LogisticRegression(random_state=0, solver='lbfgs', multi_class='auto')
|
38 |
+
model.fit(X_train, y_train)
|
39 |
+
|
40 |
+
#print the accuracy score
|
41 |
+
y_pred = model.predict(X_test)
|
42 |
+
accuracy = accuracy_score(y_test, y_pred)
|
43 |
+
|
44 |
+
return accuracy
|
45 |
+
|
46 |
+
# Upload csv file and train the model
|
47 |
+
def upload_csv(Input_CSV, Target_Variable):
|
48 |
+
columns = list(pd.read_csv('./' + Input_CSV).columns)
|
49 |
+
|
50 |
+
if Target_Variable not in columns:
|
51 |
+
Target_Variable = columns[-1]
|
52 |
+
|
53 |
+
data = pd.read_csv('./' + Input_CSV)
|
54 |
+
|
55 |
+
accuracy = train_model(data, Target_Variable)
|
56 |
+
|
57 |
+
return (data.head(4)), Target_Variable, accuracy
|
58 |
+
|
59 |
+
#list the csv files in current working directory
|
60 |
+
files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('csv')]
|
61 |
+
|
62 |
+
#set the inputs and corresponding outputs
|
63 |
+
inputs = [gr.Dropdown(files, chioces=True), gr.Textbox()]
|
64 |
+
outputs = ['dataframe', gr.Textbox(label="Target Variable"), gr.Textbox(label="Accuracy Score")]
|
65 |
+
|
66 |
+
#launch the dashboard
|
67 |
+
demo = gr.Interface(upload_csv, inputs, outputs)
|
68 |
+
demo.launch(share=True)
|
69 |
+
|
70 |
+
#in some cases this line might produce an error
|
71 |
+
# in case the above block of code throws error
|
72 |
+
# an argument needs to be passed in launch()
|
73 |
+
# demo.launch(share=True)
|
74 |
+
# the above line when run, solves the error
|