sgbaird commited on
Commit
3d21777
·
1 Parent(s): 298b47c

api example and max rows workaround

Browse files
Files changed (2) hide show
  1. api_example.py +35 -0
  2. app.py +12 -2
api_example.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gradio_client import Client
2
+ from sklearn.datasets import load_linnerud
3
+ import pandas as pd
4
+ import numpy as np
5
+ from time import time
6
+
7
+ X, y = load_linnerud(return_X_y=True, as_frame=True)
8
+
9
+ # create a dataframe with 1000 randomly generated values for predicting
10
+ rng = np.random.default_rng(42)
11
+ num_pred = 1000
12
+ X_pred = pd.DataFrame(
13
+ {
14
+ "Chins": 50 * rng.random(num_pred),
15
+ "Situps": 80 * rng.random(num_pred),
16
+ "Jumps": 20 * rng.random(num_pred),
17
+ }
18
+ )
19
+
20
+ client = Client("AccelerationConsortium/sklearn-train-basic")
21
+
22
+ t0 = time()
23
+ result = client.predict(
24
+ {
25
+ "headers": X_pred.columns.tolist(),
26
+ "data": X_pred.values.tolist(),
27
+ }, # Dict(headers: List[str], data: List[List[Any]], metadata: Dict(str, List[Any] | None) | None) in 'X' Dataframe component
28
+ api_name="/predict",
29
+ )
30
+
31
+ print(f"Time taken: {time() - t0:.2f}s")
32
+
33
+ result_df = pd.DataFrame(result["data"], columns=result["headers"])
34
+
35
+ print(result_df)
app.py CHANGED
@@ -10,8 +10,19 @@ X, y = load_linnerud(return_X_y=True, as_frame=True)
10
  regr = MultiOutputRegressor(Ridge(random_state=123)).fit(X, y)
11
  # example usage: regr.predict(X.iloc[[0]])
12
 
 
 
 
 
 
 
 
 
 
 
13
  iface = gr.Interface(
14
- fn=regr.predict,
 
15
  inputs=gr.Dataframe(
16
  value=X.head(1),
17
  headers=list(X.columns),
@@ -24,7 +35,6 @@ iface = gr.Interface(
24
  headers=list(y.columns),
25
  col_count=(y.shape[1], "fixed"),
26
  datatype=y.dtypes.apply(str).replace("float64", "number").values.tolist(),
27
- min_width=50,
28
  ),
29
  )
30
  iface.launch()
 
10
  regr = MultiOutputRegressor(Ridge(random_state=123)).fit(X, y)
11
  # example usage: regr.predict(X.iloc[[0]])
12
 
13
+
14
+ def predict(X):
15
+ max_rows = 100000
16
+ if X.shape[0] > max_rows:
17
+ raise ValueError(
18
+ f"Too many rows ({X.shape[0]}), please use less than {max_rows} rows."
19
+ )
20
+ return regr.predict(X)
21
+
22
+
23
  iface = gr.Interface(
24
+ title="MultiOutputRegressor Example",
25
+ fn=predict,
26
  inputs=gr.Dataframe(
27
  value=X.head(1),
28
  headers=list(X.columns),
 
35
  headers=list(y.columns),
36
  col_count=(y.shape[1], "fixed"),
37
  datatype=y.dtypes.apply(str).replace("float64", "number").values.tolist(),
 
38
  ),
39
  )
40
  iface.launch()