toddmattingly commited on
Commit
beb9576
·
verified ·
1 Parent(s): f19c4ad

Upload 4 files

Browse files
.gitattributes CHANGED
@@ -16,3 +16,4 @@ best_rf_model.joblib filter=lfs diff=lfs merge=lfs -text
16
  best_xgb_model_20260222_202200.joblib filter=lfs diff=lfs merge=lfs -text
17
  Random_Forest_pipeline_for_hf.joblib filter=lfs diff=lfs merge=lfs -text
18
  model.joblib filter=lfs diff=lfs merge=lfs -text
 
 
16
  best_xgb_model_20260222_202200.joblib filter=lfs diff=lfs merge=lfs -text
17
  Random_Forest_pipeline_for_hf.joblib filter=lfs diff=lfs merge=lfs -text
18
  model.joblib filter=lfs diff=lfs merge=lfs -text
19
+ final_winner_deployment_bundle.joblib filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ RUN apt-get update && apt-get install -y --no-install-recommends build-essential \
4
+ && rm -rf /var/lib/apt/lists/*
5
+
6
+ WORKDIR /app
7
+
8
+ COPY requirements.txt /app/requirements.txt
9
+ RUN pip install --no-cache-dir -r /app/requirements.txt
10
+
11
+ COPY . /app
12
+
13
+ EXPOSE 7860
14
+
15
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import joblib
3
+ import numpy as np
4
+ import pandas as pd
5
+ import gradio as gr
6
+
7
+ BUNDLE_PATH = "final_winner_deployment_bundle.joblib"
8
+
9
+ def load_bundle():
10
+ if not os.path.exists(BUNDLE_PATH):
11
+ raise FileNotFoundError(
12
+ f"Missing '{BUNDLE_PATH}'. Upload it to the Space repo root."
13
+ )
14
+
15
+ bundle = joblib.load(BUNDLE_PATH)
16
+
17
+ model = bundle["model"]
18
+ preproc_bundle = bundle["preprocessor"]
19
+ metadata = bundle.get("metadata", {})
20
+ eval_space = bundle.get("evaluation_space", "raw")
21
+ winner_name = bundle.get("winner_name", "Unknown")
22
+
23
+ preprocessor = preproc_bundle["pipeline"]
24
+ drop_cols = preproc_bundle.get("drop_cols", [])
25
+ target = metadata.get("target", "Product_Store_Sales_Total")
26
+
27
+ return model, preprocessor, drop_cols, target, eval_space, winner_name
28
+
29
+
30
+ MODEL, PREPROC, DROP_COLS, TARGET, EVAL_SPACE, WINNER = load_bundle()
31
+
32
+
33
+ def _prepare_features(df):
34
+ df = df.copy()
35
+ df = df.drop(columns=DROP_COLS, errors="ignore")
36
+ if TARGET in df.columns:
37
+ df = df.drop(columns=[TARGET], errors="ignore")
38
+ return df
39
+
40
+
41
+ def predict_from_df(df):
42
+ X_df = _prepare_features(df)
43
+ X = PREPROC.transform(X_df)
44
+ pred = MODEL.predict(X)
45
+
46
+ if EVAL_SPACE == "log1p":
47
+ pred = np.expm1(pred)
48
+
49
+ out = df.copy()
50
+ out["prediction"] = pred
51
+ return out
52
+
53
+
54
+ def predict_csv(file):
55
+ if file is None:
56
+ return None, "Please upload a CSV file."
57
+
58
+ df = pd.read_csv(file.name)
59
+ out = predict_from_df(df)
60
+
61
+ out_path = "predictions.csv"
62
+ out.to_csv(out_path, index=False)
63
+
64
+ msg = f"Predicted {len(df)} rows using {WINNER}."
65
+ return out_path, msg
66
+
67
+
68
+ def predict_single_row(json_text):
69
+ if not json_text.strip():
70
+ return "Please paste a JSON object."
71
+
72
+ try:
73
+ row = pd.read_json(json_text, typ="series")
74
+ except Exception as e:
75
+ return f"Invalid JSON: {e}"
76
+
77
+ df = pd.DataFrame([row.to_dict()])
78
+ out = predict_from_df(df)
79
+ return f"{float(out['prediction'].iloc[0]):,.4f}"
80
+
81
+
82
+ with gr.Blocks(title="SuperKart Sales Predictor") as demo:
83
+ gr.Markdown(f"""
84
+ # SuperKart Sales Prediction
85
+
86
+ Winner model: {WINNER}
87
+ Prediction space: {EVAL_SPACE}
88
+ """)
89
+
90
+ with gr.Tab("CSV Upload"):
91
+ file_in = gr.File(label="Upload CSV")
92
+ file_out = gr.File(label="Download predictions.csv")
93
+ status = gr.Textbox(label="Status")
94
+ btn = gr.Button("Run Predictions")
95
+ btn.click(predict_csv, inputs=file_in, outputs=[file_out, status])
96
+
97
+ with gr.Tab("Single Row (JSON)"):
98
+ json_in = gr.Textbox(label="Row JSON", lines=8)
99
+ pred_out = gr.Textbox(label="Prediction")
100
+ btn2 = gr.Button("Predict")
101
+ btn2.click(predict_single_row, inputs=json_in, outputs=pred_out)
102
+
103
+ demo.launch(server_name="0.0.0.0", server_port=7860)
final_winner_deployment_bundle.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:47811420ad804cdc5935dccddfc0e2a79629fad3cb65f9dcf9dbcac5023994b8
3
+ size 79687628
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==4.44.1
2
+ pandas==2.2.2
3
+ numpy==1.26.4
4
+ scikit-learn==1.4.2
5
+ joblib==1.4.2
6
+ xgboost==2.0.3