Adam Helmi commited on
Commit
efc909f
·
1 Parent(s): ce223e4

no message

Browse files
Files changed (2) hide show
  1. app.py +7 -7
  2. index.html +262 -0
app.py CHANGED
@@ -11,14 +11,14 @@ from transformers import MBartForConditionalGeneration, MBartTokenizer, MBartCon
11
  html_string = open('index.html', 'r').read()
12
  components.html(html_string, height=600)
13
 
14
- # Load JavaScript and CSS files using Streamlit
15
- with open("styles.css", "r") as css_file:
16
- css = css_file.read()
17
- st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
18
 
19
- with open("script.js", "r") as js_file:
20
- js = js_file.read()
21
- st.markdown(f"<script>{js}</script>", unsafe_allow_html=True)
22
 
23
  # Load model and tokenizer at startup
24
  MODEL_PATH = "GobLyne/Rumi-Jawi-Translater" # Path to your model folder
 
11
  html_string = open('index.html', 'r').read()
12
  components.html(html_string, height=600)
13
 
14
+ # # Load JavaScript and CSS files using Streamlit
15
+ # with open("styles.css", "r") as css_file:
16
+ # css = css_file.read()
17
+ # st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
18
 
19
+ # with open("script.js", "r") as js_file:
20
+ # js = js_file.read()
21
+ # st.markdown(f"<script>{js}</script>", unsafe_allow_html=True)
22
 
23
  # Load model and tokenizer at startup
24
  MODEL_PATH = "GobLyne/Rumi-Jawi-Translater" # Path to your model folder
index.html CHANGED
@@ -51,3 +51,265 @@
51
  <script src="script.js"></script>
52
 
53
  </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  <script src="script.js"></script>
52
 
53
  </html>
