File size: 2,954 Bytes
0f2cd64
f4c8d5e
 
 
 
 
 
 
6351d67
f4c8d5e
6024be4
 
 
 
 
 
 
aa05778
 
 
 
f4c8d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5e556a
93c60e9
0f2cd64
 
 
 
 
 
93c60e9
cb502f0
 
 
e5e556a
aa05778
 
 
 
e5e556a
93c60e9
e5e556a
f4c8d5e
aa05778
f4c8d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43a47b5
f4c8d5e
43a47b5
 
a032a3b
 
aec9d8a
f4c8d5e
5189e82
a032a3b
 
93c60e9
5189e82
43a47b5
 
aec9d8a
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from fastapi import FastAPI, File, UploadFile, Request
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
#在FastAPI中,Depends()函数用于声明依赖项

import random
import string
import sys
import timeit
import datetime
import io

import os
from dotenv import load_dotenv
load_dotenv()

def generate_random_string(length):
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(length))  

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
    print("File received:"+uploaded_file.filename)
    print("File received:", uploaded_file.filename)
    print("File received: " + str(uploaded_file.filename))    

    username = request.query_params.get("username")
    filename = request.query_params.get("filename")
    print(username)
    print(filename)
    
    #print("File received:"+file_to_process.filename)
    #`file_to_process`是`FileToProcess`类的一个实例,它具有一个名为`uploaded_file`的属性,而不是`filename`属性。
    #因此,在打印`"File received:"+file_to_process.filename`时会引发`AttributeError`。
        
    random_string = generate_random_string(20)
    file_path = Path.cwd() / random_string
    file_path.mkdir(parents=True, exist_ok=True)
    file_saved_in_api = file_path / uploaded_file.filename
    print(file_saved_in_api)
    print(file_saved_in_api.resolve())
    
    with open(file_saved_in_api, "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_saved_in_api)
    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)
    print()
    
    #api_call_msg={"INFO": f"File '{uploaded_file.filename}' saved to your profile."}
    api_call_msg={"INFO": f"File '{file_saved_in_api}' saved to your profile."}    
    print(api_call_msg)

    print(api_call_msg["INFO"])
    #File 'uploaded_file' saved to your profile.
    print()
    print(api_call_msg["INFO"].replace("uploaded_file", uploaded_file.filename))

    print("API call finished...")

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