Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Request
|
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 |
+
from fastapi import Depends
|
9 |
+
#在FastAPI中,Depends()函数用于声明依赖项
|
10 |
+
|
11 |
+
import random
|
12 |
+
import string
|
13 |
+
import sys
|
14 |
+
import timeit
|
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))
|
25 |
+
|
26 |
+
app = FastAPI()
|
27 |
+
|
28 |
+
class FileToProcess(BaseModel):
|
29 |
+
uploaded_file: UploadFile = File(...)
|
30 |
+
|
31 |
+
@app.get("/")
|
32 |
+
async def home():
|
33 |
+
return "API Working!"
|
34 |
+
|
35 |
+
@app.post("/fastapi_file_upload_process")
|
36 |
+
#async def upload_file(username: str, file_to_process: FileToProcess = Depends()):
|
37 |
+
async def pdf_file_qa_process(username: str, request: Request, file_to_process: FileToProcess = Depends()):
|
38 |
+
uploaded_file = file_to_process.uploaded_file
|
39 |
+
print("File received:"+uploaded_file.filename)
|
40 |
+
|
41 |
+
username = request.query_params.get("username")
|
42 |
+
filename = request.query_params.get("filename")
|
43 |
+
print(username)
|
44 |
+
print(filename)
|
45 |
+
|
46 |
+
random_string = generate_random_string(20)
|
47 |
+
file_path = Path.cwd() / random_string
|
48 |
+
file_path.mkdir(parents=True, exist_ok=True)
|
49 |
+
file_saved_in_api = file_path / uploaded_file.filename
|
50 |
+
print(file_saved_in_api)
|
51 |
+
print(file_saved_in_api.resolve())
|
52 |
+
|
53 |
+
with open(file_saved_in_api, "wb+") as file_object:
|
54 |
+
file_object.write(uploaded_file.file.read())
|
55 |
+
|
56 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
57 |
+
#separator = "\n",
|
58 |
+
chunk_size = 500,
|
59 |
+
chunk_overlap = 100, #striding over the text
|
60 |
+
length_function = len,
|
61 |
+
)
|
62 |
+
|
63 |
+
doc_reader = PdfReader(file_saved_in_api)
|
64 |
+
raw_text = ''
|
65 |
+
for i, page in enumerate(doc_reader.pages):
|
66 |
+
text = page.extract_text()
|
67 |
+
if text:
|
68 |
+
raw_text += text
|
69 |
+
temp_texts = text_splitter.split_text(raw_text)
|
70 |
+
|
71 |
+
print(temp_texts)
|
72 |
+
print()
|
73 |
+
|
74 |
+
api_call_msg={"INFO": f"File '{file_saved_in_api}' saved to your profile."}
|
75 |
+
print(api_call_msg)
|
76 |
+
|
77 |
+
print(api_call_msg["INFO"])
|
78 |
+
print()
|
79 |
+
print(api_call_msg["INFO"].replace("uploaded_file", uploaded_file.filename))
|
80 |
+
|
81 |
+
print("API call finished...")
|
82 |
+
|
83 |
+
#return {"INFO": f"File '{uploaded_file.filename}' saved to your profile."}
|
84 |
+
return api_call_msg
|