Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,29 @@
|
|
1 |
import math
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
import re
|
5 |
import functools
|
6 |
|
7 |
###################################
|
8 |
-
# 1) โหลดโมเดล
|
9 |
###################################
|
10 |
-
model_name = "
|
11 |
-
tokenizer =
|
12 |
-
model =
|
13 |
|
14 |
# เพิ่มการแคชผลลัพธ์การแปล
|
15 |
@functools.lru_cache(maxsize=1024)
|
16 |
def translate_th_to_en(text_th: str) -> str:
|
17 |
"""
|
18 |
-
แปลไทย -> อังกฤษ ด้วย
|
19 |
* เรียกเฉพาะส่วนที่ผู้ใช้พิมพ์ เช่น ชื่อดาว, ชื่อสิ่งมีชีวิต
|
20 |
"""
|
21 |
text_th = text_th.strip()
|
22 |
if not text_th:
|
23 |
return ""
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
outputs = model.generate(inputs, max_length=512, num_beams=4, early_stopping=True)
|
28 |
-
en_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
return en_text
|
30 |
|
31 |
###################################
|
@@ -535,13 +533,13 @@ with gr.Blocks(css=css_code) as demo:
|
|
535 |
# ฟังก์ชันคัดลอก Prompt
|
536 |
copy_button_html = """
|
537 |
<div style="display: flex; align-items: center; gap: 10px;">
|
538 |
-
<button class="copy-btn" onclick="copyText('
|
539 |
-
<button class="copy-btn" onclick="copyText('
|
540 |
-
<button class="copy-btn" onclick="copyText('
|
541 |
</div>
|
542 |
<script>
|
543 |
-
function copyText(
|
544 |
-
const promptBox = document.querySelector(`#prompt${
|
545 |
if (!promptBox) {
|
546 |
alert('ไม่พบข้อความ Prompt!');
|
547 |
return;
|
@@ -562,8 +560,8 @@ with gr.Blocks(css=css_code) as demo:
|
|
562 |
star_type_en=s_type_en,
|
563 |
distance_str=dist_au,
|
564 |
diameter_str=dia_fac,
|
565 |
-
tilt_value=tilt_val,
|
566 |
-
moon_value=moon_val,
|
567 |
oxygen_percent=oxy_val,
|
568 |
planet_type_th=p_type_th,
|
569 |
life_th=l_th
|
|
|
1 |
import math
|
2 |
import gradio as gr
|
3 |
+
from transformers import MarianTokenizer, MarianMTModel
|
4 |
import re
|
5 |
import functools
|
6 |
|
7 |
###################################
|
8 |
+
# 1) โหลดโมเดล MarianMT (Thai->En) ครั้งเดียวเมื่อเริ่มแอป
|
9 |
###################################
|
10 |
+
model_name = "Helsinki-NLP/opus-mt-th-en"
|
11 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
12 |
+
model = MarianMTModel.from_pretrained(model_name)
|
13 |
|
14 |
# เพิ่มการแคชผลลัพธ์การแปล
|
15 |
@functools.lru_cache(maxsize=1024)
|
16 |
def translate_th_to_en(text_th: str) -> str:
|
17 |
"""
|
18 |
+
แปลไทย -> อังกฤษ ด้วย MarianMT บน CPU
|
19 |
* เรียกเฉพาะส่วนที่ผู้ใช้พิมพ์ เช่น ชื่อดาว, ชื่อสิ่งมีชีวิต
|
20 |
"""
|
21 |
text_th = text_th.strip()
|
22 |
if not text_th:
|
23 |
return ""
|
24 |
+
inputs = tokenizer(text_th, return_tensors="pt", max_length=512, truncation=True)
|
25 |
+
translation_tokens = model.generate(**inputs, max_length=512)
|
26 |
+
en_text = tokenizer.decode(translation_tokens[0], skip_special_tokens=True)
|
|
|
|
|
27 |
return en_text
|
28 |
|
29 |
###################################
|
|
|
533 |
# ฟังก์ชันคัดลอก Prompt
|
534 |
copy_button_html = """
|
535 |
<div style="display: flex; align-items: center; gap: 10px;">
|
536 |
+
<button class="copy-btn" onclick="copyText('1')">คัดลอก Prompt #1</button>
|
537 |
+
<button class="copy-btn" onclick="copyText('2')">คัดลอก Prompt #2</button>
|
538 |
+
<button class="copy-btn" onclick="copyText('3')">คัดลอก Prompt #3</button>
|
539 |
</div>
|
540 |
<script>
|
541 |
+
function copyText(promptNumber) {
|
542 |
+
const promptBox = document.querySelector(`#prompt${promptNumber}-en textarea`);
|
543 |
if (!promptBox) {
|
544 |
alert('ไม่พบข้อความ Prompt!');
|
545 |
return;
|
|
|
560 |
star_type_en=s_type_en,
|
561 |
distance_str=dist_au,
|
562 |
diameter_str=dia_fac,
|
563 |
+
tilt_value=str(tilt_val),
|
564 |
+
moon_value=str(moon_val),
|
565 |
oxygen_percent=oxy_val,
|
566 |
planet_type_th=p_type_th,
|
567 |
life_th=l_th
|