Spaces:
Sleeping
Sleeping
APP
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ 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)
|
|
@@ -113,22 +114,26 @@ 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 |
@app.post("/translate/")
|
| 117 |
-
async def translate_text(
|
| 118 |
-
if not text.strip():
|
| 119 |
return JSONResponse(content={"error": "Texte vide"}, status_code=400)
|
| 120 |
|
| 121 |
if translator is None:
|
| 122 |
return JSONResponse(content={"error": "Modèle de traduction non disponible"}, status_code=500)
|
| 123 |
|
| 124 |
try:
|
| 125 |
-
translated_text = translator(text, src_lang=source_lang, tgt_lang=target_lang)[0]["translation_text"]
|
| 126 |
return JSONResponse(content={"translated_text": translated_text})
|
| 127 |
except Exception as e:
|
| 128 |
logging.error(f"❌ Erreur de traduction : {e}")
|
| 129 |
return JSONResponse(content={"error": "Échec de la traduction"}, status_code=500)
|
| 130 |
|
| 131 |
-
|
| 132 |
# ✅ Déplace ici le montage des fichiers statiques
|
| 133 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 134 |
|
|
|
|
| 10 |
from PyPDF2 import PdfReader
|
| 11 |
import docx
|
| 12 |
from PIL import Image # Pour ouvrir les images avant analyse
|
| 13 |
+
from pydantic import BaseModel
|
| 14 |
|
| 15 |
# Configuration du logging
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 114 |
logging.error(f"❌ Erreur interprétation image : {e}")
|
| 115 |
return JSONResponse(content={"error": "Échec de l'analyse de l'image"}, status_code=400)
|
| 116 |
|
| 117 |
+
class TranslationRequest(BaseModel):
|
| 118 |
+
text: str
|
| 119 |
+
source_lang: str
|
| 120 |
+
target_lang: str
|
| 121 |
+
|
| 122 |
@app.post("/translate/")
|
| 123 |
+
async def translate_text(request: TranslationRequest):
|
| 124 |
+
if not request.text.strip():
|
| 125 |
return JSONResponse(content={"error": "Texte vide"}, status_code=400)
|
| 126 |
|
| 127 |
if translator is None:
|
| 128 |
return JSONResponse(content={"error": "Modèle de traduction non disponible"}, status_code=500)
|
| 129 |
|
| 130 |
try:
|
| 131 |
+
translated_text = translator(request.text, src_lang=request.source_lang, tgt_lang=request.target_lang)[0]["translation_text"]
|
| 132 |
return JSONResponse(content={"translated_text": translated_text})
|
| 133 |
except Exception as e:
|
| 134 |
logging.error(f"❌ Erreur de traduction : {e}")
|
| 135 |
return JSONResponse(content={"error": "Échec de la traduction"}, status_code=500)
|
| 136 |
|
|
|
|
| 137 |
# ✅ Déplace ici le montage des fichiers statiques
|
| 138 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 139 |
|