From 0bca8e6ce440bd0438369a9d3d23eac7eb3e1e14 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 17 May 2023 03:54:52 +0100 Subject: [PATCH] error --- api-front/.vscode/settings.json | 7 ++ api-front/__pycache__/main.cpython-311.pyc | Bin 0 -> 2643 bytes api-front/lasso_model.pkl | Bin 0 -> 943 bytes api-front/main.py | 124 +++++++++++++++++++++ api-front/query.py | 25 +++++ api_model/lasso_model.pkl | 0 api_model/main.py | 0 src/app/line-chart/line-chart.component.ts | 4 +- 8 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 api-front/.vscode/settings.json create mode 100644 api-front/__pycache__/main.cpython-311.pyc create mode 100644 api-front/lasso_model.pkl create mode 100644 api-front/main.py create mode 100644 api-front/query.py delete mode 100644 api_model/lasso_model.pkl delete mode 100644 api_model/main.py diff --git a/api-front/.vscode/settings.json b/api-front/.vscode/settings.json new file mode 100644 index 0000000..3e99ede --- /dev/null +++ b/api-front/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "." + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/api-front/__pycache__/main.cpython-311.pyc b/api-front/__pycache__/main.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b4823f66879bfb3649c8e1faa72f4c7b9a7236cd GIT binary patch literal 2643 zcmah~%WvC88XvwSO4P%VCBOPuXKg1|62wX2Lz{LBv`M$w1a{n{*0J8{C9dgK_Kyyd|Uz+W1b4YJJw*LWT2naE!0E=R`HwEcldfIO&QI?Vd9nO67 zJ?8u7_05m}NT-ts%8%cb@PA_n{hJF>iT9c3Hw1*fL>OVgL^Yu#2%Hy9u_Qt-nNm$I z$$gztiiNg#DGt4Csx`Hw))J*eXjjZ+O)F^v5)p~tOhpsn*h84%TWGyfN{i@2gyVM* zR*C#j0voixEd##7LlC}=RH&5237o8or7@D-J2X%aF+?L{Si`BRz}utrLk0Yy^@@PN zFBeI}J_(5nX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000000..8d7f57750a6f847d8c25371f617fb9a3392f0578 GIT binary patch literal 943 zcmZuwzfTlF6uuuEcdWpHKr|>qWzNDiXQH8yoCFgSPLFso@ftIkot<+Rc4v2HXTeL* zK*6tMs2r@cv*mxVp|&O3QITkr&Qjm*T}UKOvGaD`_uhQ(dv7-S4nAcw=~9(w#iLj- z({mZ@$PaAlnTP~|uw8~FMK+BHWm2oc6C6c>ey4A3>NmQg3fK!BtRL5&m46=pKKY=E_n6R)4b{@LD)epK;>oN zL6*t;C*Q9x+*ZaK7Cwq37BUG6gUFTcYLZ}3U}guY4-z)iKtw%CfCJNzu0uSRXf@iV zSjK`PhJ6|V9i-LRa$tF`WwO|ZJ42U%4uEVVzw<)i&I^IV5Qv@ETZ&?fc%T7Dkw$4% zWHwH)08)j>!6DW4{1(`t9)zmw0@$|fz`1kPnFdsDX;$y;?&zkj)blF4t{aWU;vZP) zr(ToVQU>gO+WUen_g<5L`fD&5ff?fOc!PJC>cCk&usA= zfnM5_LAl7KTK%wl&@Wcyg>By7BCI#--Y=USq^Gv(ZHrkxp; zYyYG5g9}<`Kx^mbZfu>E_nXgaA8Y$Zi}PRRram+AWApgyuiC-eE|ZKJuw(3@S2%>h znfPxrVA>*rJ)F#ob7!IS?X++1@NlN4x1rU_IDY{^_+H}QIEuu|d-q}2KZ1JC5Uo-X PrNdjOns?2rjxF;q&}(Qa literal 0 HcmV?d00001 diff --git a/api-front/main.py b/api-front/main.py new file mode 100644 index 0000000..71d1327 --- /dev/null +++ b/api-front/main.py @@ -0,0 +1,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 +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 + + \ No newline at end of file diff --git a/api-front/query.py b/api-front/query.py new file mode 100644 index 0000000..b1691d9 --- /dev/null +++ b/api-front/query.py @@ -0,0 +1,25 @@ +import psycopg2 + +# Connect to the PostgreSQL database +conn = psycopg2.connect( + host="localhost", + port = "5432", + database="postgres", + user="postgres", + password="4242" +) + +# Create a cursor object to execute queries +cur = conn.cursor() + +# Execute a SELECT query to retrieve all rows from the 'predictions' table +cur.execute("SELECT * FROM postgres") + +# Fetch all rows and print them +rows = cur.fetchall() +for row in rows: + print(row) + +# Close the cursor and database connection +cur.close() +conn.close() \ No newline at end of file diff --git a/api_model/lasso_model.pkl b/api_model/lasso_model.pkl deleted file mode 100644 index e69de29..0000000 diff --git a/api_model/main.py b/api_model/main.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/line-chart/line-chart.component.ts b/src/app/line-chart/line-chart.component.ts index b64931f..da0942e 100644 --- a/src/app/line-chart/line-chart.component.ts +++ b/src/app/line-chart/line-chart.component.ts @@ -28,10 +28,10 @@ export class LineChartComponent { const input = { - "day": 15, + "day": 18, "month": 5, "yesterday_price": 27639, - "forecast_days": 10 + "forecast_days": 5 }