File size: 3,807 Bytes
3320fc6 dac3f07 fa0996d ae6c842 3320fc6 ae6c842 a0e137a dac3f07 a0e137a dac3f07 a0e137a dac3f07 a0e137a dac3f07 a0e137a dac3f07 a0e137a dac3f07 ae6c842 3320fc6 a0e137a 3320fc6 a0e137a fa0996d 3320fc6 fa0996d 3320fc6 e059b03 a0e137a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from transformers import pipeline
# Création de l'application FastAPI
app = FastAPI(title="Traduction Anglais → Français")
# Chargement du modèle de traduction
translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")
@app.get("/", response_class=HTMLResponse)
def serve_html():
""" Sert l'interface utilisateur en HTML """
return """
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Traduction Anglais → Français</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin: 50px; background-color: #f4f4f4; }
h2 { color: #333; }
textarea { width: 80%; height: 100px; margin: 10px; padding: 10px; font-size: 16px; }
button { padding: 10px 20px; font-size: 16px; background-color: #007BFF; color: white; border: none; cursor: pointer; }
button:hover { background-color: #0056b3; }
#output { margin-top: 20px; font-size: 18px; font-weight: bold; color: #28a745; }
.loader { display: none; margin: 10px auto; border: 5px solid #f3f3f3; border-top: 5px solid #007BFF; border-radius: 50%; width: 40px; height: 40px; animation: spin 1s linear infinite; }
@keyframes spin { 100% { transform: rotate(360deg); } }
</style>
</head>
<body>
<h2>Traduction Anglais → Français</h2>
<textarea id="textInput" placeholder="Entrez votre texte en anglais..."></textarea><br>
<button onclick="translateText()">Traduire</button>
<div class="loader" id="loader"></div>
<p id="output"></p>
<script>
async function translateText() {
let text = document.getElementById("textInput").value;
let output = document.getElementById("output");
let loader = document.getElementById("loader");
if (!text.trim()) {
output.innerText = "Veuillez entrer du texte !";
output.style.color = "red";
return;
}
loader.style.display = "block";
output.innerText = "";
try {
let response = await fetch("/translate/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: text })
});
let data = await response.json();
if (response.ok) {
output.innerText = "Traduction : " + data.translated;
output.style.color = "#28a745";
} else {
output.innerText = "Erreur : " + data.detail;
output.style.color = "red";
}
} catch (error) {
output.innerText = "Erreur de connexion.";
output.style.color = "red";
} finally {
loader.style.display = "none";
}
}
</script>
</body>
</html>
"""
@app.post("/translate/")
def translate(text: str):
""" Traduit un texte de l'anglais vers le français """
if not text:
raise HTTPException(status_code=400, detail="Le texte est vide.")
try:
translated_text = translator(text)[0]['translation_text']
return {"original": text, "translated": translated_text}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Erreur de traduction : {e}")
|