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 #在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 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 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 finished...") #return {"INFO": f"File '{uploaded_file.filename}' saved to your profile."} return api_call_msg