-
Notifications
You must be signed in to change notification settings - Fork 6
/
appWebSocket.py
63 lines (54 loc) · 2.06 KB
/
appWebSocket.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
from fastapi import FastAPI, WebSocket
from app_helper import pre
from predictor import predict1
from multiprocessing.pool import ThreadPool
from news_scraper import scraper
from summarize import Summarize
from extractTicker import stock_graph
from AltException import InvalidQuery, InvalidTicker
app = FastAPI()
@app.websocket("/ws")
async def websocket_handler(ws: WebSocket):
await ws.accept()
while True:
query = await ws.receive_text()
if query is None or query == "":
await ws.send_json({"message": "Invalid query"})
await ws.close(code=1003)
break
try:
news = scraper(query)
news_score = predict1(query, news.results).final_pred
except InvalidQuery as e:
await ws.send_json({"message": str(e)})
await ws.close(code=1003)
break
pool = ThreadPool(processes=4)
inter1 = pool.apply_async(pre, (1, news_score))
inter7 = pool.apply_async(pre, (7, news_score))
inter15 = pool.apply_async(pre, (15, news_score))
inter30 = pool.apply_async(pre, (30, news_score))
list_predicted = {}
list_predicted[1] = inter1.get()
list_predicted[7] = inter7.get()
list_predicted[15] = inter15.get()
list_predicted[30] = inter30.get()
try:
stg = stock_graph(query, [list_predicted[i] for i in [1, 7, 15, 30]])
except InvalidTicker as e:
await ws.send_json({"message": str(e)})
await ws.close(code=1003)
break
responseObj = {
"predictedPrice": {
1: stg.predict_price_value.iloc[1]["price"],
7: stg.predict_price_value.iloc[2]["price"],
15: stg.predict_price_value.iloc[3]["price"],
30: stg.predict_price_value.iloc[4]["price"],
},
"graph": stg.graphSocket(),
"summary": Summarize(news.get_title(), 3).generate_summary()
}
await ws.send_json(responseObj)
await ws.close(code=1000)
break