54
+
55
+ <script>
56
+ async function translateText(rumiText) {
57
+ try {
58
+ const response = await fetch('https://huggingface.co/spaces/GobLyne/Jawi-Translation/translate', { // Replace with your FastAPI server URL
59
+ method: 'POST',
60
+ headers: {
61
+ 'Content-Type': 'application/json',
62
+ },
63
+ body: JSON.stringify({ text: rumiText }),
64
+ });
65
+
66
+ if (!response.ok) {
67
+ throw new Error('Failed to fetch translation');
68
+ }
69
+
70
+ const data = await response.json();
71
+ return data.translated_text; // Assuming the FastAPI response includes "translated_text"
72
+ } catch (error) {
73
+ console.error('Error:', error);
74
+ return 'Error: Unable to fetch translation';
75
+ }
76
+ }
77
+
78
+ // DOM Elements
79
+ const rumiTextArea = document.getElementById("rumi-text");
80
+ const jawiTextArea = document.getElementById("jawi-text");
81
+ const clearButton = document.querySelector(".btn.clear");
82
+ const translateButton = document.querySelector(".btn.translate");
83
+ const copyButton = document.querySelector(".btn.copy");
84
+
85
+ // Character limit counter
86
+ const charLimit = 120;
87
+ const charCounter = document.createElement("div");
88
+ charCounter.style.marginTop = "10px";
89
+ charCounter.style.color = "#838383";
90
+ charCounter.textContent = `Characters: 0/${charLimit}`;
91
+ rumiTextArea.parentElement.appendChild(charCounter);
92
+
93
+ // Event Listeners
94
+ rumiTextArea.addEventListener("input", () => {
95
+ const inputText = rumiTextArea.value;
96
+ const charCount = inputText.length;
97
+
98
+ if (charCount > charLimit) {
99
+ rumiTextArea.value = inputText.slice(0, charLimit);
100
+ }
101
+
102
+ charCounter.textContent = `Characters: ${Math.min(charCount, charLimit)}/${charLimit}`;
103
+ });
104
+
105
+ clearButton.addEventListener("click", () => {
106
+ rumiTextArea.value = "";
107
+ jawiTextArea.value = "";
108
+ charCounter.textContent = `Characters: 0/${charLimit}`;
109
+ });
110
+
111
+ translateButton.addEventListener("click", async () => {
112
+ const rumiText = rumiTextArea.value.trim();
113
+
114
+ if (!rumiText) {
115
+ alert("Sila masukkan teks Rumi untuk diterjemahkan.");
116
+ return;
117
+ }
118
+
119
+ jawiTextArea.value = "Translating...";
120
+
121
+ try {
122
+ const translatedText = await translateText(rumiText);
123
+ jawiTextArea.value = translatedText;
124
+ } catch (error) {
125
+ jawiTextArea.value = "Error: Unable to translate the text. Please try again later.";
126
+ console.error("Translation error:", error);
127
+ }
128
+ });
129
+
130
+ copyButton.addEventListener("click", () => {
131
+ if (jawiTextArea.value) {
132
+ jawiTextArea.select();
133
+ document.execCommand("copy");
134
+
135
+ // Display text pop-up inside the website
136
+ const popup = document.createElement("div");
137
+ popup.textContent = "Teks telah disalin!";
138
+ popup.style.position = "fixed";
139
+ popup.style.bottom = "20px";
140
+ popup.style.right = "20px";
141
+ popup.style.backgroundColor = "#4CAF50";
142
+ popup.style.color = "white";
143
+ popup.style.padding = "10px";
144
+ popup.style.borderRadius = "5px";
145
+ popup.style.boxShadow = "0px 4px 6px rgba(0, 0, 0, 0.1)";
146
+ popup.style.zIndex = "1000";
147
+ document.body.appendChild(popup);
148
+
149
+ setTimeout(() => {
150
+ document.body.removeChild(popup);
151
+ }, 2000);
152
+ } else {
153
+ alert("Tiada teks untuk disalin.");
154
+ }
155
+ });
156
+ </script>
157
+
158
+ <style>
159
+ /* General Reset */
160
+ * {
161
+ margin: 0;
162
+ padding: 0;
163
+ box-sizing: border-box;
164
+ }
165
+
166
+ body {
167
+ font-family: Arial, sans-serif;
168
+ line-height: 1.6;
169
+ background-color: #f8fcf9;
170
+ color: #333;
171
+ }
172
+
173
+ header {
174
+ display: flex;
175
+ justify-content: space-between;
176
+ align-items: center;
177
+ padding: 10px 20px;
178
+ background-color: #edf7f1;
179
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
180
+ }
181
+
182
+ header .logo {
183
+ font-size: 1.5rem;
184
+ font-weight: bold;
185
+ font-family: 'Georgia', serif;
186
+ }
187
+
188
+ nav a {
189
+ margin: 0 10px;
190
+ text-decoration: none;
191
+ color: #838383;
192
+ font-weight: bold;
193
+ }
194
+
195
+ nav a:hover {
196
+ color: #007a5e;
197
+ }
198
+
199
+ main {
200
+ padding: 20px;
201
+ text-align: center;
202
+ }
203
+
204
+ .greeting h1 {
205
+ font-size: 2rem;
206
+ margin-left: 10px;
207
+ margin-right: 10px;
208
+ margin-bottom: 10px;
209
+ text-align: left;
210
+ }
211
+
212
+ .greeting p {
213
+ font-size: 1.2rem;
214
+ margin-left: 11px;
215
+ margin-right: 11px;
216
+ margin-bottom: 20px;
217
+ text-align: left;
218
+ color: #838383;
219
+ }
220
+
221
+ .highlight {
222
+ color: #007a5e;
223
+ font-weight: bold;
224
+ }
225
+
226
+ .translator {
227
+ display: flex;
228
+ justify-content: left;
229
+ flex-wrap: wrap;
230
+ margin-top: 30px;
231
+ margin-left: 13px;
232
+ margin-right: 13px;
233
+ }
234
+
235
+ .input-box {
236
+ width: 45%;
237
+ resize: none;
238
+ }
239
+
240
+ .output-box {
241
+ width: 45%;
242
+ resize: none;
243
+ margin-left: 25px;
244
+ }
245
+
246
+ textarea {
247
+ width: 100%;
248
+ height: 150px;
249
+ padding: 10px;
250
+ font-size: 1rem;
251
+ border: 1px solid #ccc;
252
+ border-radius: 8px;
253
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
254
+ }
255
+
256
+ textarea:focus {
257
+ outline: none;
258
+ border-color: #007a5e;
259
+ }
260
+
261
+ .buttons {
262
+ display: flex;
263
+ flex-direction: row;
264
+ justify-content: right;
265
+ gap: 10px;
266
+ margin: 10px 0;
267
+ }
268
+
269
+ .btn {
270
+ padding: 10px 20px;
271
+ font-size: 1rem;
272
+ border: none;
273
+ border-radius: 5px;
274
+ cursor: pointer;
275
+ }
276
+
277
+ .btn.clear {
278
+ background-color: #ff6b6b;
279
+ color: white;
280
+ width: 100px;
281
+ justify-content: center;
282
+ }
283
+
284
+ .btn.clear:hover {
285
+ background-color: #e54848;
286
+ }
287
+
288
+ .btn.translate {
289
+ background-color: #007a5e;
290
+ color: white;
291
+ width: 100px;
292
+ justify-content: center;
293
+ }
294
+
295
+ .btn.translate:hover {
296
+ background-color: #005a42;
297
+ }
298
+
299
+ .btn.copy {
300
+ background-color: #ffc107;
301
+ color: white;
302
+ }
303
+
304
+ .btn.copy:hover {
305
+ background-color: #e0a800;
306
+ }
307
+
308
+ footer {
309
+ margin-top: 35px;
310
+ padding: 10px 20px;
311
+ text-align: center;
312
+ font-size: 0.9rem;
313
+ color: #555;
314
+ }
315
+ </style>