Spaces:
Sleeping
Sleeping
APPPP
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from fastapi.responses import JSONResponse, RedirectResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from transformers import pipeline
|
| 6 |
import shutil
|
| 7 |
import os
|
| 8 |
import logging
|
|
|
|
| 9 |
from PyPDF2 import PdfReader
|
| 10 |
import docx
|
| 11 |
-
from PIL import Image
|
| 12 |
-
import fitz # PyMuPDF pour l'extraction de texte PDF
|
| 13 |
|
| 14 |
# Configuration du logging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -43,16 +43,11 @@ except Exception as e:
|
|
| 43 |
image_captioning = None
|
| 44 |
logging.error(f"❌ Erreur chargement modèle image : {e}")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
translator = pipeline("translation", model="facebook/m2m100_418M")
|
| 48 |
-
logging.info("✅ Modèle de traduction chargé avec succès !")
|
| 49 |
-
except Exception as e:
|
| 50 |
-
translator = None
|
| 51 |
-
logging.error(f"❌ Erreur chargement modèle traduction : {e}")
|
| 52 |
-
|
| 53 |
@app.post("/summarize/")
|
| 54 |
async def summarize_document(file: UploadFile = File(...)):
|
| 55 |
logging.info(f"📂 Requête reçue - Fichier : {file.filename}")
|
|
|
|
| 56 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 57 |
with open(file_path, "wb") as buffer:
|
| 58 |
shutil.copyfileobj(file.file, buffer)
|
|
@@ -66,74 +61,54 @@ async def summarize_document(file: UploadFile = File(...)):
|
|
| 66 |
reader = PdfReader(file_path)
|
| 67 |
text = "\n".join([page.extract_text() or "" for page in reader.pages]).strip()
|
| 68 |
except Exception as e:
|
|
|
|
| 69 |
return JSONResponse(content={"error": "Impossible de lire le PDF"}, status_code=400)
|
| 70 |
elif file.filename.endswith(".docx"):
|
| 71 |
try:
|
| 72 |
doc = docx.Document(file_path)
|
| 73 |
text = "\n".join([para.text for para in doc.paragraphs]).strip()
|
| 74 |
except Exception as e:
|
|
|
|
| 75 |
return JSONResponse(content={"error": "Impossible de lire le fichier DOCX"}, status_code=400)
|
| 76 |
else:
|
| 77 |
return JSONResponse(content={"error": "Format de fichier non supporté"}, status_code=400)
|
| 78 |
|
| 79 |
if not text:
|
|
|
|
| 80 |
return JSONResponse(content={"error": "Le fichier est vide ou non lisible"}, status_code=400)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 84 |
return JSONResponse(content={"summary": summary})
|
| 85 |
except Exception as e:
|
|
|
|
| 86 |
return JSONResponse(content={"error": "Échec du résumé. Texte trop long ou format invalide."}, status_code=500)
|
| 87 |
|
|
|
|
| 88 |
@app.post("/interpret/")
|
| 89 |
async def interpret_image(file: UploadFile = File(...)):
|
| 90 |
logging.info(f"📂 Requête reçue - Image : {file.filename}")
|
|
|
|
| 91 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 92 |
with open(file_path, "wb") as buffer:
|
| 93 |
shutil.copyfileobj(file.file, buffer)
|
| 94 |
-
|
| 95 |
try:
|
| 96 |
-
with Image.open(file_path) as img:
|
| 97 |
caption = image_captioning(img)[0]["generated_text"]
|
| 98 |
return JSONResponse(content={"caption": caption})
|
| 99 |
except Exception as e:
|
|
|
|
| 100 |
return JSONResponse(content={"error": "Échec de l'analyse de l'image"}, status_code=400)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
async def translate_document(file: UploadFile = File(...), target_lang: str = "fr"):
|
| 104 |
-
logging.info(f"📂 Requête reçue - Traduction : {file.filename}")
|
| 105 |
-
|
| 106 |
-
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 107 |
-
with open(file_path, "wb") as buffer:
|
| 108 |
-
shutil.copyfileobj(file.file, buffer)
|
| 109 |
-
|
| 110 |
-
try:
|
| 111 |
-
text = ""
|
| 112 |
-
if file.filename.endswith(".pdf"):
|
| 113 |
-
doc = fitz.open(file_path) # Correction ici
|
| 114 |
-
text = "\n".join([page.get_text("text") for page in doc]) # Extraction correcte
|
| 115 |
-
|
| 116 |
-
elif file.filename.endswith(".docx"):
|
| 117 |
-
doc = docx.Document(file_path) # Correction ici
|
| 118 |
-
text = "\n".join([para.text for para in doc.paragraphs])
|
| 119 |
-
|
| 120 |
-
else:
|
| 121 |
-
return JSONResponse(status_code=400, content={"error": "Format non supporté"})
|
| 122 |
-
|
| 123 |
-
if not text.strip():
|
| 124 |
-
return JSONResponse(status_code=400, content={"error": "Le document est vide ou non lisible"})
|
| 125 |
-
|
| 126 |
-
translated_text = translator(text, src_lang="en", target_lang=target_lang) # Correction ici
|
| 127 |
-
|
| 128 |
-
return {"translated_text": translated_text[0]["translation_text"]}
|
| 129 |
-
|
| 130 |
-
except Exception as e:
|
| 131 |
-
logging.error(f"❌ Erreur lors de la traduction : {e}")
|
| 132 |
-
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 133 |
-
|
| 134 |
-
|
| 135 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 136 |
|
|
|
|
| 137 |
@app.get("/")
|
| 138 |
async def root():
|
| 139 |
-
return RedirectResponse(url="/index.html")
|
|
|
|
| 1 |
+
|
| 2 |
from fastapi import FastAPI, File, UploadFile
|
| 3 |
from fastapi.responses import JSONResponse, RedirectResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
import shutil
|
| 7 |
import os
|
| 8 |
import logging
|
| 9 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from PyPDF2 import PdfReader
|
| 11 |
import docx
|
| 12 |
+
from PIL import Image # Pour ouvrir les images avant analyse
|
|
|
|
| 13 |
|
| 14 |
# Configuration du logging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 43 |
image_captioning = None
|
| 44 |
logging.error(f"❌ Erreur chargement modèle image : {e}")
|
| 45 |
|
| 46 |
+
# ✅ Déclare les routes AVANT le montage des fichiers statiques
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
@app.post("/summarize/")
|
| 48 |
async def summarize_document(file: UploadFile = File(...)):
|
| 49 |
logging.info(f"📂 Requête reçue - Fichier : {file.filename}")
|
| 50 |
+
|
| 51 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 52 |
with open(file_path, "wb") as buffer:
|
| 53 |
shutil.copyfileobj(file.file, buffer)
|
|
|
|
| 61 |
reader = PdfReader(file_path)
|
| 62 |
text = "\n".join([page.extract_text() or "" for page in reader.pages]).strip()
|
| 63 |
except Exception as e:
|
| 64 |
+
logging.error(f"❌ Erreur lecture PDF : {e}")
|
| 65 |
return JSONResponse(content={"error": "Impossible de lire le PDF"}, status_code=400)
|
| 66 |
elif file.filename.endswith(".docx"):
|
| 67 |
try:
|
| 68 |
doc = docx.Document(file_path)
|
| 69 |
text = "\n".join([para.text for para in doc.paragraphs]).strip()
|
| 70 |
except Exception as e:
|
| 71 |
+
logging.error(f"❌ Erreur lecture DOCX : {e}")
|
| 72 |
return JSONResponse(content={"error": "Impossible de lire le fichier DOCX"}, status_code=400)
|
| 73 |
else:
|
| 74 |
return JSONResponse(content={"error": "Format de fichier non supporté"}, status_code=400)
|
| 75 |
|
| 76 |
if not text:
|
| 77 |
+
logging.error("❌ Le fichier ne contient pas de texte lisible")
|
| 78 |
return JSONResponse(content={"error": "Le fichier est vide ou non lisible"}, status_code=400)
|
| 79 |
|
| 80 |
+
# Tronquer le texte pour éviter l'erreur "IndexError: index out of range"
|
| 81 |
+
max_input_length = 1024 # Limite du modèle
|
| 82 |
+
text = text[:max_input_length] # Tronquer le texte s'il est trop long
|
| 83 |
+
|
| 84 |
try:
|
| 85 |
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 86 |
return JSONResponse(content={"summary": summary})
|
| 87 |
except Exception as e:
|
| 88 |
+
logging.error(f"❌ Erreur lors du résumé : {e}")
|
| 89 |
return JSONResponse(content={"error": "Échec du résumé. Texte trop long ou format invalide."}, status_code=500)
|
| 90 |
|
| 91 |
+
|
| 92 |
@app.post("/interpret/")
|
| 93 |
async def interpret_image(file: UploadFile = File(...)):
|
| 94 |
logging.info(f"📂 Requête reçue - Image : {file.filename}")
|
| 95 |
+
|
| 96 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 97 |
with open(file_path, "wb") as buffer:
|
| 98 |
shutil.copyfileobj(file.file, buffer)
|
| 99 |
+
|
| 100 |
try:
|
| 101 |
+
with Image.open(file_path) as img: # Charger l'image correctement
|
| 102 |
caption = image_captioning(img)[0]["generated_text"]
|
| 103 |
return JSONResponse(content={"caption": caption})
|
| 104 |
except Exception as e:
|
| 105 |
+
logging.error(f"❌ Erreur interprétation image : {e}")
|
| 106 |
return JSONResponse(content={"error": "Échec de l'analyse de l'image"}, status_code=400)
|
| 107 |
|
| 108 |
+
# ✅ Déplace ici le montage des fichiers statiques
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 110 |
|
| 111 |
+
# Redirection vers index.html
|
| 112 |
@app.get("/")
|
| 113 |
async def root():
|
| 114 |
+
return RedirectResponse(url="/index.html")
|