Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,22 +1,46 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
from pydantic import BaseModel
|
3 |
from pathlib import Path
|
4 |
-
|
5 |
from fastapi import Form
|
6 |
-
|
7 |
from fastapi.responses import JSONResponse
|
|
|
|
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
|
|
|
|
|
|
11 |
@app.get("/")
|
12 |
async def home():
|
13 |
return "API Working!"
|
14 |
|
15 |
@app.post("/file/upload")
|
16 |
-
async def upload_file(username: str,
|
|
|
|
|
17 |
path_to_save_file = Path.home() / username / "saved_files"
|
18 |
path_to_save_file.mkdir(parents=True, exist_ok=True)
|
19 |
file_location = f"{path_to_save_file}/{uploaded_file.filename}"
|
20 |
with open(file_location, "wb+") as file_object:
|
21 |
file_object.write(uploaded_file.file.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return {"INFO": f"File '{uploaded_file.filename}' saved to your profile."}
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
from pydantic import BaseModel
|
3 |
from pathlib import Path
|
|
|
4 |
from fastapi import Form
|
|
|
5 |
from fastapi.responses import JSONResponse
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from PyPDF2 import PdfReader
|
8 |
+
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
class FileToProcess(BaseModel):
|
13 |
+
uploaded_file: UploadFile = File(...)
|
14 |
+
|
15 |
@app.get("/")
|
16 |
async def home():
|
17 |
return "API Working!"
|
18 |
|
19 |
@app.post("/file/upload")
|
20 |
+
async def upload_file(username: str, file_to_process: FileToProcess = Depends()):
|
21 |
+
uploaded_file = file_to_process.uploaded_file
|
22 |
+
|
23 |
path_to_save_file = Path.home() / username / "saved_files"
|
24 |
path_to_save_file.mkdir(parents=True, exist_ok=True)
|
25 |
file_location = f"{path_to_save_file}/{uploaded_file.filename}"
|
26 |
with open(file_location, "wb+") as file_object:
|
27 |
file_object.write(uploaded_file.file.read())
|
28 |
+
|
29 |
+
# 下面是你要处理的代码
|
30 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
31 |
+
#separator = "\n",
|
32 |
+
chunk_size = 500,
|
33 |
+
chunk_overlap = 100, #striding over the text
|
34 |
+
length_function = len,
|
35 |
+
)
|
36 |
+
|
37 |
+
doc_reader = PdfReader(file_location)
|
38 |
+
raw_text = ''
|
39 |
+
for i, page in enumerate(doc_reader.pages):
|
40 |
+
text = page.extract_text()
|
41 |
+
if text:
|
42 |
+
raw_text += text
|
43 |
+
temp_texts = text_splitter.split_text(raw_text)
|
44 |
+
print(temp_texts)
|
45 |
+
|
46 |
return {"INFO": f"File '{uploaded_file.filename}' saved to your profile."}
|