smithmegan commited on
Commit
ce151f9
·
verified ·
1 Parent(s): 8861196

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -1,37 +1,24 @@
1
- import spaces
2
-
3
- @spaces.GPU
4
- def train_and_predict():
5
- model.fit(x_train, y_train.values.ravel())
6
- return model.predict(x_test)
7
-
8
- y_pred = train_and_predict()
9
  import os
10
  import numpy as np
11
  import pandas as pd
12
  import gradio as gr
 
13
  from sklearn.model_selection import train_test_split
14
  from sklearn.linear_model import LogisticRegression
15
  from sklearn import metrics
16
 
17
  ### ------------------------------ ###
18
- ### Data & File Check ###
19
  ### ------------------------------ ###
20
 
21
- # Ensure data.csv exists
22
- if not os.path.exists('data.csv'):
23
- raise FileNotFoundError("Missing 'data.csv'! Please upload data.csv to your Hugging Face Space files.")
24
-
25
  uncleaned_data = pd.read_csv('data.csv')
26
 
27
- # Verify column count
28
- if uncleaned_data.shape[1] < 2:
29
- raise ValueError("'data.csv' must contain at least 2 columns (a timestamp/ID column and at least one feature or target column).")
30
-
31
- # Remove timestamp column (first column)
32
  uncleaned_data = uncleaned_data.iloc[:, 1:]
33
  data = pd.DataFrame()
34
 
 
35
  cat_value_dicts = {}
36
  final_colname = uncleaned_data.columns[-1]
37
 
@@ -67,8 +54,14 @@ y = data[final_colname]
67
 
68
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
69
 
70
- model = LogisticRegression(max_iter=1000)
71
- model.fit(x_train, y_train.values.ravel())
 
 
 
 
 
 
72
  y_pred = model.predict(x_test)
73
 
74
  acc = str(round(metrics.accuracy_score(y_test, y_pred) * 100, 1)) + "%"
@@ -101,6 +94,8 @@ if os.path.exists('info.md'):
101
  ### Interface Creation ###
102
  ### ------------------------------- ###
103
 
 
 
104
  def general_predictor(*args):
105
  features = []
106
  for colname, arg in zip(feature_cols, args):
@@ -149,5 +144,4 @@ with gr.Blocks() as demo:
149
  with gr.Column():
150
  gr.Markdown(body_text)
151
 
152
- # Launch without Node.js SSR proxy to prevent instant shutdowns
153
- demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
 
 
 
 
 
 
 
 
 
1
  import os
2
  import numpy as np
3
  import pandas as pd
4
  import gradio as gr
5
+ import spaces
6
  from sklearn.model_selection import train_test_split
7
  from sklearn.linear_model import LogisticRegression
8
  from sklearn import metrics
9
 
10
  ### ------------------------------ ###
11
+ ### Data Transformation ###
12
  ### ------------------------------ ###
13
 
14
+ # Load dataset
 
 
 
15
  uncleaned_data = pd.read_csv('data.csv')
16
 
17
+ # Remove timestamp from dataset (first column)
 
 
 
 
18
  uncleaned_data = uncleaned_data.iloc[:, 1:]
19
  data = pd.DataFrame()
20
 
21
+ # Track categorical mappings
22
  cat_value_dicts = {}
23
  final_colname = uncleaned_data.columns[-1]
24
 
 
54
 
55
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
56
 
57
+ # ZeroGPU decorator on startup model training
58
+ @spaces.GPU
59
+ def train_model(x_tr, y_tr):
60
+ clf = LogisticRegression(max_iter=1000)
61
+ clf.fit(x_tr, y_tr.values.ravel())
62
+ return clf
63
+
64
+ model = train_model(x_train, y_train)
65
  y_pred = model.predict(x_test)
66
 
67
  acc = str(round(metrics.accuracy_score(y_test, y_pred) * 100, 1)) + "%"
 
94
  ### Interface Creation ###
95
  ### ------------------------------- ###
96
 
97
+ # ZeroGPU decorator on prediction function
98
+ @spaces.GPU
99
  def general_predictor(*args):
100
  features = []
101
  for colname, arg in zip(feature_cols, args):
 
144
  with gr.Column():
145
  gr.Markdown(body_text)
146
 
147
+ demo.launch()