Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,6 +6,7 @@ from fastapi.responses import JSONResponse
|
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
from PyPDF2 import PdfReader
|
| 8 |
from fastapi import Depends
|
|
|
|
| 9 |
|
| 10 |
import random
|
| 11 |
import string
|
|
@@ -14,6 +15,10 @@ import timeit
|
|
| 14 |
import datetime
|
| 15 |
import io
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def generate_random_string(length):
|
| 18 |
letters = string.ascii_lowercase
|
| 19 |
return ''.join(random.choice(letters) for i in range(length))
|
|
@@ -31,15 +36,13 @@ async def home():
|
|
| 31 |
async def upload_file(username: str, file_to_process: FileToProcess = Depends()):
|
| 32 |
uploaded_file = file_to_process.uploaded_file
|
| 33 |
|
| 34 |
-
random_string=generate_random_string(20)
|
| 35 |
-
file_path = random_string
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
file_saved_in_api = f"{file_path}/{uploaded_file.filename}"
|
| 39 |
with open(file_saved_in_api, "wb+") as file_object:
|
| 40 |
-
file_object.write(uploaded_file.file.read())
|
| 41 |
|
| 42 |
-
# 下面是你要处理的代码
|
| 43 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 44 |
#separator = "\n",
|
| 45 |
chunk_size = 500,
|
|
|
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
from PyPDF2 import PdfReader
|
| 8 |
from fastapi import Depends
|
| 9 |
+
在FastAPI中,Depends()函数用于声明依赖项
|
| 10 |
|
| 11 |
import random
|
| 12 |
import string
|
|
|
|
| 15 |
import datetime
|
| 16 |
import io
|
| 17 |
|
| 18 |
+
import os
|
| 19 |
+
from dotenv import load_dotenv
|
| 20 |
+
load_dotenv()
|
| 21 |
+
|
| 22 |
def generate_random_string(length):
|
| 23 |
letters = string.ascii_lowercase
|
| 24 |
return ''.join(random.choice(letters) for i in range(length))
|
|
|
|
| 36 |
async def upload_file(username: str, file_to_process: FileToProcess = Depends()):
|
| 37 |
uploaded_file = file_to_process.uploaded_file
|
| 38 |
|
| 39 |
+
random_string = generate_random_string(20)
|
| 40 |
+
file_path = Path.cwd() / random_string
|
| 41 |
+
file_path.mkdir(parents=True, exist_ok=True)
|
| 42 |
+
file_saved_in_api = file_path / uploaded_file.filename
|
|
|
|
| 43 |
with open(file_saved_in_api, "wb+") as file_object:
|
| 44 |
+
file_object.write(uploaded_file.file.read())
|
| 45 |
|
|
|
|
| 46 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 47 |
#separator = "\n",
|
| 48 |
chunk_size = 500,
|