Spaces:
Running
Running
Update main.py
Browse files- app/main.py +24 -7
- requirements.txt +0 -1
app/main.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import time
|
|
|
4 |
from typing import List
|
5 |
from fastapi import FastAPI, UploadFile
|
6 |
from fastapi.middleware import Middleware
|
@@ -22,21 +23,33 @@ app = FastAPI(middleware=middleware)
|
|
22 |
files_dir = os.path.expanduser("~/wtp_be_files/")
|
23 |
session_assistant = ChatPDF()
|
24 |
|
|
|
|
|
|
|
25 |
|
26 |
def astreamer(generator):
|
27 |
t0 = time.time()
|
28 |
for i in generator:
|
29 |
-
|
30 |
yield i
|
31 |
-
|
32 |
|
33 |
|
34 |
@app.get("/query")
|
35 |
async def process_input(text: str):
|
|
|
36 |
if text and len(text.strip()) > 0:
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
|
42 |
@app.post("/upload")
|
@@ -55,13 +68,17 @@ def upload(files: list[UploadFile]):
|
|
55 |
session_assistant.ingest(files_dir)
|
56 |
shutil.rmtree(files_dir)
|
57 |
|
58 |
-
|
|
|
|
|
59 |
|
60 |
|
61 |
@app.get("/clear")
|
62 |
def ping():
|
63 |
session_assistant.clear()
|
64 |
-
|
|
|
|
|
65 |
|
66 |
|
67 |
@app.get("/")
|
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import time
|
4 |
+
import re
|
5 |
from typing import List
|
6 |
from fastapi import FastAPI, UploadFile
|
7 |
from fastapi.middleware import Middleware
|
|
|
23 |
files_dir = os.path.expanduser("~/wtp_be_files/")
|
24 |
session_assistant = ChatPDF()
|
25 |
|
26 |
+
logging.basicConfig(level=logging.INFO)
|
27 |
+
logger = logging.getLogger(__name__)
|
28 |
+
|
29 |
|
30 |
def astreamer(generator):
|
31 |
t0 = time.time()
|
32 |
for i in generator:
|
33 |
+
logger.info(f"Chunk being yielded (time {int((time.time()-t0)*1000)}ms)", flush=True)
|
34 |
yield i
|
35 |
+
logger.info(f"Over (time {int((time.time()-t0)*1000)}ms)", flush=True)
|
36 |
|
37 |
|
38 |
@app.get("/query")
|
39 |
async def process_input(text: str):
|
40 |
+
generator = None
|
41 |
if text and len(text.strip()) > 0:
|
42 |
+
if session_assistant.pdf_count > 0:
|
43 |
+
text = text.strip()
|
44 |
+
streaming_response = session_assistant.ask(text)
|
45 |
+
generator = streaming_response.response_gen
|
46 |
+
else:
|
47 |
+
message = "Please add a PDF document first."
|
48 |
+
generator = re.split(r'(\s)', message)
|
49 |
+
else:
|
50 |
+
message = "The provided query is empty."
|
51 |
+
generator = re.split(r'(\s)', message)
|
52 |
+
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
53 |
|
54 |
|
55 |
@app.post("/upload")
|
|
|
68 |
session_assistant.ingest(files_dir)
|
69 |
shutil.rmtree(files_dir)
|
70 |
|
71 |
+
message = "Files inserted successfully."
|
72 |
+
generator = re.split(r'(\s)', message)
|
73 |
+
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
74 |
|
75 |
|
76 |
@app.get("/clear")
|
77 |
def ping():
|
78 |
session_assistant.clear()
|
79 |
+
message = "All files have been cleared."
|
80 |
+
generator = re.split(r'(\s)', message)
|
81 |
+
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
82 |
|
83 |
|
84 |
@app.get("/")
|
requirements.txt
CHANGED
@@ -6,6 +6,5 @@ llama-index-readers-file
|
|
6 |
llama-index-llms-llama-cpp
|
7 |
llama-index-embeddings-fastembed
|
8 |
qdrant-client
|
9 |
-
websockets
|
10 |
unstructured
|
11 |
fastembed==0.2.7
|
|
|
6 |
llama-index-llms-llama-cpp
|
7 |
llama-index-embeddings-fastembed
|
8 |
qdrant-client
|
|
|
9 |
unstructured
|
10 |
fastembed==0.2.7
|