Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,22 +1,12 @@
|
|
1 |
-
|
|
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
|
4 |
import pandas as pd
|
5 |
-
import
|
6 |
-
import joblib
|
7 |
|
8 |
-
|
9 |
-
# Load your trained model and encoders
|
10 |
-
xgb_model = joblib.load("model/xgb_model.joblib")
|
11 |
-
encoders = joblib.load("model/encoders.joblib")
|
12 |
-
|
13 |
-
# Function to handle unseen labels during encoding
|
14 |
-
def safe_transform(encoder, column):
|
15 |
-
classes = encoder.classes_
|
16 |
-
return [encoder.transform([x])[0] if x in classes else -1 for x in column]
|
17 |
-
|
18 |
-
# Define FastAPI app
|
19 |
app = FastAPI()
|
|
|
20 |
app.add_middleware(
|
21 |
CORSMiddleware,
|
22 |
allow_origins=["*"],
|
@@ -25,46 +15,52 @@ app.add_middleware(
|
|
25 |
allow_headers=["*"],
|
26 |
)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
from fastapi import FastAPI
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
import requests
|
5 |
import pandas as pd
|
6 |
+
import json
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
app = FastAPI()
|
9 |
+
|
10 |
app.add_middleware(
|
11 |
CORSMiddleware,
|
12 |
allow_origins=["*"],
|
|
|
15 |
allow_headers=["*"],
|
16 |
)
|
17 |
|
18 |
+
global page
|
19 |
+
page = 1
|
20 |
+
# Declare the continuous function as an async function.
|
21 |
+
async def your_continuous_function():
|
22 |
+
import pandas as pd
|
23 |
+
import requests
|
24 |
+
import json
|
25 |
+
global page
|
26 |
+
while True:
|
27 |
+
print("data fetcher running.....")
|
28 |
+
|
29 |
+
# Initialize an empty DataFrame to store the combined data
|
30 |
+
combined_df = pd.DataFrame()
|
31 |
+
|
32 |
+
# Update the payload for each page
|
33 |
+
url = "https://dev3.api.curfox.parallaxtec.com/api/ml/order-list?sort=id&paginate=500&page="+str(page)
|
34 |
+
|
35 |
+
payload = {}
|
36 |
+
headers = {
|
37 |
+
'Accept': 'application/json',
|
38 |
+
'X-Tenant': 'royalexpress'
|
39 |
+
}
|
40 |
+
|
41 |
+
response = requests.request("GET", url, headers=headers, data=payload)
|
42 |
+
|
43 |
+
# Sample JSON response
|
44 |
+
json_response = response.json()
|
45 |
+
# Extracting 'data' for conversion
|
46 |
+
data = json_response['data']
|
47 |
+
|
48 |
+
df = pd.json_normalize(data)
|
49 |
+
|
50 |
+
# Concatenate the current page's DataFrame with the combined DataFrame
|
51 |
+
combined_df = pd.concat([combined_df, df], ignore_index=True)
|
52 |
+
|
53 |
+
data = combined_df[combined_df['status.name'].isin(['RETURN TO CLIENT', 'DELIVERED'])]
|
54 |
+
print("data collected from page : "+str(page))
|
55 |
+
page+=1
|
56 |
+
await asyncio.sleep(60) # Adjust the sleep interval as needed
|
57 |
|
58 |
+
# Create a startup event.
|
59 |
+
@app.on_event("startup")
|
60 |
+
async def startup_event():
|
61 |
+
# Start the continuous function as a background task.
|
62 |
+
asyncio.create_task(your_continuous_function())
|
63 |
|
64 |
+
@app.get("/test_api")
|
65 |
+
def test_api():
|
66 |
+
return "kpi_result"
|