AlexVed commited on
Commit
fdc81e6
·
verified ·
1 Parent(s): 9c69fdb

Update app.js

Browse files

Updated app.js

Files changed (1) hide show
  1. app.js +18 -7
app.js CHANGED
@@ -3,16 +3,27 @@ import { useState } from "react";
3
  export default function Translator() {
4
  const [text, setText] = useState("");
5
  const [translation, setTranslation] = useState("");
 
6
 
7
  const translateText = async () => {
8
  if (!text) return;
9
- const response = await fetch("http://localhost:8501/translate", {
10
- method: "POST",
11
- headers: { "Content-Type": "application/json" },
12
- body: JSON.stringify({ text }),
13
- });
 
 
 
 
 
 
 
 
 
14
  const data = await response.json();
15
- setTranslation(data.translation);
 
16
  };
17
 
18
  return (
@@ -28,7 +39,7 @@ export default function Translator() {
28
  onClick={translateText}
29
  className="mt-4 px-6 py-2 bg-blue-600 text-white rounded-lg shadow-md hover:bg-blue-700"
30
  >
31
- Translate
32
  </button>
33
  {translation && (
34
  <div className="mt-4 p-4 bg-white rounded-lg shadow-md max-w-md">
 
3
  export default function Translator() {
4
  const [text, setText] = useState("");
5
  const [translation, setTranslation] = useState("");
6
+ const [loading, setLoading] = useState(false);
7
 
8
  const translateText = async () => {
9
  if (!text) return;
10
+ setLoading(true);
11
+
12
+ const response = await fetch(
13
+ "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-uk",
14
+ {
15
+ method: "POST",
16
+ headers: {
17
+ "Authorization": `Bearer YOUR_HUGGINGFACE_API_KEY`, // Replace with your API key
18
+ "Content-Type": "application/json",
19
+ },
20
+ body: JSON.stringify({ inputs: text }),
21
+ }
22
+ );
23
+
24
  const data = await response.json();
25
+ setTranslation(data[0]?.translation_text || "Translation failed.");
26
+ setLoading(false);
27
  };
28
 
29
  return (
 
39
  onClick={translateText}
40
  className="mt-4 px-6 py-2 bg-blue-600 text-white rounded-lg shadow-md hover:bg-blue-700"
41
  >
42
+ {loading ? "Translating..." : "Translate"}
43
  </button>
44
  {translation && (
45
  <div className="mt-4 p-4 bg-white rounded-lg shadow-md max-w-md">