Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -113,57 +113,25 @@ async def interpret_image(file: UploadFile = File(...)):
|
|
| 113 |
logging.error(f"❌ Erreur interprétation image : {e}")
|
| 114 |
return JSONResponse(content={"error": "Échec de l'analyse de l'image"}, status_code=400)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
@app.post("/translate/")
|
| 121 |
-
async def
|
| 122 |
-
if
|
| 123 |
-
return JSONResponse(content={"error": "
|
| 124 |
-
|
| 125 |
-
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 126 |
-
|
| 127 |
-
with open(file_path, "wb") as buffer:
|
| 128 |
-
shutil.copyfileobj(file.file, buffer)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
# 📄 Gestion des fichiers TXT
|
| 133 |
-
if file.filename.endswith(".txt"):
|
| 134 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
| 135 |
-
text = f.read()
|
| 136 |
-
|
| 137 |
-
# 📄 Gestion des fichiers PDF
|
| 138 |
-
elif file.filename.endswith(".pdf"):
|
| 139 |
-
try:
|
| 140 |
-
reader = PdfReader(file_path)
|
| 141 |
-
text = "\n".join([page.extract_text() or "" for page in reader.pages]).strip()
|
| 142 |
-
except Exception as e:
|
| 143 |
-
logging.error(f"❌ Erreur lecture PDF : {e}")
|
| 144 |
-
return JSONResponse(content={"error": "Impossible de lire le PDF"}, status_code=400)
|
| 145 |
-
|
| 146 |
-
# 📄 Gestion des fichiers DOCX
|
| 147 |
-
elif file.filename.endswith(".docx"):
|
| 148 |
-
try:
|
| 149 |
-
doc = docx.Document(file_path)
|
| 150 |
-
text = "\n".join([para.text for para in doc.paragraphs]).strip()
|
| 151 |
-
except Exception as e:
|
| 152 |
-
logging.error(f"❌ Erreur lecture DOCX : {e}")
|
| 153 |
-
return JSONResponse(content={"error": "Impossible de lire le fichier DOCX"}, status_code=400)
|
| 154 |
-
|
| 155 |
-
else:
|
| 156 |
-
return JSONResponse(content={"error": "Format non supporté"}, status_code=400)
|
| 157 |
-
|
| 158 |
-
if not text:
|
| 159 |
-
return JSONResponse(content={"error": "Fichier vide ou non lisible"}, status_code=400)
|
| 160 |
|
| 161 |
try:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
return {"translation": translation}
|
| 165 |
except Exception as e:
|
| 166 |
-
|
|
|
|
| 167 |
|
| 168 |
# ✅ Déplace ici le montage des fichiers statiques
|
| 169 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
@@ -171,4 +139,4 @@ app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
| 171 |
# Redirection vers index.html
|
| 172 |
@app.get("/")
|
| 173 |
async def root():
|
| 174 |
-
return RedirectResponse(url="/index.html")
|
|
|
|
| 113 |
logging.error(f"❌ Erreur interprétation image : {e}")
|
| 114 |
return JSONResponse(content={"error": "Échec de l'analyse de l'image"}, status_code=400)
|
| 115 |
|
| 116 |
+
class TranslationRequest(BaseModel):
|
| 117 |
+
text: str
|
| 118 |
+
source_lang: str
|
| 119 |
+
target_lang: str
|
| 120 |
|
| 121 |
@app.post("/translate/")
|
| 122 |
+
async def translate_text(request: TranslationRequest):
|
| 123 |
+
if not request.text.strip():
|
| 124 |
+
return JSONResponse(content={"error": "Texte vide"}, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
if translator is None:
|
| 127 |
+
return JSONResponse(content={"error": "Modèle de traduction non disponible"}, status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
try:
|
| 130 |
+
translated_text = translator(request.text, src_lang=request.source_lang, tgt_lang=request.target_lang)[0]["translation_text"]
|
| 131 |
+
return JSONResponse(content={"translated_text": translated_text})
|
|
|
|
| 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")
|
|
|
|
| 139 |
# Redirection vers index.html
|
| 140 |
@app.get("/")
|
| 141 |
async def root():
|
| 142 |
+
return RedirectResponse(url="/index.html")
|