Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- app/main.py +17 -3
app/main.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import os
|
|
|
2 |
import logging
|
3 |
import pathlib
|
4 |
import time
|
5 |
import re
|
6 |
from typing import List
|
7 |
-
from fastapi import FastAPI, Request, Depends
|
8 |
from fastapi.middleware import Middleware
|
9 |
from fastapi.middleware.cors import CORSMiddleware
|
10 |
from fastapi.responses import StreamingResponse
|
@@ -27,7 +28,7 @@ session_assistant = ChatPDF()
|
|
27 |
|
28 |
logging.basicConfig(level=logging.INFO)
|
29 |
logger = logging.getLogger(__name__)
|
30 |
-
|
31 |
|
32 |
def astreamer(generator):
|
33 |
t0 = time.time()
|
@@ -39,6 +40,10 @@ def astreamer(generator):
|
|
39 |
|
40 |
@app.get("/query")
|
41 |
async def process_input(text: str):
|
|
|
|
|
|
|
|
|
42 |
generator = None
|
43 |
if text and len(text.strip()) > 0:
|
44 |
if session_assistant.pdf_count > 0:
|
@@ -51,6 +56,7 @@ async def process_input(text: str):
|
|
51 |
else:
|
52 |
message = "The provided query is empty."
|
53 |
generator = re.split(r'(\s)', message)
|
|
|
54 |
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
55 |
|
56 |
async def parse_body(request: Request):
|
@@ -59,6 +65,11 @@ async def parse_body(request: Request):
|
|
59 |
|
60 |
@app.post("/upload")
|
61 |
def upload(filename: str, data: bytes = Depends(parse_body)):
|
|
|
|
|
|
|
|
|
|
|
62 |
if data:
|
63 |
try:
|
64 |
print("Filename: " + filename)
|
@@ -72,14 +83,17 @@ def upload(filename: str, data: bytes = Depends(parse_body)):
|
|
72 |
|
73 |
message = "Files inserted successfully."
|
74 |
generator = re.split(r'(\s)', message)
|
|
|
75 |
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
76 |
|
77 |
|
78 |
@app.get("/clear")
|
79 |
-
def
|
|
|
80 |
session_assistant.clear()
|
81 |
message = "All files have been cleared. The first query may take a little longer."
|
82 |
generator = re.split(r'(\s)', message)
|
|
|
83 |
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
84 |
|
85 |
|
|
|
1 |
import os
|
2 |
+
import traceback
|
3 |
import logging
|
4 |
import pathlib
|
5 |
import time
|
6 |
import re
|
7 |
from typing import List
|
8 |
+
from fastapi import FastAPI, Request, Depends, HTTPException
|
9 |
from fastapi.middleware import Middleware
|
10 |
from fastapi.middleware.cors import CORSMiddleware
|
11 |
from fastapi.responses import StreamingResponse
|
|
|
28 |
|
29 |
logging.basicConfig(level=logging.INFO)
|
30 |
logger = logging.getLogger(__name__)
|
31 |
+
isBusy = False
|
32 |
|
33 |
def astreamer(generator):
|
34 |
t0 = time.time()
|
|
|
40 |
|
41 |
@app.get("/query")
|
42 |
async def process_input(text: str):
|
43 |
+
global isBusy
|
44 |
+
if isBusy:
|
45 |
+
raise HTTPException(status_code=503, detail="Server is busy")
|
46 |
+
isBusy = True
|
47 |
generator = None
|
48 |
if text and len(text.strip()) > 0:
|
49 |
if session_assistant.pdf_count > 0:
|
|
|
56 |
else:
|
57 |
message = "The provided query is empty."
|
58 |
generator = re.split(r'(\s)', message)
|
59 |
+
isBusy = False
|
60 |
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
61 |
|
62 |
async def parse_body(request: Request):
|
|
|
65 |
|
66 |
@app.post("/upload")
|
67 |
def upload(filename: str, data: bytes = Depends(parse_body)):
|
68 |
+
global isBusy
|
69 |
+
if isBusy:
|
70 |
+
raise HTTPException(status_code=503, detail="Server is busy")
|
71 |
+
isBusy = True
|
72 |
+
session_assistant.clear()
|
73 |
if data:
|
74 |
try:
|
75 |
print("Filename: " + filename)
|
|
|
83 |
|
84 |
message = "Files inserted successfully."
|
85 |
generator = re.split(r'(\s)', message)
|
86 |
+
isBusy = False
|
87 |
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
88 |
|
89 |
|
90 |
@app.get("/clear")
|
91 |
+
def clear():
|
92 |
+
global isBusy
|
93 |
session_assistant.clear()
|
94 |
message = "All files have been cleared. The first query may take a little longer."
|
95 |
generator = re.split(r'(\s)', message)
|
96 |
+
isBusy = False
|
97 |
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
98 |
|
99 |
|