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