Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,17 @@ import math
|
|
2 |
import gradio as gr
|
3 |
from transformers import MarianTokenizer, MarianMTModel
|
4 |
import re
|
|
|
5 |
|
6 |
###################################
|
7 |
-
# 1) โหลดโมเดล MarianMT (Thai->En)
|
8 |
###################################
|
9 |
model_name = "Helsinki-NLP/opus-mt-th-en"
|
10 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
11 |
model = MarianMTModel.from_pretrained(model_name)
|
12 |
|
|
|
|
|
13 |
def translate_th_to_en(text_th: str) -> str:
|
14 |
"""
|
15 |
แปลไทย -> อังกฤษ ด้วย MarianMT บน CPU
|
@@ -32,18 +35,14 @@ def approximate_temp_with_star(star_type, distance_au, albedo=0.3):
|
|
32 |
distance_au: ระยะห่าง (AU)
|
33 |
albedo: สะท้อนแสง (0.3)
|
34 |
"""
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
elif star_type == "Supergiant":
|
44 |
-
lum_ratio = 100.0
|
45 |
-
else:
|
46 |
-
lum_ratio = 1.0
|
47 |
|
48 |
luminosity = 3.828e26 * lum_ratio
|
49 |
dist_m = distance_au * 1.496e11
|
@@ -67,14 +66,12 @@ def approximate_gravity(diameter_factor, planet_type_th):
|
|
67 |
radius = diameter_factor * earth_radius # m
|
68 |
|
69 |
# กำหนดความหนาแน่นตามชนิดดาว
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
else:
|
77 |
-
density = 5500 # ค่าเริ่มต้นเป็นดาวหิน
|
78 |
|
79 |
# คำนวณมวล: M = density * volume = density * (4/3) * pi * R^3
|
80 |
mass = density * (4/3) * math.pi * radius**3
|
@@ -371,11 +368,11 @@ def generate_planet_info(
|
|
371 |
# -----------------------------
|
372 |
# (C) สร้าง Prompt อังกฤษ 3 แบบ
|
373 |
# -----------------------------
|
374 |
-
# 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input)
|
375 |
planet_name_en = translate_th_to_en(planet_name_th)
|
376 |
life_en = translate_th_to_en(life_th)
|
377 |
|
378 |
-
# 2) อธิบาย distance/temp/gravity/tilt/moon/oxygen เป็นภาษาไทย -> แปลเป็น EN
|
379 |
dist_desc_th = describe_distance(distance_au)
|
380 |
temp_desc_th = describe_temp(temp_c)
|
381 |
grav_desc_th = describe_gravity(g_approx)
|
@@ -511,7 +508,7 @@ with gr.Blocks(css=css_code) as demo:
|
|
511 |
choices=["Red Dwarf", "White Dwarf", "Sun-like", "Blue Giant", "Supergiant"],
|
512 |
value="Sun-like"
|
513 |
)
|
514 |
-
distance_au = gr.Textbox(label="ระยะห่าง AU
|
515 |
diameter_factor = gr.Textbox(label="ขนาด (เท่าโลก)", placeholder="เช่น 1, 2, 0.5...")
|
516 |
planet_type_th = gr.Dropdown(
|
517 |
label="ชนิดดาวเคราะห์ (ไทย)",
|
@@ -542,7 +539,7 @@ with gr.Blocks(css=css_code) as demo:
|
|
542 |
</div>
|
543 |
<script>
|
544 |
function copyText(promptId) {
|
545 |
-
const promptBox = document.querySelector(
|
546 |
if (!promptBox) {
|
547 |
alert('ไม่พบข้อความ Prompt!');
|
548 |
return;
|
|
|
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
|
|
|
35 |
distance_au: ระยะห่าง (AU)
|
36 |
albedo: สะท้อนแสง (0.3)
|
37 |
"""
|
38 |
+
lum_ratio_map = {
|
39 |
+
"Red Dwarf": 0.02,
|
40 |
+
"White Dwarf": 0.001,
|
41 |
+
"Sun-like": 1.0,
|
42 |
+
"Blue Giant": 10.0,
|
43 |
+
"Supergiant": 100.0
|
44 |
+
}
|
45 |
+
lum_ratio = lum_ratio_map.get(star_type, 1.0)
|
|
|
|
|
|
|
|
|
46 |
|
47 |
luminosity = 3.828e26 * lum_ratio
|
48 |
dist_m = distance_au * 1.496e11
|
|
|
66 |
radius = diameter_factor * earth_radius # m
|
67 |
|
68 |
# กำหนดความหนาแน่นตามชนิดดาว
|
69 |
+
density_map = {
|
70 |
+
"ดาวหิน": 5500, # kg/m³
|
71 |
+
"ดาวก๊าซ": 1300, # kg/m³
|
72 |
+
"ดาวน้ำแข็ง": 1600 # kg/m³
|
73 |
+
}
|
74 |
+
density = density_map.get(planet_type_th, 5500) # ค่าเริ่มต้นเป็นดาวหิน
|
|
|
|
|
75 |
|
76 |
# คำนวณมวล: M = density * volume = density * (4/3) * pi * R^3
|
77 |
mass = density * (4/3) * math.pi * radius**3
|
|
|
368 |
# -----------------------------
|
369 |
# (C) สร้าง Prompt อังกฤษ 3 แบบ
|
370 |
# -----------------------------
|
371 |
+
# 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input) ผ่านแคช
|
372 |
planet_name_en = translate_th_to_en(planet_name_th)
|
373 |
life_en = translate_th_to_en(life_th)
|
374 |
|
375 |
+
# 2) อธิบาย distance/temp/gravity/tilt/moon/oxygen เป็นภาษาไทย -> แปลเป็น EN จาก Dictionary
|
376 |
dist_desc_th = describe_distance(distance_au)
|
377 |
temp_desc_th = describe_temp(temp_c)
|
378 |
grav_desc_th = describe_gravity(g_approx)
|
|
|
508 |
choices=["Red Dwarf", "White Dwarf", "Sun-like", "Blue Giant", "Supergiant"],
|
509 |
value="Sun-like"
|
510 |
)
|
511 |
+
distance_au = gr.Textbox(label="ระยะห่าง (AU)", placeholder="เช่น 1, 0.5, 2...")
|
512 |
diameter_factor = gr.Textbox(label="ขนาด (เท่าโลก)", placeholder="เช่น 1, 2, 0.5...")
|
513 |
planet_type_th = gr.Dropdown(
|
514 |
label="ชนิดดาวเคราะห์ (ไทย)",
|
|
|
539 |
</div>
|
540 |
<script>
|
541 |
function copyText(promptId) {
|
542 |
+
const promptBox = document.querySelector(`#prompt${promptId.slice(-1)}-en textarea`);
|
543 |
if (!promptBox) {
|
544 |
alert('ไม่พบข้อความ Prompt!');
|
545 |
return;
|