cloud450 commited on
Commit
ffb9283
·
verified ·
1 Parent(s): e79fe6f

Upload 8 files

Browse files
Files changed (1) hide show
  1. app.py +59 -38
app.py CHANGED
@@ -1,38 +1,59 @@
1
- from fastapi import FastAPI
2
- from fastapi.middleware.cors import CORSMiddleware
3
- import joblib
4
- import pandas as pd
5
-
6
- app = FastAPI()
7
-
8
- app.add_middleware(
9
- CORSMiddleware,
10
- allow_origins=["*"], # Allows all origins
11
- allow_credentials=True,
12
- allow_methods=["*"], # Allows all methods
13
- allow_headers=["*"], # Allows all headers
14
- )
15
-
16
- # Load models once at startup
17
- future_spend_model = joblib.load("future_spend_7d.pkl")
18
- spike_model = joblib.load("spike_probability.pkl")
19
- acc_model = joblib.load("acceleration.pkl")
20
- FEATURES = joblib.load("model_features.pkl")
21
-
22
- @app.get("/")
23
- def root():
24
- return {"status": "ML backend running"}
25
-
26
- @app.post("/predict")
27
- def predict(payload: dict):
28
- X = pd.DataFrame([payload], columns=FEATURES)
29
-
30
- future_spend = future_spend_model.predict(X)[0]
31
- spike_prob = spike_model.predict_proba(X)[0][1]
32
- acceleration = acc_model.predict(X)[0]
33
-
34
- return {
35
- "future_7d_spend": round(float(future_spend), 2),
36
- "spike_probability": round(float(spike_prob), 3),
37
- "acceleration": round(float(acceleration), 2)
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import joblib
5
+ import pandas as pd
6
+
7
+ app = FastAPI()
8
+
9
+ app.add_middleware(
10
+ CORSMiddleware,
11
+ allow_origins=["*"], # Allows all origins
12
+ allow_credentials=True,
13
+ allow_methods=["*"], # Allows all methods
14
+ allow_headers=["*"], # Allows all headers
15
+ )
16
+
17
+ # Load models once at startup
18
+ future_spend_model = joblib.load("future_spend_7d.pkl")
19
+ spike_model = joblib.load("spike_probability.pkl")
20
+ acc_model = joblib.load("acceleration.pkl")
21
+ FEATURES = joblib.load("model_features.pkl")
22
+
23
+ @app.get("/", response_class=HTMLResponse)
24
+ def root():
25
+ return """
26
+ <!DOCTYPE html>
27
+ <html>
28
+ <head>
29
+ <title>ML Backend Status</title>
30
+ <style>
31
+ body { font-family: sans-serif; text-align: center; padding-top: 50px; background-color: #f0f2f5; }
32
+ h1 { color: #333; }
33
+ .status { color: green; font-weight: bold; }
34
+ a { text-decoration: none; color: white; background-color: #007bff; padding: 10px 20px; border-radius: 5px; }
35
+ a:hover { background-color: #0056b3; }
36
+ </style>
37
+ </head>
38
+ <body>
39
+ <h1>ML Backend is <span class="status">RUNNING</span></h1>
40
+ <p>The API is active and ready to accept requests.</p>
41
+ <br>
42
+ <a href="/docs">View API Documentation</a>
43
+ </body>
44
+ </html>
45
+ """
46
+
47
+ @app.post("/predict")
48
+ def predict(payload: dict):
49
+ X = pd.DataFrame([payload], columns=FEATURES)
50
+
51
+ future_spend = future_spend_model.predict(X)[0]
52
+ spike_prob = spike_model.predict_proba(X)[0][1]
53
+ acceleration = acc_model.predict(X)[0]
54
+
55
+ return {
56
+ "future_7d_spend": round(float(future_spend), 2),
57
+ "spike_probability": round(float(spike_prob), 3),
58
+ "acceleration": round(float(acceleration), 2)
59
+ }