luck210 commited on
Commit
a0e137a
·
verified ·
1 Parent(s): dac3f07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -29
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 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">
@@ -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
- .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>
@@ -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
+