File size: 1,491 Bytes
6458a1a
 
9c49f3a
6458a1a
 
85dbfd5
 
1425b2c
6458a1a
b474372
 
85dbfd5
 
 
7cf34f0
 
 
 
5ae2a0a
85dbfd5
 
 
5ae2a0a
 
 
 
 
85dbfd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ae2a0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
from pathlib import Path
from fastapi import Form
from fastapi.responses import JSONResponse
from langchain.text_splitter import RecursiveCharacterTextSplitter
from PyPDF2 import PdfReader
from fastapi import Depends

app = FastAPI()

class FileToProcess(BaseModel):
    uploaded_file: UploadFile = File(...)

@app.get("/")
async def home():
    return "API Working!"

@app.post("/file/upload")
async def upload_file(username: str, file_to_process: FileToProcess = Depends()):
    uploaded_file = file_to_process.uploaded_file

    path_to_save_file = Path.home() / username / "saved_files"
    path_to_save_file.mkdir(parents=True, exist_ok=True)
    file_location = f"{path_to_save_file}/{uploaded_file.filename}"
    with open(file_location, "wb+") as file_object:
        file_object.write(uploaded_file.file.read())

    # 下面是你要处理的代码
    text_splitter = RecursiveCharacterTextSplitter(        
        #separator = "\n",
        chunk_size = 500,
        chunk_overlap  = 100, #striding over the text
        length_function = len,
    )        

    doc_reader = PdfReader(file_location)
    raw_text = ''
    for i, page in enumerate(doc_reader.pages):
        text = page.extract_text()
        if text:
            raw_text += text
    temp_texts = text_splitter.split_text(raw_text)
    print(temp_texts)

    return {"INFO": f"File '{uploaded_file.filename}' saved to your profile."}