Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,68 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
# Création de l'application FastAPI
|
5 |
app = FastAPI(title="Traduction Anglais → Français")
|
6 |
|
7 |
-
|
|
|
|
|
|
|
8 |
def home():
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
@app.post("/translate/")
|
12 |
def translate(text: str):
|
13 |
-
"""
|
14 |
-
Traduit un texte de l'anglais vers le français.
|
15 |
-
"""
|
16 |
if not text:
|
17 |
raise HTTPException(status_code=400, detail="Le texte est vide.")
|
18 |
|
19 |
try:
|
20 |
-
translator = pipeline("translation", model="facebook/wmt19-en-fr")
|
21 |
translated_text = translator(text)[0]['translation_text']
|
22 |
return {"original": text, "translated": translated_text}
|
23 |
except Exception as e:
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
from transformers import pipeline
|
4 |
|
5 |
# Création de l'application FastAPI
|
6 |
app = FastAPI(title="Traduction Anglais → Français")
|
7 |
|
8 |
+
# Chargement du modèle une seule fois pour éviter les rechargements inutiles
|
9 |
+
translator = pipeline("translation_en_to_fr", model="facebook/wmt19-en-fr")
|
10 |
+
|
11 |
+
@app.get("/", response_class=HTMLResponse)
|
12 |
def home():
|
13 |
+
# Interface Web (HTML, CSS, JS intégrés)
|
14 |
+
return """
|
15 |
+
<!DOCTYPE html>
|
16 |
+
<html lang="fr">
|
17 |
+
<head>
|
18 |
+
<meta charset="UTF-8">
|
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 |
+
.container { max-width: 500px; margin: 50px auto; padding: 20px; background: white; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); }
|
24 |
+
textarea { width: 100%; height: 100px; margin-bottom: 10px; padding: 10px; border-radius: 5px; border: 1px solid #ccc; }
|
25 |
+
button { padding: 10px 20px; border: none; background: #007BFF; color: white; font-size: 16px; border-radius: 5px; cursor: pointer; }
|
26 |
+
button:hover { background: #0056b3; }
|
27 |
+
.result { margin-top: 20px; padding: 10px; background: #e0ffe0; border-radius: 5px; }
|
28 |
+
</style>
|
29 |
+
</head>
|
30 |
+
<body>
|
31 |
+
<div class="container">
|
32 |
+
<h2>Traduction Anglais → Français</h2>
|
33 |
+
<textarea id="inputText" placeholder="Entrez du texte en anglais..."></textarea>
|
34 |
+
<button onclick="translateText()">Traduire</button>
|
35 |
+
<div id="output" class="result"></div>
|
36 |
+
</div>
|
37 |
+
<script>
|
38 |
+
async function translateText() {
|
39 |
+
const text = document.getElementById("inputText").value;
|
40 |
+
if (!text) {
|
41 |
+
alert("Veuillez entrer du texte !");
|
42 |
+
return;
|
43 |
+
}
|
44 |
+
|
45 |
+
const response = await fetch("/translate/?text=" + encodeURIComponent(text), { method: "POST" });
|
46 |
+
const data = await response.json();
|
47 |
+
|
48 |
+
if (response.ok) {
|
49 |
+
document.getElementById("output").innerText = "Traduction: " + data.translated;
|
50 |
+
} else {
|
51 |
+
alert("Erreur: " + data.detail);
|
52 |
+
}
|
53 |
+
}
|
54 |
+
</script>
|
55 |
+
</body>
|
56 |
+
</html>
|
57 |
+
"""
|
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:
|