Spaces:
Sleeping
Sleeping
ajout
Browse files
app.py
CHANGED
|
@@ -119,20 +119,46 @@ class TranslationRequest(BaseModel):
|
|
| 119 |
target_lang: str
|
| 120 |
|
| 121 |
@app.post("/translate/")
|
| 122 |
-
async def
|
| 123 |
-
|
| 124 |
-
return JSONResponse(content={"error": "Texte vide"}, status_code=400)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
try:
|
| 130 |
-
translated_text = translator(
|
| 131 |
-
return JSONResponse(content={"
|
| 132 |
except Exception as e:
|
| 133 |
-
logging.error(f"❌ Erreur de traduction : {e}")
|
| 134 |
return JSONResponse(content={"error": "Échec de la traduction"}, status_code=500)
|
| 135 |
|
|
|
|
| 136 |
# ✅ Déplace ici le montage des fichiers statiques
|
| 137 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 138 |
|
|
|
|
| 119 |
target_lang: str
|
| 120 |
|
| 121 |
@app.post("/translate/")
|
| 122 |
+
async def translate_document(file: UploadFile = File(...), target_lang: str = "fr"):
|
| 123 |
+
logging.info(f"📂 Traduction demandée - Fichier : {file.filename} en {target_lang}")
|
|
|
|
| 124 |
|
| 125 |
+
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 126 |
+
with open(file_path, "wb") as buffer:
|
| 127 |
+
shutil.copyfileobj(file.file, buffer)
|
| 128 |
+
|
| 129 |
+
text = ""
|
| 130 |
+
if file.filename.endswith(".txt"):
|
| 131 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 132 |
+
text = f.read()
|
| 133 |
+
elif file.filename.endswith(".pdf"):
|
| 134 |
+
try:
|
| 135 |
+
reader = PdfReader(file_path)
|
| 136 |
+
text = "\n".join([page.extract_text() or "" for page in reader.pages]).strip()
|
| 137 |
+
except Exception as e:
|
| 138 |
+
logging.error(f"❌ Erreur lecture PDF : {e}")
|
| 139 |
+
return JSONResponse(content={"error": "Impossible de lire le PDF"}, status_code=400)
|
| 140 |
+
elif file.filename.endswith(".docx"):
|
| 141 |
+
try:
|
| 142 |
+
doc = docx.Document(file_path)
|
| 143 |
+
text = "\n".join([para.text for para in doc.paragraphs]).strip()
|
| 144 |
+
except Exception as e:
|
| 145 |
+
logging.error(f"❌ Erreur lecture DOCX : {e}")
|
| 146 |
+
return JSONResponse(content={"error": "Impossible de lire le fichier DOCX"}, status_code=400)
|
| 147 |
+
else:
|
| 148 |
+
return JSONResponse(content={"error": "Format de fichier non supporté"}, status_code=400)
|
| 149 |
+
|
| 150 |
+
if not text:
|
| 151 |
+
logging.error("❌ Le fichier ne contient pas de texte lisible")
|
| 152 |
+
return JSONResponse(content={"error": "Le fichier est vide ou non lisible"}, status_code=400)
|
| 153 |
|
| 154 |
try:
|
| 155 |
+
translated_text = translator(text, src_lang="auto", tgt_lang=target_lang)[0]["translation_text"]
|
| 156 |
+
return JSONResponse(content={"translation": translated_text})
|
| 157 |
except Exception as e:
|
| 158 |
+
logging.error(f"❌ Erreur lors de la traduction : {e}")
|
| 159 |
return JSONResponse(content={"error": "Échec de la traduction"}, status_code=500)
|
| 160 |
|
| 161 |
+
|
| 162 |
# ✅ Déplace ici le montage des fichiers statiques
|
| 163 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 164 |
|