-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
72 lines (62 loc) · 2.65 KB
/
routes.py
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
from fastapi import APIRouter, UploadFile, File, Body, HTTPException, Path
from services import PDFService
import os
from datetime import datetime
router = APIRouter()
pdf_service = PDFService()
@router.post("/upload/pdf/")
async def upload_pdf(pdf_file: UploadFile = File(...)):
""" Realiza o upload do PDF """
return await pdf_service.upload_pdf(pdf_file)
@router.get("/split/{pdf_name}")
async def split_pdf(pdf_name: str):
""" Realiza o split e vetorização do PDF """
UPLOAD_DIR = "uploads"
file_path = os.path.join(UPLOAD_DIR, pdf_name)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="Arquivo PDF não encontrado")
try:
await pdf_service.split_and_vectorize_pdf(file_path)
return {"message": "Vetor salvo com sucesso."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/list-pdfs")
async def list_pdfs():
""" Lista todos os PDF salvos em disco """
UPLOAD_DIR = "uploads"
try:
pdf_files_info = []
for f in os.listdir(UPLOAD_DIR):
if f.endswith('.pdf'):
file_path = os.path.join(UPLOAD_DIR, f)
modification_time = os.path.getmtime(file_path)
modification_date = datetime.fromtimestamp(modification_time)
formatted_date = modification_date.strftime('%Y-%m-%d %H:%M:%S')
pdf_files_info.append({"title": f, "modification_date": formatted_date})
return {"pdfs": pdf_files_info}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete("/delete-pdf/{pdf_name}")
async def delete_pdf(pdf_name: str = Path(...)):
""" Exclui um arquivo PDF da pasta uploads """
UPLOAD_DIR = "uploads"
file_path = os.path.join(UPLOAD_DIR, pdf_name)
try:
if os.path.exists(file_path) and os.path.isfile(file_path):
os.remove(file_path)
return {"detail": f"O arquivo {pdf_name} foi excluído com sucesso."}
else:
raise HTTPException(status_code=404, detail=f"O arquivo {pdf_name} não foi encontrado.")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/chat/")
async def chat(question: dict = Body(...)):
""" Interage com o PDF através de uma pergunta. """
try:
question = question.get("question")
if not question:
raise ValueError("A pergunta não foi fornecida")
answer = await pdf_service.ask_question(question)
return {"answer": answer}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))