Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- app/main.py +33 -13
app/main.py
CHANGED
@@ -25,31 +25,53 @@ session_assistant = ChatPDF()
|
|
25 |
|
26 |
logging.basicConfig(level=logging.INFO)
|
27 |
logger = logging.getLogger(__name__)
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
@app.middleware("http")
|
31 |
async def resolve_availability(request: Request, call_next):
|
32 |
-
|
33 |
-
if isBusy:
|
34 |
return PlainTextResponse("Server is busy", status_code=503)
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
|
39 |
def astreamer(generator):
|
40 |
-
global isBusy
|
41 |
t0 = time.time()
|
42 |
for i in generator:
|
43 |
logger.info(f"Chunk being yielded (time {int((time.time()-t0)*1000)}ms) - {i}")
|
44 |
yield i
|
45 |
logger.info(f"X-Process-Time: {int((time.time()-t0)*1000)}ms")
|
46 |
-
isBusy = False
|
47 |
|
48 |
|
49 |
@app.get("/query")
|
50 |
async def process_input(text: str):
|
51 |
-
|
52 |
-
isBusy = True
|
53 |
generator = None
|
54 |
if text and len(text.strip()) > 0:
|
55 |
if session_assistant.pdf_count > 0:
|
@@ -67,8 +89,7 @@ async def process_input(text: str):
|
|
67 |
|
68 |
@app.post("/upload")
|
69 |
def upload(files: list[UploadFile]):
|
70 |
-
|
71 |
-
isBusy = True
|
72 |
try:
|
73 |
os.makedirs(files_dir)
|
74 |
for file in files:
|
@@ -89,8 +110,7 @@ def upload(files: list[UploadFile]):
|
|
89 |
|
90 |
@app.get("/clear")
|
91 |
def clear():
|
92 |
-
|
93 |
-
isBusy = True
|
94 |
session_assistant.clear()
|
95 |
message = "Your files have been cleared successfully."
|
96 |
generator = re.split(r'(\s)', message)
|
|
|
25 |
|
26 |
logging.basicConfig(level=logging.INFO)
|
27 |
logger = logging.getLogger(__name__)
|
28 |
+
|
29 |
+
class Session:
|
30 |
+
isBusy = False # Processing upload or query response
|
31 |
+
curUserID = ""
|
32 |
+
prevUserID = ""
|
33 |
+
lastQueryTimestamp = 0
|
34 |
+
|
35 |
+
session = Session()
|
36 |
|
37 |
@app.middleware("http")
|
38 |
async def resolve_availability(request: Request, call_next):
|
39 |
+
if session.isBusy:
|
|
|
40 |
return PlainTextResponse("Server is busy", status_code=503)
|
41 |
+
|
42 |
+
request_args = dict(request.query_params)
|
43 |
+
if not "id" in request_args:
|
44 |
+
return PlainTextResponse("Bad request", status_code=400)
|
45 |
+
|
46 |
+
now = time.time()
|
47 |
+
if session.curUserID == request_args["id"]:
|
48 |
+
session.lastQueryTimestamp = now
|
49 |
+
return await call_next(request)
|
50 |
+
|
51 |
+
if session.prevUserID == request_args["id"]:
|
52 |
+
return PlainTextResponse("Session has expired", status_code=419)
|
53 |
+
|
54 |
+
if now - session.lastQueryTimestamp >= 300:
|
55 |
+
session.lastQueryTimestamp = now
|
56 |
+
session.prevUserID = session.curUserID
|
57 |
+
session.curUserID = request_args["id"]
|
58 |
+
return await call_next(request)
|
59 |
+
|
60 |
+
return PlainTextResponse("Server is busy", status_code=503)
|
61 |
|
62 |
|
63 |
def astreamer(generator):
|
|
|
64 |
t0 = time.time()
|
65 |
for i in generator:
|
66 |
logger.info(f"Chunk being yielded (time {int((time.time()-t0)*1000)}ms) - {i}")
|
67 |
yield i
|
68 |
logger.info(f"X-Process-Time: {int((time.time()-t0)*1000)}ms")
|
69 |
+
session.isBusy = False
|
70 |
|
71 |
|
72 |
@app.get("/query")
|
73 |
async def process_input(text: str):
|
74 |
+
session.isBusy = True
|
|
|
75 |
generator = None
|
76 |
if text and len(text.strip()) > 0:
|
77 |
if session_assistant.pdf_count > 0:
|
|
|
89 |
|
90 |
@app.post("/upload")
|
91 |
def upload(files: list[UploadFile]):
|
92 |
+
session.isBusy = True
|
|
|
93 |
try:
|
94 |
os.makedirs(files_dir)
|
95 |
for file in files:
|
|
|
110 |
|
111 |
@app.get("/clear")
|
112 |
def clear():
|
113 |
+
session.isBusy = True
|
|
|
114 |
session_assistant.clear()
|
115 |
message = "Your files have been cleared successfully."
|
116 |
generator = re.split(r'(\s)', message)
|