Spaces:
Paused
Paused
Commit
·
ef18a5d
1
Parent(s):
f61e523
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
from typing import Union
|
| 2 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
| 3 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 4 |
|
| 5 |
from answerer import Answerer
|
|
@@ -63,30 +65,21 @@ def index():
|
|
| 63 |
|
| 64 |
@app.get("/map")
|
| 65 |
def map(query: Union[str, None], items: Union[list[str], None]):
|
| 66 |
-
|
| 67 |
-
return JSONResponse(
|
| 68 |
|
| 69 |
@app.websocket("/answer")
|
| 70 |
async def answer(ws: WebSocket):
|
| 71 |
-
print("ws started!")
|
| 72 |
-
|
| 73 |
await ws.accept()
|
| 74 |
-
|
| 75 |
-
print("ws accepted!")
|
| 76 |
|
| 77 |
try: input = await ws.receive_text()
|
| 78 |
except WebSocketDisconnect: return
|
| 79 |
|
| 80 |
-
print("input received!")
|
| 81 |
-
|
| 82 |
-
await ws.send_text("OK!")
|
| 83 |
-
|
| 84 |
output = answerer(input, 32)
|
| 85 |
-
|
| 86 |
-
print("output created!")
|
| 87 |
-
|
| 88 |
async for el in output:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
await ws.close()
|
|
|
|
| 1 |
from typing import Union
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from fastapi.encoders import jsonable_encoder
|
| 4 |
+
from fastapi.websockets import WebSocket, WebSocketDisconnect, WebSocketState
|
| 5 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 6 |
|
| 7 |
from answerer import Answerer
|
|
|
|
| 65 |
|
| 66 |
@app.get("/map")
|
| 67 |
def map(query: Union[str, None], items: Union[list[str], None]):
|
| 68 |
+
scores = mapper(query, items)
|
| 69 |
+
return JSONResponse(jsonable_encoder(scores))
|
| 70 |
|
| 71 |
@app.websocket("/answer")
|
| 72 |
async def answer(ws: WebSocket):
|
|
|
|
|
|
|
| 73 |
await ws.accept()
|
|
|
|
|
|
|
| 74 |
|
| 75 |
try: input = await ws.receive_text()
|
| 76 |
except WebSocketDisconnect: return
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
output = answerer(input, 32)
|
|
|
|
|
|
|
|
|
|
| 79 |
async for el in output:
|
| 80 |
+
if(ws.client_state == WebSocketState.CONNECTED):
|
| 81 |
+
await ws.send_text(el)
|
| 82 |
+
elif(ws.client_state == WebSocketState.DISCONNECTED):
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
await ws.close()
|