malaknihed commited on
Commit
11f418e
·
verified ·
1 Parent(s): 63821fd
Files changed (1) hide show
  1. app.py +11 -34
app.py CHANGED
@@ -115,45 +115,22 @@ async def interpret_image(file: UploadFile = File(...)):
115
 
116
 
117
  @app.post("/translate/")
118
- async def translate_document(file: UploadFile = File(...), target_lang: str = "fr"):
119
- logging.info(f"📂 Traduction demandée - Fichier : {file.filename} en {target_lang}")
 
 
 
120
 
121
- file_path = os.path.join(UPLOAD_DIR, file.filename)
122
- with open(file_path, "wb") as buffer:
123
- shutil.copyfileobj(file.file, buffer)
124
 
125
- text = ""
126
- if file.filename.endswith(".txt"):
127
- with open(file_path, "r", encoding="utf-8") as f:
128
- text = f.read()
129
- elif file.filename.endswith(".pdf"):
130
- try:
131
- reader = PdfReader(file_path)
132
- text = "\n".join([page.extract_text() or "" for page in reader.pages]).strip()
133
- except Exception as e:
134
- logging.error(f"❌ Erreur lecture PDF : {e}")
135
- return JSONResponse(content={"error": "Impossible de lire le PDF"}, status_code=400)
136
- elif file.filename.endswith(".docx"):
137
- try:
138
- doc = docx.Document(file_path)
139
- text = "\n".join([para.text for para in doc.paragraphs]).strip()
140
- except Exception as e:
141
- logging.error(f"❌ Erreur lecture DOCX : {e}")
142
- return JSONResponse(content={"error": "Impossible de lire le fichier DOCX"}, status_code=400)
143
- else:
144
- return JSONResponse(content={"error": "Format de fichier non supporté"}, status_code=400)
145
 
146
- if not text:
147
- logging.error("❌ Le fichier ne contient pas de texte lisible")
148
- return JSONResponse(content={"error": "Le fichier est vide ou non lisible"}, status_code=400)
149
 
150
- try:
151
- translated_text = translator(text, src_lang="auto", tgt_lang=target_lang)[0]["translation_text"]
152
- return JSONResponse(content={"translation": translated_text})
153
  except Exception as e:
154
- logging.error(f"❌ Erreur lors de la traduction : {e}")
155
- return JSONResponse(content={"error": "Échec de la traduction"}, status_code=500)
156
-
157
 
158
  # ✅ Déplace ici le montage des fichiers statiques
159
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
 
115
 
116
 
117
  @app.post("/translate/")
118
+ async def translate_file(file: UploadFile, target_lang: str = Form(...)):
119
+ try:
120
+ # Lire le contenu du fichier
121
+ content = await file.read()
122
+ text = content.decode("utf-8")
123
 
124
+ # Ajouter le préfixe spécial pour le modèle
125
+ formatted_text = f">>{target_lang}<< {text}"
 
126
 
127
+ # Traduction avec le modèle
128
+ translation = translator(formatted_text, max_length=512)[0]["translation_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
+ return {"translation": translation}
 
 
131
 
 
 
 
132
  except Exception as e:
133
+ return {"error": str(e)}
 
 
134
 
135
  # ✅ Déplace ici le montage des fichiers statiques
136
  app.mount("/", StaticFiles(directory="static", html=True), name="static")