Update admin.py
Browse files
admin.py
CHANGED
@@ -1,130 +1,130 @@
|
|
1 |
-
from fastapi import APIRouter, File, UploadFile, HTTPException, Depends
|
2 |
-
from bson.objectid import ObjectId
|
3 |
-
import os
|
4 |
-
import PyPDF2
|
5 |
-
from io import BytesIO
|
6 |
-
from datetime import datetime
|
7 |
-
|
8 |
-
from auth import get_admin_user
|
9 |
-
from database import get_db
|
10 |
-
from config import SAVE_FOLDER
|
11 |
-
from chat import embedding_model
|
12 |
-
|
13 |
-
router = APIRouter(prefix="/api/admin", tags=["Administration"])
|
14 |
-
db=get_db()
|
15 |
-
|
16 |
-
@router.post("/knowledge/upload")
|
17 |
-
async def upload_pdf(
|
18 |
-
file: UploadFile = File(...),
|
19 |
-
title: str = None,
|
20 |
-
tags: str = None,
|
21 |
-
current_user: dict = Depends(get_admin_user)
|
22 |
-
):
|
23 |
-
try:
|
24 |
-
if not file.filename.endswith('.pdf'):
|
25 |
-
raise HTTPException(status_code=400, detail="Le fichier doit être un PDF")
|
26 |
-
|
27 |
-
contents = await file.read()
|
28 |
-
pdf_file = BytesIO(contents)
|
29 |
-
|
30 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
31 |
-
text_content = ""
|
32 |
-
for page_num in range(len(pdf_reader.pages)):
|
33 |
-
text_content += pdf_reader.pages[page_num].extract_text() + "\n"
|
34 |
-
|
35 |
-
embedding = None
|
36 |
-
if embedding_model:
|
37 |
-
try:
|
38 |
-
max_length = 5000
|
39 |
-
truncated_text = text_content[:max_length]
|
40 |
-
embedding = embedding_model.
|
41 |
-
except Exception as e:
|
42 |
-
print(f"Erreur
|
43 |
-
|
44 |
-
doc_id = ObjectId()
|
45 |
-
|
46 |
-
pdf_path = f"files/{str(doc_id)}.pdf"
|
47 |
-
os.makedirs("files", exist_ok=True)
|
48 |
-
with open(pdf_path, "wb") as f:
|
49 |
-
pdf_file.seek(0)
|
50 |
-
f.write(contents)
|
51 |
-
|
52 |
-
document = {
|
53 |
-
"_id": doc_id,
|
54 |
-
"text": text_content,
|
55 |
-
"embedding": embedding,
|
56 |
-
"title": title or file.filename,
|
57 |
-
"tags": tags.split(",") if tags else [],
|
58 |
-
"uploaded_by": str(current_user["_id"]),
|
59 |
-
"upload_date": datetime.utcnow()
|
60 |
-
}
|
61 |
-
|
62 |
-
print(f"Tentative d'insertion du document avec ID: {doc_id}")
|
63 |
-
result = db.connaissances.insert_one(document)
|
64 |
-
print(f"Document inséré avec ID: {result.inserted_id}")
|
65 |
-
|
66 |
-
verification = db.connaissances.find_one({"_id": doc_id})
|
67 |
-
if verification:
|
68 |
-
print(f"Document vérifié et trouvé dans la base de données")
|
69 |
-
return {"success": True, "document_id": str(doc_id)}
|
70 |
-
else:
|
71 |
-
print(f"ERREUR: Document non trouvé après insertion")
|
72 |
-
return {"success": False, "error": "Document non trouvé après insertion"}
|
73 |
-
|
74 |
-
except Exception as e:
|
75 |
-
import traceback
|
76 |
-
print(f"Erreur lors de l'upload du PDF: {traceback.format_exc()}")
|
77 |
-
raise HTTPException(status_code=500, detail=f"Erreur: {str(e)}")
|
78 |
-
|
79 |
-
@router.get("/knowledge")
|
80 |
-
async def list_documents(current_user: dict = Depends(get_admin_user)):
|
81 |
-
try:
|
82 |
-
documents = list(db.connaissances.find().sort("upload_date", -1))
|
83 |
-
|
84 |
-
result = []
|
85 |
-
for doc in documents:
|
86 |
-
doc_safe = {
|
87 |
-
"id": str(doc["_id"]),
|
88 |
-
"title": doc.get("title", "Sans titre"),
|
89 |
-
"tags": doc.get("tags", []),
|
90 |
-
"date": doc.get("upload_date").isoformat() if "upload_date" in doc else None,
|
91 |
-
"text_preview": doc.get("text", "")[:100] + "..." if len(doc.get("text", "")) > 100 else doc.get("text", "")
|
92 |
-
}
|
93 |
-
result.append(doc_safe)
|
94 |
-
|
95 |
-
return {"documents": result}
|
96 |
-
except Exception as e:
|
97 |
-
print(f"Erreur lors de la liste des documents: {str(e)}")
|
98 |
-
raise HTTPException(status_code=500, detail=f"Erreur: {str(e)}")
|
99 |
-
|
100 |
-
@router.delete("/knowledge/{document_id}")
|
101 |
-
async def delete_document(document_id: str, current_user: dict = Depends(get_admin_user)):
|
102 |
-
try:
|
103 |
-
try:
|
104 |
-
doc_id = ObjectId(document_id)
|
105 |
-
except Exception:
|
106 |
-
raise HTTPException(status_code=400, detail="ID de document invalide")
|
107 |
-
|
108 |
-
document = db.connaissances.find_one({"_id": doc_id})
|
109 |
-
if not document:
|
110 |
-
raise HTTPException(status_code=404, detail="Document non trouvé")
|
111 |
-
|
112 |
-
result = db.connaissances.delete_one({"_id": doc_id})
|
113 |
-
|
114 |
-
if result.deleted_count == 0:
|
115 |
-
raise HTTPException(status_code=500, detail="Échec de la suppression du document")
|
116 |
-
|
117 |
-
pdf_path = f"files/{document_id}.pdf"
|
118 |
-
if os.path.exists(pdf_path):
|
119 |
-
try:
|
120 |
-
os.remove(pdf_path)
|
121 |
-
print(f"Fichier supprimé: {pdf_path}")
|
122 |
-
except Exception as e:
|
123 |
-
print(f"Erreur lors de la suppression du fichier: {str(e)}")
|
124 |
-
|
125 |
-
return {"success": True, "message": "Document supprimé avec succès"}
|
126 |
-
|
127 |
-
except HTTPException as he:
|
128 |
-
raise he
|
129 |
-
except Exception as e:
|
130 |
raise HTTPException(status_code=500, detail=f"Erreur lors de la suppression: {str(e)}")
|
|
|
1 |
+
from fastapi import APIRouter, File, UploadFile, HTTPException, Depends
|
2 |
+
from bson.objectid import ObjectId
|
3 |
+
import os
|
4 |
+
import PyPDF2
|
5 |
+
from io import BytesIO
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
from auth import get_admin_user
|
9 |
+
from database import get_db
|
10 |
+
from config import SAVE_FOLDER
|
11 |
+
from chat import embedding_model
|
12 |
+
|
13 |
+
router = APIRouter(prefix="/api/admin", tags=["Administration"])
|
14 |
+
db=get_db()
|
15 |
+
|
16 |
+
@router.post("/knowledge/upload")
|
17 |
+
async def upload_pdf(
|
18 |
+
file: UploadFile = File(...),
|
19 |
+
title: str = None,
|
20 |
+
tags: str = None,
|
21 |
+
current_user: dict = Depends(get_admin_user)
|
22 |
+
):
|
23 |
+
try:
|
24 |
+
if not file.filename.endswith('.pdf'):
|
25 |
+
raise HTTPException(status_code=400, detail="Le fichier doit être un PDF")
|
26 |
+
|
27 |
+
contents = await file.read()
|
28 |
+
pdf_file = BytesIO(contents)
|
29 |
+
|
30 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
31 |
+
text_content = ""
|
32 |
+
for page_num in range(len(pdf_reader.pages)):
|
33 |
+
text_content += pdf_reader.pages[page_num].extract_text() + "\n"
|
34 |
+
|
35 |
+
embedding = None
|
36 |
+
if embedding_model:
|
37 |
+
try:
|
38 |
+
max_length = 5000
|
39 |
+
truncated_text = text_content[:max_length]
|
40 |
+
embedding = embedding_model.embed_query(truncated_text).tolist()
|
41 |
+
except Exception as e:
|
42 |
+
print(f"Erreur: {str(e)}")
|
43 |
+
|
44 |
+
doc_id = ObjectId()
|
45 |
+
|
46 |
+
pdf_path = f"files/{str(doc_id)}.pdf"
|
47 |
+
os.makedirs("files", exist_ok=True)
|
48 |
+
with open(pdf_path, "wb") as f:
|
49 |
+
pdf_file.seek(0)
|
50 |
+
f.write(contents)
|
51 |
+
|
52 |
+
document = {
|
53 |
+
"_id": doc_id,
|
54 |
+
"text": text_content,
|
55 |
+
"embedding": embedding,
|
56 |
+
"title": title or file.filename,
|
57 |
+
"tags": tags.split(",") if tags else [],
|
58 |
+
"uploaded_by": str(current_user["_id"]),
|
59 |
+
"upload_date": datetime.utcnow()
|
60 |
+
}
|
61 |
+
|
62 |
+
print(f"Tentative d'insertion du document avec ID: {doc_id}")
|
63 |
+
result = db.connaissances.insert_one(document)
|
64 |
+
print(f"Document inséré avec ID: {result.inserted_id}")
|
65 |
+
|
66 |
+
verification = db.connaissances.find_one({"_id": doc_id})
|
67 |
+
if verification:
|
68 |
+
print(f"Document vérifié et trouvé dans la base de données")
|
69 |
+
return {"success": True, "document_id": str(doc_id)}
|
70 |
+
else:
|
71 |
+
print(f"ERREUR: Document non trouvé après insertion")
|
72 |
+
return {"success": False, "error": "Document non trouvé après insertion"}
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
import traceback
|
76 |
+
print(f"Erreur lors de l'upload du PDF: {traceback.format_exc()}")
|
77 |
+
raise HTTPException(status_code=500, detail=f"Erreur: {str(e)}")
|
78 |
+
|
79 |
+
@router.get("/knowledge")
|
80 |
+
async def list_documents(current_user: dict = Depends(get_admin_user)):
|
81 |
+
try:
|
82 |
+
documents = list(db.connaissances.find().sort("upload_date", -1))
|
83 |
+
|
84 |
+
result = []
|
85 |
+
for doc in documents:
|
86 |
+
doc_safe = {
|
87 |
+
"id": str(doc["_id"]),
|
88 |
+
"title": doc.get("title", "Sans titre"),
|
89 |
+
"tags": doc.get("tags", []),
|
90 |
+
"date": doc.get("upload_date").isoformat() if "upload_date" in doc else None,
|
91 |
+
"text_preview": doc.get("text", "")[:100] + "..." if len(doc.get("text", "")) > 100 else doc.get("text", "")
|
92 |
+
}
|
93 |
+
result.append(doc_safe)
|
94 |
+
|
95 |
+
return {"documents": result}
|
96 |
+
except Exception as e:
|
97 |
+
print(f"Erreur lors de la liste des documents: {str(e)}")
|
98 |
+
raise HTTPException(status_code=500, detail=f"Erreur: {str(e)}")
|
99 |
+
|
100 |
+
@router.delete("/knowledge/{document_id}")
|
101 |
+
async def delete_document(document_id: str, current_user: dict = Depends(get_admin_user)):
|
102 |
+
try:
|
103 |
+
try:
|
104 |
+
doc_id = ObjectId(document_id)
|
105 |
+
except Exception:
|
106 |
+
raise HTTPException(status_code=400, detail="ID de document invalide")
|
107 |
+
|
108 |
+
document = db.connaissances.find_one({"_id": doc_id})
|
109 |
+
if not document:
|
110 |
+
raise HTTPException(status_code=404, detail="Document non trouvé")
|
111 |
+
|
112 |
+
result = db.connaissances.delete_one({"_id": doc_id})
|
113 |
+
|
114 |
+
if result.deleted_count == 0:
|
115 |
+
raise HTTPException(status_code=500, detail="Échec de la suppression du document")
|
116 |
+
|
117 |
+
pdf_path = f"files/{document_id}.pdf"
|
118 |
+
if os.path.exists(pdf_path):
|
119 |
+
try:
|
120 |
+
os.remove(pdf_path)
|
121 |
+
print(f"Fichier supprimé: {pdf_path}")
|
122 |
+
except Exception as e:
|
123 |
+
print(f"Erreur lors de la suppression du fichier: {str(e)}")
|
124 |
+
|
125 |
+
return {"success": True, "message": "Document supprimé avec succès"}
|
126 |
+
|
127 |
+
except HTTPException as he:
|
128 |
+
raise he
|
129 |
+
except Exception as e:
|
130 |
raise HTTPException(status_code=500, detail=f"Erreur lors de la suppression: {str(e)}")
|