Spaces:
Running
Running
lewiskimaru
commited on
Commit
•
75a0eef
1
Parent(s):
5ecc89c
Update static/index.html
Browse files- static/index.html +64 -50
static/index.html
CHANGED
@@ -1,52 +1,66 @@
|
|
1 |
-
<!DOCTYPE html>
|
2 |
<html>
|
3 |
-
<head>
|
4 |
<meta charset="utf-8">
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
<html>
|
2 |
+
<head>
|
3 |
<meta charset="utf-8">
|
4 |
+
<title>Sema API Integration Example</title>
|
5 |
+
</head>
|
6 |
+
<body>
|
7 |
+
<h1>Translate and Detect Source Language</h1>
|
8 |
+
|
9 |
+
<form action="#" method="post">
|
10 |
+
<label for="userinput">Enter text to translate:</label>
|
11 |
+
|
12 |
+
<input type="text" id="userinput" name="userinput">
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
<label for="target_lang">Enter target language:</label>
|
17 |
+
|
18 |
+
<input type="text" id="target_lang" name="target_lang">
|
19 |
+
|
20 |
+
|
21 |
+
<input type="submit" value="Translate">
|
22 |
+
</form>
|
23 |
+
|
24 |
+
<div id="output"></div>
|
25 |
+
|
26 |
+
<script>
|
27 |
+
const form = document.querySelector('form');
|
28 |
+
const targetLangInput = document.querySelector('#target_lang');
|
29 |
+
const outputDiv = document.querySelector('#output');
|
30 |
+
|
31 |
+
form.addEventListener('submit', async (e) => {
|
32 |
+
e.preventDefault();
|
33 |
+
|
34 |
+
const userInput = document.querySelector('#userinput').value;
|
35 |
+
const targetLang = targetLangInput.value || 'swh_Latn';
|
36 |
+
|
37 |
+
try {
|
38 |
+
const response = await fetch('https://kamau1-sema-api.hf.space/translate_detect/', {
|
39 |
+
method: 'POST',
|
40 |
+
headers: {
|
41 |
+
'Content-Type': 'application/json'
|
42 |
+
},
|
43 |
+
body: JSON.stringify({
|
44 |
+
userinput: userInput,
|
45 |
+
target_lang: targetLang
|
46 |
+
})
|
47 |
+
});
|
48 |
+
|
49 |
+
const data = await response.json();
|
50 |
+
const sourceLanguage = data.source_language;
|
51 |
+
const translatedText = data.translations[0].text;
|
52 |
+
|
53 |
+
outputDiv.innerHTML = `
|
54 |
+
<p><strong>Output:</strong></p>
|
55 |
+
<p>Source Language: ${source_language}</p>
|
56 |
+
<p>Translated Text: ${translated_text}</p>
|
57 |
+
`;
|
58 |
+
|
59 |
+
} catch (error) {
|
60 |
+
console.error(error);
|
61 |
+
outputDiv.textContent = 'An error occurred while translating the text. Please check the input and try again.';
|
62 |
+
}
|
63 |
+
});
|
64 |
+
</script>
|
65 |
+
</body>
|
66 |
+
</html>
|