Spaces:
Sleeping
Sleeping
APP
Browse files
app.py
CHANGED
|
@@ -4,8 +4,13 @@ from fastapi.staticfiles import StaticFiles
|
|
| 4 |
from transformers import pipeline
|
| 5 |
import shutil
|
| 6 |
import os
|
|
|
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -17,8 +22,6 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
# Servir les fichiers statiques (HTML, CSS, JS)
|
| 23 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 24 |
|
|
@@ -38,17 +41,37 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
| 38 |
|
| 39 |
@app.post("/summarize/")
|
| 40 |
async def summarize_document(file: UploadFile = File(...)):
|
| 41 |
-
""" Analyse et résume un document texte. """
|
|
|
|
|
|
|
| 42 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 43 |
-
|
| 44 |
-
# Sauvegarde temporaire du fichier
|
| 45 |
with open(file_path, "wb") as buffer:
|
| 46 |
shutil.copyfileobj(file.file, buffer)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 54 |
|
|
@@ -57,11 +80,16 @@ async def summarize_document(file: UploadFile = File(...)):
|
|
| 57 |
@app.post("/interpret/")
|
| 58 |
async def interpret_image(file: UploadFile = File(...)):
|
| 59 |
""" Génère une légende pour une image. """
|
| 60 |
-
|
| 61 |
|
|
|
|
| 62 |
with open(file_path, "wb") as buffer:
|
| 63 |
shutil.copyfileobj(file.file, buffer)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
return JSONResponse(content={"caption": caption})
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
import shutil
|
| 6 |
import os
|
| 7 |
+
import logging
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from PyPDF2 import PdfReader
|
| 10 |
+
import docx
|
| 11 |
|
| 12 |
+
# Configuration du logging
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
| 25 |
# Servir les fichiers statiques (HTML, CSS, JS)
|
| 26 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 27 |
|
|
|
|
| 41 |
|
| 42 |
@app.post("/summarize/")
|
| 43 |
async def summarize_document(file: UploadFile = File(...)):
|
| 44 |
+
""" Analyse et résume un document texte, PDF ou DOCX. """
|
| 45 |
+
logging.info(f"📂 Fichier reçu : {file.filename}")
|
| 46 |
+
|
| 47 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
|
|
|
| 48 |
with open(file_path, "wb") as buffer:
|
| 49 |
shutil.copyfileobj(file.file, buffer)
|
| 50 |
+
|
| 51 |
+
text = ""
|
| 52 |
+
|
| 53 |
+
if file.filename.endswith(".txt"):
|
| 54 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 55 |
+
text = f.read()
|
| 56 |
+
elif file.filename.endswith(".pdf"):
|
| 57 |
+
try:
|
| 58 |
+
reader = PdfReader(file_path)
|
| 59 |
+
text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 60 |
+
except Exception as e:
|
| 61 |
+
logging.error(f"❌ Erreur lecture PDF : {e}")
|
| 62 |
+
return JSONResponse(content={"error": "Impossible de lire le PDF"}, status_code=400)
|
| 63 |
+
elif file.filename.endswith(".docx"):
|
| 64 |
+
try:
|
| 65 |
+
doc = docx.Document(file_path)
|
| 66 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logging.error(f"❌ Erreur lecture DOCX : {e}")
|
| 69 |
+
return JSONResponse(content={"error": "Impossible de lire le fichier DOCX"}, status_code=400)
|
| 70 |
+
else:
|
| 71 |
+
return JSONResponse(content={"error": "Format de fichier non supporté"}, status_code=400)
|
| 72 |
+
|
| 73 |
+
if not text.strip():
|
| 74 |
+
return JSONResponse(content={"error": "Le fichier ne contient pas de texte lisible"}, status_code=400)
|
| 75 |
|
| 76 |
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 77 |
|
|
|
|
| 80 |
@app.post("/interpret/")
|
| 81 |
async def interpret_image(file: UploadFile = File(...)):
|
| 82 |
""" Génère une légende pour une image. """
|
| 83 |
+
logging.info(f"📂 Image reçue : {file.filename}")
|
| 84 |
|
| 85 |
+
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 86 |
with open(file_path, "wb") as buffer:
|
| 87 |
shutil.copyfileobj(file.file, buffer)
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
caption = image_captioning(file_path)[0]["generated_text"]
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logging.error(f"❌ Erreur interprétation image : {e}")
|
| 93 |
+
return JSONResponse(content={"error": "Échec de l'analyse de l'image"}, status_code=400)
|
| 94 |
+
|
| 95 |
return JSONResponse(content={"caption": caption})
|