-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (99 loc) · 3.28 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from fastapi import FastAPI
import pickle
from pydantic import BaseModel
import numpy as np
import joblib
from datetime import datetime, timedelta
from fastapi.middleware.cors import CORSMiddleware
import psycopg2
# model
lasso_model = joblib.load('lasso_model.pkl')
class PredictionRequest(BaseModel):
day: int
month: int
yesterday_price: float
forecast_days: int
# api
app = FastAPI()
origins = [
"http://localhost",
"http://localhost:4200",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def connexion():
try:
conn = psycopg2.connect(
host="localhost",
port = "5432",
database="postgres",
user="postgres",
password="4242"
)
except psycopg2.Error as e:
return {"error": f"Failed to connect to database: {str(e)}"}
return conn
@app.post("/predict")
def predict(data: PredictionRequest):
day = data.day
month = data.month
yesterday_price = data.yesterday_price
forecast_days = data.forecast_days
# Connect to the PostgreSQL database
conn=connexion()
# Create the table if it doesn't exist
with conn:
with conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS predictions (
id SERIAL PRIMARY KEY,
date DATE NOT NULL,
prediction FLOAT NOT NULL
)
""")
predictions = []
current_day = day
current_month = month
current_price = yesterday_price
for _ in range(forecast_days):
# Make a prediction using the current inputs
prediction = lasso_model.predict([[current_day, current_month, current_price]])
predictions.append({"date": datetime(year=2023, month=current_month, day=current_day).strftime("%d/%m/%Y"), "prediction": prediction[0]})
# Insert the prediction into the PostgreSQL database
with conn:
with conn.cursor() as cur:
try:
cur.execute("""
INSERT INTO postgres (date, prediction) VALUES (%s, %s)
""", (datetime(year=2023, month=current_month, day=current_day), prediction[0]))
except psycopg2.Error as e:
return {"error": f"Failed to insert data into database: {str(e)}"}
# Update the inputs
current_date = datetime(year=2023, month=current_month, day=current_day)
next_date = current_date + timedelta(days=1)
current_day = next_date.day
current_month = next_date.month
current_price = prediction[0]
# Close the database connection
conn.close()
@app.get("/get_predict")
def get_predict():
conn = connexion()
cur = conn.cursor()
# Execute a SELECT query to retrieve all rows from the 'predictions' table
cur.execute("SELECT * FROM predictions")
# Fetch all rows and print them
rows = cur.fetchall()
# cur.close()
# conn.close()
return rows
# # Close the cursor and database connection
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='localhost', port=5000)
# python -m uvicorn main:app --reload