Update app.py
Browse files
app.py
CHANGED
@@ -153,7 +153,7 @@ def describe_oxygen_th_to_en(desc_th):
|
|
153 |
return "high oxygen"
|
154 |
|
155 |
###################################
|
156 |
-
# ฟังก์ชันบรรยาย (ภาษาไทย)
|
157 |
###################################
|
158 |
def describe_distance(distance_au):
|
159 |
if distance_au < 0.5:
|
@@ -232,35 +232,60 @@ def describe_oxygen(oxygen_percent):
|
|
232 |
return "ออกซิเจนสูง"
|
233 |
|
234 |
###################################
|
235 |
-
#
|
236 |
###################################
|
237 |
def build_prompts_en(
|
238 |
planet_name_en, star_type_en,
|
239 |
-
dist_desc_en, temp_desc_en, grav_desc_en, tilt_desc_en, moon_desc_en, oxygen_desc_en, life_en
|
240 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
# Prompt 1
|
242 |
prompt1 = (
|
243 |
f"A vibrant space painting of planet '{planet_name_en}' orbiting a {star_type_en} star. "
|
244 |
f"It is {dist_desc_en}, with {temp_desc_en} conditions and {grav_desc_en} gravity. "
|
245 |
-
f"{tilt_desc_en}, {moon_desc_en}, atmosphere has {oxygen_desc_en}.
|
|
|
246 |
)
|
247 |
|
248 |
# Prompt 2
|
249 |
prompt2 = (
|
250 |
f"On planet '{planet_name_en}', we discover {life_en} thriving in {temp_desc_en} weather, "
|
251 |
-
f"{grav_desc_en} pull, and {oxygen_desc_en} in the air.
|
|
|
252 |
)
|
253 |
|
254 |
# Prompt 3
|
255 |
prompt3 = (
|
256 |
f"Exploring the surface of '{planet_name_en}': {temp_desc_en} climate, {grav_desc_en}, "
|
257 |
-
f"{tilt_desc_en} tilt, and {moon_desc_en}.
|
|
|
258 |
)
|
259 |
|
260 |
return prompt1, prompt2, prompt3
|
261 |
|
262 |
###################################
|
263 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
###################################
|
265 |
def generate_planet_info(
|
266 |
planet_name_th,
|
@@ -271,7 +296,8 @@ def generate_planet_info(
|
|
271 |
moon_value,
|
272 |
oxygen_percent,
|
273 |
planet_type_th,
|
274 |
-
life_th
|
|
|
275 |
):
|
276 |
# parse
|
277 |
try:
|
@@ -366,7 +392,7 @@ def generate_planet_info(
|
|
366 |
)
|
367 |
|
368 |
# -----------------------------
|
369 |
-
# (C) สร้าง Prompt อังกฤษ 3 แบบ
|
370 |
# -----------------------------
|
371 |
# 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input) ผ่านแคช
|
372 |
planet_name_en = translate_th_to_en(planet_name_th)
|
@@ -398,7 +424,8 @@ def generate_planet_info(
|
|
398 |
tilt_desc_en,
|
399 |
moon_desc_en,
|
400 |
oxygen_desc_en,
|
401 |
-
life_en
|
|
|
402 |
)
|
403 |
|
404 |
return child_summary, detail_th, prompt1, prompt2, prompt3
|
@@ -408,30 +435,62 @@ def generate_planet_info(
|
|
408 |
###################################
|
409 |
formula_text = r"""
|
410 |
**สูตรอุณหภูมิ (Stefan-Boltzmann) แบบง่าย**
|
411 |
-
|
412 |
T = \left(\frac{(1 - A) \times L}{16 \pi \sigma \, d^2}\right)^{\frac{1}{4}} - 273.15 + 15^\circ\text{C (Greenhouse)}
|
413 |
-
|
414 |
|
415 |
-
-
|
416 |
-
-
|
417 |
-
-
|
418 |
-
-
|
419 |
|
420 |
**สูตรแรงโน้มถ่วงนิวตัน**
|
421 |
-
|
422 |
-
(เราใช้สมมุติว่า
|
423 |
"""
|
424 |
|
425 |
###################################
|
426 |
-
#
|
427 |
###################################
|
428 |
css_code = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
429 |
body {
|
430 |
-
background-color:
|
|
|
431 |
font-family: "Kanit", sans-serif;
|
432 |
}
|
433 |
#title {
|
434 |
-
color:
|
435 |
text-align: center;
|
436 |
font-size: 2rem;
|
437 |
margin-top: 20px;
|
@@ -441,37 +500,48 @@ body {
|
|
441 |
.game-desc {
|
442 |
margin: 0 auto;
|
443 |
width: 90%;
|
444 |
-
background-color:
|
445 |
-
border: 2px dashed
|
446 |
border-radius: 10px;
|
447 |
padding: 15px;
|
448 |
-
color:
|
449 |
margin-bottom: 20px;
|
450 |
}
|
451 |
.btn-main {
|
452 |
-
background-color:
|
453 |
-
border: 2px solid
|
454 |
font-weight: bold;
|
455 |
font-size: 1.1rem;
|
456 |
padding: 10px 30px;
|
457 |
border-radius: 10px;
|
458 |
margin-right: 10px;
|
|
|
|
|
|
|
|
|
|
|
459 |
}
|
460 |
#child-summary, #detail-th, #prompt1-en, #prompt2-en, #prompt3-en, #formula-box {
|
461 |
-
background-color:
|
462 |
-
border: 2px solid
|
463 |
border-radius: 10px;
|
464 |
padding: 10px;
|
465 |
margin-bottom: 20px;
|
|
|
466 |
}
|
467 |
.copy-btn {
|
468 |
-
background-color:
|
469 |
-
border: 2px solid
|
470 |
font-weight: bold;
|
471 |
font-size: 0.8rem;
|
472 |
padding: 5px 10px;
|
473 |
border-radius: 5px;
|
474 |
margin-top: 5px;
|
|
|
|
|
|
|
|
|
|
|
475 |
}
|
476 |
"""
|
477 |
|
@@ -522,6 +592,13 @@ with gr.Blocks(css=css_code) as demo:
|
|
522 |
oxygen_slider = gr.Slider(0, 100, step=1, value=21, label="% ออกซิเจน")
|
523 |
life_th = gr.Textbox(label="สิ่งมีชีวิต (ไทย)", placeholder="เช่น แมลงยักษ์เรืองแสง...")
|
524 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
525 |
create_btn = gr.Button("สร้างโลกแฟนตาซี", elem_classes="btn-main")
|
526 |
|
527 |
child_summary_out = gr.Textbox(label="สรุปสำหรับเด็ก (ไทย)", interactive=False, elem_id="child-summary")
|
@@ -553,7 +630,7 @@ with gr.Blocks(css=css_code) as demo:
|
|
553 |
gr.HTML(copy_button_html)
|
554 |
|
555 |
def generate_wrapper(
|
556 |
-
p_name_th, s_type_en, dist_au, dia_fac, tilt_val, moon_val, oxy_val, p_type_th, l_th
|
557 |
):
|
558 |
return generate_planet_info(
|
559 |
planet_name_th=p_name_th,
|
@@ -564,7 +641,8 @@ with gr.Blocks(css=css_code) as demo:
|
|
564 |
moon_value=str(moon_val),
|
565 |
oxygen_percent=oxy_val,
|
566 |
planet_type_th=p_type_th,
|
567 |
-
life_th=l_th
|
|
|
568 |
)
|
569 |
|
570 |
create_btn.click(
|
@@ -578,7 +656,8 @@ with gr.Blocks(css=css_code) as demo:
|
|
578 |
moon_slider,
|
579 |
oxygen_slider,
|
580 |
planet_type_th,
|
581 |
-
life_th
|
|
|
582 |
],
|
583 |
outputs=[
|
584 |
child_summary_out,
|
|
|
153 |
return "high oxygen"
|
154 |
|
155 |
###################################
|
156 |
+
# 5) ฟังก์ชันบรรยาย (ภาษาไทย)
|
157 |
###################################
|
158 |
def describe_distance(distance_au):
|
159 |
if distance_au < 0.5:
|
|
|
232 |
return "ออกซิเจนสูง"
|
233 |
|
234 |
###################################
|
235 |
+
# 6) สร้าง Prompt 3 แบบ (Pre-translate dictionary) พร้อมสไตล์
|
236 |
###################################
|
237 |
def build_prompts_en(
|
238 |
planet_name_en, star_type_en,
|
239 |
+
dist_desc_en, temp_desc_en, grav_desc_en, tilt_desc_en, moon_desc_en, oxygen_desc_en, life_en, style_en
|
240 |
):
|
241 |
+
# ถ้าเลือกสไตล์มาตรฐาน ไม่เพิ่มสไตล์ลงในพรอมพ์
|
242 |
+
if style_en.lower() != "standard":
|
243 |
+
style_text = f"in the style of {style_en}"
|
244 |
+
else:
|
245 |
+
style_text = ""
|
246 |
+
|
247 |
# Prompt 1
|
248 |
prompt1 = (
|
249 |
f"A vibrant space painting of planet '{planet_name_en}' orbiting a {star_type_en} star. "
|
250 |
f"It is {dist_desc_en}, with {temp_desc_en} conditions and {grav_desc_en} gravity. "
|
251 |
+
f"{tilt_desc_en}, {moon_desc_en}, atmosphere has {oxygen_desc_en}. "
|
252 |
+
f"{style_text}. Cinematic details."
|
253 |
)
|
254 |
|
255 |
# Prompt 2
|
256 |
prompt2 = (
|
257 |
f"On planet '{planet_name_en}', we discover {life_en} thriving in {temp_desc_en} weather, "
|
258 |
+
f"{grav_desc_en} pull, and {oxygen_desc_en} in the air. {style_text.capitalize()} "
|
259 |
+
f"alien ecosystem, rich concept art."
|
260 |
)
|
261 |
|
262 |
# Prompt 3
|
263 |
prompt3 = (
|
264 |
f"Exploring the surface of '{planet_name_en}': {temp_desc_en} climate, {grav_desc_en}, "
|
265 |
+
f"{tilt_desc_en} tilt, and {moon_desc_en}. {style_text.capitalize()} "
|
266 |
+
f"environment design, atmospheric perspective."
|
267 |
)
|
268 |
|
269 |
return prompt1, prompt2, prompt3
|
270 |
|
271 |
###################################
|
272 |
+
# 7) สร้าง Dropdown สำหรับเลือกสไตล์
|
273 |
+
###################################
|
274 |
+
style_options = [
|
275 |
+
"Standard",
|
276 |
+
"Disney-Pixar",
|
277 |
+
"Studio Ghibli",
|
278 |
+
"Cartoon",
|
279 |
+
"Superhero",
|
280 |
+
"Fairy Tale",
|
281 |
+
"Adventure",
|
282 |
+
"Sci-Fi",
|
283 |
+
"Anime",
|
284 |
+
"Comic Book"
|
285 |
+
]
|
286 |
+
|
287 |
+
###################################
|
288 |
+
# 8) ฟังก์ชันหลัก generate_planet_info พร้อมสไตล์
|
289 |
###################################
|
290 |
def generate_planet_info(
|
291 |
planet_name_th,
|
|
|
296 |
moon_value,
|
297 |
oxygen_percent,
|
298 |
planet_type_th,
|
299 |
+
life_th,
|
300 |
+
style_selected
|
301 |
):
|
302 |
# parse
|
303 |
try:
|
|
|
392 |
)
|
393 |
|
394 |
# -----------------------------
|
395 |
+
# (C) สร้าง Prompt อังกฤษ 3 แบบ พร้อมสไตล์
|
396 |
# -----------------------------
|
397 |
# 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input) ผ่านแคช
|
398 |
planet_name_en = translate_th_to_en(planet_name_th)
|
|
|
424 |
tilt_desc_en,
|
425 |
moon_desc_en,
|
426 |
oxygen_desc_en,
|
427 |
+
life_en,
|
428 |
+
style_selected
|
429 |
)
|
430 |
|
431 |
return child_summary, detail_th, prompt1, prompt2, prompt3
|
|
|
435 |
###################################
|
436 |
formula_text = r"""
|
437 |
**สูตรอุณหภูมิ (Stefan-Boltzmann) แบบง่าย**
|
438 |
+
\[
|
439 |
T = \left(\frac{(1 - A) \times L}{16 \pi \sigma \, d^2}\right)^{\frac{1}{4}} - 273.15 + 15^\circ\text{C (Greenhouse)}
|
440 |
+
\]
|
441 |
|
442 |
+
- \(A\) = Albedo
|
443 |
+
- \(L\) = ความสว่างของดาว (W)
|
444 |
+
- \(\sigma\) = 5.67\times10^{-8}
|
445 |
+
- \(d\) = ระยะทาง (m)
|
446 |
|
447 |
**สูตรแรงโน้มถ่วงนิวตัน**
|
448 |
+
\(g = \frac{G M}{R^2}\)
|
449 |
+
(เราใช้สมมุติว่า \(M \propto R^3\) => \(g \propto R\))
|
450 |
"""
|
451 |
|
452 |
###################################
|
453 |
+
# ส���้าง UI ด้วย Gradio พร้อมรองรับ Dark Mode
|
454 |
###################################
|
455 |
css_code = """
|
456 |
+
/* ใช้ CSS Variables สำหรับรองรับ Light และ Dark Mode */
|
457 |
+
:root {
|
458 |
+
--background-color: #F9FBFF;
|
459 |
+
--text-color: #333333;
|
460 |
+
--title-color: #4A90E2;
|
461 |
+
--game-desc-bg: #ECF6FF;
|
462 |
+
--game-desc-border: #B3DAFF;
|
463 |
+
--btn-main-bg: #FFE066;
|
464 |
+
--btn-main-border: #FFCA28;
|
465 |
+
--output-bg: #FFFDF5;
|
466 |
+
--output-border: #FFE082;
|
467 |
+
--copy-btn-bg: #F06292;
|
468 |
+
--copy-btn-border: #E91E63;
|
469 |
+
}
|
470 |
+
|
471 |
+
@media (prefers-color-scheme: dark) {
|
472 |
+
:root {
|
473 |
+
--background-color: #121212;
|
474 |
+
--text-color: #FFFFFF;
|
475 |
+
--title-color: #BB86FC;
|
476 |
+
--game-desc-bg: #1F1F1F;
|
477 |
+
--game-desc-border: #333333;
|
478 |
+
--btn-main-bg: #BB86FC;
|
479 |
+
--btn-main-border: #3700B3;
|
480 |
+
--output-bg: #2C2C2C;
|
481 |
+
--output-border: #BB86FC;
|
482 |
+
--copy-btn-bg: #CF6679;
|
483 |
+
--copy-btn-border: #B00020;
|
484 |
+
}
|
485 |
+
}
|
486 |
+
|
487 |
body {
|
488 |
+
background-color: var(--background-color);
|
489 |
+
color: var(--text-color);
|
490 |
font-family: "Kanit", sans-serif;
|
491 |
}
|
492 |
#title {
|
493 |
+
color: var(--title-color);
|
494 |
text-align: center;
|
495 |
font-size: 2rem;
|
496 |
margin-top: 20px;
|
|
|
500 |
.game-desc {
|
501 |
margin: 0 auto;
|
502 |
width: 90%;
|
503 |
+
background-color: var(--game-desc-bg);
|
504 |
+
border: 2px dashed var(--game-desc-border);
|
505 |
border-radius: 10px;
|
506 |
padding: 15px;
|
507 |
+
color: var(--text-color);
|
508 |
margin-bottom: 20px;
|
509 |
}
|
510 |
.btn-main {
|
511 |
+
background-color: var(--btn-main-bg);
|
512 |
+
border: 2px solid var(--btn-main-border);
|
513 |
font-weight: bold;
|
514 |
font-size: 1.1rem;
|
515 |
padding: 10px 30px;
|
516 |
border-radius: 10px;
|
517 |
margin-right: 10px;
|
518 |
+
color: var(--text-color);
|
519 |
+
cursor: pointer;
|
520 |
+
}
|
521 |
+
.btn-main:hover {
|
522 |
+
opacity: 0.8;
|
523 |
}
|
524 |
#child-summary, #detail-th, #prompt1-en, #prompt2-en, #prompt3-en, #formula-box {
|
525 |
+
background-color: var(--output-bg);
|
526 |
+
border: 2px solid var(--output-border);
|
527 |
border-radius: 10px;
|
528 |
padding: 10px;
|
529 |
margin-bottom: 20px;
|
530 |
+
color: var(--text-color);
|
531 |
}
|
532 |
.copy-btn {
|
533 |
+
background-color: var(--copy-btn-bg);
|
534 |
+
border: 2px solid var(--copy-btn-border);
|
535 |
font-weight: bold;
|
536 |
font-size: 0.8rem;
|
537 |
padding: 5px 10px;
|
538 |
border-radius: 5px;
|
539 |
margin-top: 5px;
|
540 |
+
color: var(--text-color);
|
541 |
+
cursor: pointer;
|
542 |
+
}
|
543 |
+
.copy-btn:hover {
|
544 |
+
opacity: 0.8;
|
545 |
}
|
546 |
"""
|
547 |
|
|
|
592 |
oxygen_slider = gr.Slider(0, 100, step=1, value=21, label="% ออกซิเจน")
|
593 |
life_th = gr.Textbox(label="สิ่งมีชีวิต (ไทย)", placeholder="เช่น แมลงยักษ์เรืองแสง...")
|
594 |
|
595 |
+
with gr.Row():
|
596 |
+
style_selected = gr.Dropdown(
|
597 |
+
label="เลือกสไตล์ของพรอมพ์",
|
598 |
+
choices=style_options,
|
599 |
+
value="Standard"
|
600 |
+
)
|
601 |
+
|
602 |
create_btn = gr.Button("สร้างโลกแฟนตาซี", elem_classes="btn-main")
|
603 |
|
604 |
child_summary_out = gr.Textbox(label="สรุปสำหรับเด็ก (ไทย)", interactive=False, elem_id="child-summary")
|
|
|
630 |
gr.HTML(copy_button_html)
|
631 |
|
632 |
def generate_wrapper(
|
633 |
+
p_name_th, s_type_en, dist_au, dia_fac, tilt_val, moon_val, oxy_val, p_type_th, l_th, style_sel
|
634 |
):
|
635 |
return generate_planet_info(
|
636 |
planet_name_th=p_name_th,
|
|
|
641 |
moon_value=str(moon_val),
|
642 |
oxygen_percent=oxy_val,
|
643 |
planet_type_th=p_type_th,
|
644 |
+
life_th=l_th,
|
645 |
+
style_selected=style_sel
|
646 |
)
|
647 |
|
648 |
create_btn.click(
|
|
|
656 |
moon_slider,
|
657 |
oxygen_slider,
|
658 |
planet_type_th,
|
659 |
+
life_th,
|
660 |
+
style_selected
|
661 |
],
|
662 |
outputs=[
|
663 |
child_summary_out,
|