Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -17,6 +17,136 @@ app.add_middleware(
|
|
17 |
allow_headers=["*"],
|
18 |
)
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def forecast(monthly_sales):
|
21 |
# Prepare the data for Prophet
|
22 |
monthly_sales.rename(columns={'transaction_date': 'ds', 'sell_qty': 'y'}, inplace=True)
|
@@ -71,19 +201,19 @@ def process_product(product_name, data):
|
|
71 |
return {
|
72 |
"Product Name": product_name,
|
73 |
"next_month": str(result_dict["next_month"]),
|
74 |
-
"predicted_count": result_dict["predicted_count"]
|
75 |
-
"full_trend" : str(full_trend)
|
76 |
}
|
77 |
except Exception as e:
|
78 |
return {
|
79 |
"Product Name": product_name,
|
80 |
"next_month": str(e),
|
81 |
-
"predicted_count": "not predicted"
|
82 |
-
"full_trend" : str(full_trend)
|
83 |
}
|
84 |
|
85 |
-
@app.post("/
|
86 |
-
async def
|
87 |
try:
|
88 |
# main
|
89 |
data, message = dc.get_data(b_id=b_id, product_name="sample")
|
@@ -98,20 +228,33 @@ async def get_product_count_prediction(b_id: int):
|
|
98 |
for future in as_completed(futures):
|
99 |
results.append(future.result())
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
107 |
|
108 |
-
|
109 |
-
raise HTTPException(status_code=400, detail=message)
|
110 |
except Exception as e:
|
111 |
print(str(e))
|
112 |
response_content = {
|
113 |
"status": "error",
|
114 |
"message": str(e),
|
115 |
-
"data": None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
}
|
117 |
-
return
|
|
|
17 |
allow_headers=["*"],
|
18 |
)
|
19 |
|
20 |
+
def insert_data(bid,forecast_data):#mysql-connector-python
|
21 |
+
import mysql.connector
|
22 |
+
import json
|
23 |
+
|
24 |
+
# Define connection parameters
|
25 |
+
host = "68.183.225.237"
|
26 |
+
user = "sm_ml"
|
27 |
+
password = "Fz6/I733"
|
28 |
+
database = "sm_qa_1"
|
29 |
+
|
30 |
+
# Establish connection
|
31 |
+
connection = mysql.connector.connect(
|
32 |
+
host=host,
|
33 |
+
user=user,
|
34 |
+
password=password,
|
35 |
+
database=database
|
36 |
+
)
|
37 |
+
# Create a cursor object
|
38 |
+
cursor = connection.cursor()
|
39 |
+
# Convert forecast_data to JSON string
|
40 |
+
forecast_data_json = json.dumps(forecast_data)
|
41 |
+
|
42 |
+
# SQL command to insert data
|
43 |
+
insert_query = """
|
44 |
+
INSERT INTO sm_product_count_forecast (bid, forecast_data)
|
45 |
+
VALUES (%s, %s)
|
46 |
+
"""
|
47 |
+
|
48 |
+
# Execute the SQL command with data
|
49 |
+
cursor.execute(insert_query, (bid, forecast_data_json))
|
50 |
+
|
51 |
+
# Commit the transaction
|
52 |
+
connection.commit()
|
53 |
+
|
54 |
+
print("Data inserted successfully")
|
55 |
+
|
56 |
+
# Close the cursor and connection
|
57 |
+
cursor.close()
|
58 |
+
connection.close()
|
59 |
+
|
60 |
+
|
61 |
+
def delete_json(b_id):
|
62 |
+
import mysql.connector
|
63 |
+
|
64 |
+
# Define connection parameters
|
65 |
+
host = "68.183.225.237"
|
66 |
+
user = "sm_ml"
|
67 |
+
password = "Fz6/I733"
|
68 |
+
database = "sm_qa_1"
|
69 |
+
|
70 |
+
# Establish connection
|
71 |
+
connection = mysql.connector.connect(
|
72 |
+
host=host,
|
73 |
+
user=user,
|
74 |
+
password=password,
|
75 |
+
database=database
|
76 |
+
)
|
77 |
+
# Create a cursor object
|
78 |
+
cursor = connection.cursor()
|
79 |
+
# SQL command to delete a specific record
|
80 |
+
delete_query = """
|
81 |
+
DELETE FROM sm_product_count_forecast
|
82 |
+
WHERE bid = %s
|
83 |
+
"""
|
84 |
+
|
85 |
+
# Execute the SQL command with the specified BID
|
86 |
+
cursor.execute(delete_query, (b_id,))
|
87 |
+
# Commit the transaction
|
88 |
+
connection.commit()
|
89 |
+
print(f"Record with BID {bid_to_delete} deleted successfully")
|
90 |
+
# Close the cursor and connection
|
91 |
+
cursor.close()
|
92 |
+
connection.close()
|
93 |
+
|
94 |
+
def get_data(b_id):
|
95 |
+
import mysql.connector
|
96 |
+
import json
|
97 |
+
|
98 |
+
# Define connection parameters
|
99 |
+
host = "68.183.225.237"
|
100 |
+
user = "sm_ml"
|
101 |
+
password = "Fz6/I733"
|
102 |
+
database = "sm_qa_1"
|
103 |
+
|
104 |
+
# Establish connection
|
105 |
+
connection = mysql.connector.connect(
|
106 |
+
host=host,
|
107 |
+
user=user,
|
108 |
+
password=password,
|
109 |
+
database=database
|
110 |
+
)
|
111 |
+
|
112 |
+
# Create a cursor object
|
113 |
+
cursor = connection.cursor()
|
114 |
+
|
115 |
+
# SQL command to select data for a specific BID
|
116 |
+
select_query = """
|
117 |
+
SELECT bid, forecast_data, created_at
|
118 |
+
FROM sm_product_count_forecast
|
119 |
+
WHERE bid = %s
|
120 |
+
"""
|
121 |
+
|
122 |
+
# Execute the SQL command with the specified BID
|
123 |
+
cursor.execute(select_query, (b_id,))
|
124 |
+
|
125 |
+
# Fetch the result
|
126 |
+
row = cursor.fetchone()
|
127 |
+
|
128 |
+
if row:
|
129 |
+
bid = row[0]
|
130 |
+
forecast_data_json = row[1]
|
131 |
+
created_at = row[2]
|
132 |
+
|
133 |
+
# Convert JSON string back to Python dictionary
|
134 |
+
forecast_data = json.loads(forecast_data_json)
|
135 |
+
result = {
|
136 |
+
"BID":bid,
|
137 |
+
"created_at":created_at,
|
138 |
+
"forecast_data":forecast_data
|
139 |
+
}
|
140 |
+
return result
|
141 |
+
|
142 |
+
else:
|
143 |
+
return None
|
144 |
+
|
145 |
+
# Close the cursor and connection
|
146 |
+
cursor.close()
|
147 |
+
connection.close()
|
148 |
+
|
149 |
+
|
150 |
def forecast(monthly_sales):
|
151 |
# Prepare the data for Prophet
|
152 |
monthly_sales.rename(columns={'transaction_date': 'ds', 'sell_qty': 'y'}, inplace=True)
|
|
|
201 |
return {
|
202 |
"Product Name": product_name,
|
203 |
"next_month": str(result_dict["next_month"]),
|
204 |
+
"predicted_count": result_dict["predicted_count"]
|
205 |
+
#"full_trend" : str(full_trend)
|
206 |
}
|
207 |
except Exception as e:
|
208 |
return {
|
209 |
"Product Name": product_name,
|
210 |
"next_month": str(e),
|
211 |
+
"predicted_count": "not predicted"
|
212 |
+
#"full_trend" : str(full_trend)
|
213 |
}
|
214 |
|
215 |
+
@app.post("/generate_product_count_prediction")
|
216 |
+
async def generate_product_count_prediction(b_id: int):
|
217 |
try:
|
218 |
# main
|
219 |
data, message = dc.get_data(b_id=b_id, product_name="sample")
|
|
|
228 |
for future in as_completed(futures):
|
229 |
results.append(future.result())
|
230 |
|
231 |
+
|
232 |
+
delete_json(b_id)
|
233 |
+
insert_data(bid,results)
|
234 |
+
return {"status": "success",
|
235 |
+
"b_id":b_id,
|
236 |
+
"message": "Prediction successful and saved to DB",
|
237 |
+
"status_code":200
|
238 |
+
}
|
239 |
|
240 |
+
|
|
|
241 |
except Exception as e:
|
242 |
print(str(e))
|
243 |
response_content = {
|
244 |
"status": "error",
|
245 |
"message": str(e),
|
246 |
+
"data": None,
|
247 |
+
"status_code":200
|
248 |
+
}
|
249 |
+
return response_content
|
250 |
+
|
251 |
+
|
252 |
+
@app.post("/get_product_count_prediction_from_DB")
|
253 |
+
async def get_product_count_prediction_from_DB(b_id: int):
|
254 |
+
response_content = {
|
255 |
+
"status": "done",
|
256 |
+
"message": "data from DB",
|
257 |
+
"data": get_data(b_id),
|
258 |
+
"status_code":200
|
259 |
}
|
260 |
+
return response_content
|