Spaces:
Running
Running
Update main.py
Browse files- app/main.py +13 -14
app/main.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import os
|
2 |
import logging
|
3 |
-
import
|
4 |
import time
|
5 |
import re
|
6 |
from typing import List
|
7 |
-
from fastapi import FastAPI,
|
8 |
from fastapi.middleware import Middleware
|
9 |
from fastapi.middleware.cors import CORSMiddleware
|
10 |
from fastapi.responses import StreamingResponse
|
@@ -22,6 +22,7 @@ middleware = [
|
|
22 |
app = FastAPI(middleware=middleware)
|
23 |
|
24 |
files_dir = os.path.expanduser("~/wtp_be_files/")
|
|
|
25 |
session_assistant = ChatPDF()
|
26 |
|
27 |
logging.basicConfig(level=logging.INFO)
|
@@ -52,22 +53,20 @@ async def process_input(text: str):
|
|
52 |
generator = re.split(r'(\s)', message)
|
53 |
return StreamingResponse(astreamer(generator), media_type='text/event-stream')
|
54 |
|
|
|
|
|
|
|
55 |
|
56 |
@app.post("/upload")
|
57 |
-
def upload(
|
58 |
try:
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
path = f"{files_dir}/{file.filename}"
|
63 |
-
file.file.seek(0)
|
64 |
-
with open(path, 'wb') as destination:
|
65 |
-
shutil.copyfileobj(file.file, destination)
|
66 |
-
finally:
|
67 |
-
file.file.close()
|
68 |
-
finally:
|
69 |
session_assistant.ingest(files_dir)
|
70 |
-
|
|
|
|
|
71 |
|
72 |
message = "Files inserted successfully."
|
73 |
generator = re.split(r'(\s)', message)
|
|
|
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
|
8 |
from fastapi.middleware import Middleware
|
9 |
from fastapi.middleware.cors import CORSMiddleware
|
10 |
from fastapi.responses import StreamingResponse
|
|
|
22 |
app = FastAPI(middleware=middleware)
|
23 |
|
24 |
files_dir = os.path.expanduser("~/wtp_be_files/")
|
25 |
+
os.makedirs(files_dir)
|
26 |
session_assistant = ChatPDF()
|
27 |
|
28 |
logging.basicConfig(level=logging.INFO)
|
|
|
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):
|
57 |
+
data: bytes = await request.body()
|
58 |
+
return data
|
59 |
|
60 |
@app.post("/upload")
|
61 |
+
def upload(data: bytes = Depends(parse_body)):
|
62 |
try:
|
63 |
+
path = f"{files_dir}/file"
|
64 |
+
with open(path, "wb") as f:
|
65 |
+
f.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
session_assistant.ingest(files_dir)
|
67 |
+
pathlib.Path(path).unlink()
|
68 |
+
except Exception as e:
|
69 |
+
logging.error(traceback.format_exc())
|
70 |
|
71 |
message = "Files inserted successfully."
|
72 |
generator = re.split(r'(\s)', message)
|