Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines4567891011121314151617181920212223242526272873031323334353637383940414243444546474849505152535455565789101 unmodified line12131465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541516173 unmodified linesfrom fastapi.middleware.cors import CORSMiddlewarefrom transformers import pipeline, M2M100ForConditionalGeneration, M2M100Tokenizer, MarianMTModel, MarianTokenizerimport shutil#import osimport loggingfrom PyPDF2 import PdfReaderimport docxfrom PIL import Imageimport openpyxl # 📌 Pour lire les fichiers Excel (.xlsx)from pptx import Presentationimport fitz # PyMuPDFimport iofrom docx import Documentimport matplotlib.pyplot as pltimport seaborn as snsimport torchimport reimport pandas as pdfrom transformers import AutoTokenizer, AutoModelForCausalLMfrom fastapi.responses import FileResponseimport osfrom fastapi.middleware.cors import CORSMiddlewareimport matplotlibmatplotlib.use('Agg')
import reimport torchimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsfrom transformers import AutoTokenizer, AutoModelForCausalLMfrom fastapi import FastAPI, File, UploadFile, Formfrom fastapi.responses import FileResponseimport osfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi import FastAPI, File, UploadFile, Formfrom fastapi.responses import JSONResponse, RedirectResponsefrom fastapi.staticfiles import StaticFilesfrom transformers import pipeline, M2M100ForConditionalGeneration, M2M100Tokenizerimport shutilimport osimport loggingfrom fastapi.middleware.cors import CORSMiddlewarefrom PyPDF2 import PdfReaderimport docxfrom PIL import Image # Pour ouvrir les images avant analysefrom transformers import MarianMTModel, MarianTokenizerimport osimport fitz from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
import loggingimport openpyxl
# Configuration du logging1 unmodified line
app = FastAPI()
# Configuration CORSapp.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],)
UPLOAD_DIR = "uploads"os.makedirs(UPLOAD_DIR, exist_ok=True)
#traduction-----------------------------------------------------------------------------------------------------------
from transformers import AutoModelForSeq2SeqLM, AutoTokenizermodel_name = "facebook/m2m100_418M"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# Fonction pour extraire le textedef extract_text_from_pdf(file): doc = fitz.open(stream=file.file.read(), filetype="pdf") return "\n".join([page.get_text() for page in doc]).strip()
def extract_text_from_docx(file): doc = Document(io.BytesIO(file.file.read())) return "\n".join([para.text for para in doc.paragraphs]).strip()
def extract_text_from_pptx(file): prs = Presentation(io.BytesIO(file.file.read())) return "\n".join([shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text")]).strip()
def extract_text_from_excel(file): wb = openpyxl.load_workbook(io.BytesIO(file.file.read()), data_only=True) text = [str(cell) for sheet in wb.worksheets for row in sheet.iter_rows(values_only=True) for cell in row if cell] return "\n".join(text).strip()
@app.post("/translate/")async def translate_document(file: UploadFile = File(...), target_lang: str = Form(...)): """API pour traduire un document.""" try: logging.info(f"📥 Fichier reçu : {file.filename}") logging.info(f"🌍 Langue cible reçue : {target_lang}")
if model is None or tokenizer is None: return JSONResponse(status_code=500, content={"error": "Modèle de traduction non chargé"})
# Extraction du texte if file.filename.endswith(".pdf"): text = extract_text_from_pdf(file) elif file.filename.endswith(".docx"): text = extract_text_from_docx(file) elif file.filename.endswith(".pptx"): text = extract_text_from_pptx(file) elif file.filename.endswith(".xlsx"): text = extract_text_from_excel(file) else: return JSONResponse(status_code=400, content={"error": "Format non supporté"})
logging.info(f"📜 Texte extrait : {text[:50]}...")
if not text: return JSONResponse(status_code=400, content={"error": "Aucun texte trouvé dans le document"})
# Vérifier si la langue cible est supportée target_lang_id = tokenizer.get_lang_id(target_lang)
if target_lang_id is None: return JSONResponse( status_code=400, content={"error": f"Langue cible '{target_lang}' non supportée. Langues disponibles : {list(tokenizer.lang_code_to_id.keys())}"} )
# Traduction tokenizer.src_lang = "fr" encoded_text = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
logging.info(f"🔍 ID de la langue cible : {target_lang_id}")
generated_tokens = model.generate(**encoded_text, forced_bos_token_id=target_lang_id)
translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
logging.info(f"✅ Traduction réussie : {translated_text[:50]}...") return {"translated_text": translated_text}
except Exception as e: logging.error(f"❌ Erreur lors de la traduction : {e}") return JSONResponse(status_code=500, content={"error": "Échec de la traduction"})
# Servir les fichiers statiques (HTML, CSS, JS)app.mount("/static", StaticFiles(directory="static", html=True), name="static")app.py-140