File size: 23,022 Bytes
e7c2a53
adc07a1
1e3d5d5
2a4265e
1e3d5d5
 
 
4ce32cc
 
 
 
e7c2a53
4452a6b
 
 
 
e7c2a53
 
4ce32cc
e7c2a53
4ce32cc
 
 
 
1e3d5d5
4452a6b
1e3d5d5
e7c2a53
 
4452a6b
 
 
e7c2a53
 
1e3d5d5
 
4452a6b
1e3d5d5
 
e7c2a53
 
1e3d5d5
 
 
4452a6b
e7c2a53
1e3d5d5
e7c2a53
 
1e3d5d5
e7c2a53
 
4452a6b
e7c2a53
 
 
1e3d5d5
4452a6b
1e3d5d5
 
e7c2a53
4452a6b
e7c2a53
 
adc07a1
1e3d5d5
4452a6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e3d5d5
e7c2a53
 
1e3d5d5
e7c2a53
1e3d5d5
e7c2a53
4452a6b
e7c2a53
1e3d5d5
e7c2a53
 
 
 
 
1e3d5d5
e7c2a53
1e3d5d5
adc07a1
e7c2a53
adc07a1
e7c2a53
 
1e3d5d5
e7c2a53
 
 
1e3d5d5
adc07a1
1e3d5d5
4ce32cc
e7c2a53
1e3d5d5
 
 
 
 
 
e7c2a53
 
adc07a1
e7c2a53
1e3d5d5
e7c2a53
 
 
 
1e3d5d5
4452a6b
1e3d5d5
 
 
4452a6b
 
 
 
 
 
 
 
 
 
1e3d5d5
4452a6b
1e3d5d5
4452a6b
 
 
 
 
4ce32cc
1e3d5d5
 
 
4ce32cc
 
4452a6b
4ce32cc
1e3d5d5
 
4ce32cc
 
4452a6b
4ce32cc
1e3d5d5
 
4ce32cc
 
e7c2a53
 
 
 
 
 
4ce32cc
1e3d5d5
4452a6b
1e3d5d5
adc07a1
e7c2a53
4452a6b
e7c2a53
 
1e3d5d5
 
e7c2a53
4452a6b
e7c2a53
adc07a1
1e3d5d5
adc07a1
 
 
 
 
 
 
 
 
 
e7c2a53
1e3d5d5
e7c2a53
 
adc07a1
e7c2a53
1e3d5d5
e7c2a53
1e3d5d5
 
4452a6b
1e3d5d5
 
 
4452a6b
 
 
 
 
6f46957
 
4452a6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adc07a1
4452a6b
 
 
 
 
 
 
 
 
 
6f46957
4452a6b
 
 
 
 
 
adc07a1
1e3d5d5
 
 
 
 
 
 
 
 
4452a6b
 
adc07a1
 
4452a6b
 
 
 
4ce32cc
 
 
4452a6b
 
1e3d5d5
e7c2a53
1e3d5d5
e7c2a53
1e3d5d5
4452a6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7c2a53
4452a6b
 
 
 
 
 
 
 
 
 
 
e7c2a53
4452a6b
1e3d5d5
e7c2a53
1e3d5d5
 
e7c2a53
1e3d5d5
e7c2a53
 
 
 
adc07a1
 
1e3d5d5
 
 
4452a6b
1e3d5d5
 
4452a6b
1e3d5d5
 
 
adc07a1
4452a6b
 
 
1e3d5d5
4ce32cc
4452a6b
 
 
1e3d5d5
 
 
4452a6b
1e3d5d5
adc07a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e3d5d5
adc07a1
 
 
 
 
 
 
 
1e3d5d5
 
 
adc07a1
1e3d5d5
4452a6b
e7c2a53
adc07a1
6f46957
adc07a1
 
6f46957
4452a6b
 
 
adc07a1
 
 
1e3d5d5
 
 
 
 
 
 
 
4452a6b
1e3d5d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adc07a1
 
 
e7c2a53
 
1e3d5d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4452a6b
1e3d5d5
 
 
adc07a1
 
1e3d5d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adc07a1
1e3d5d5
4ce32cc
1e3d5d5
 
4ce32cc
 
1e3d5d5
 
4ce32cc
1e3d5d5
4ce32cc
 
e7c2a53
 
 
 
 
adc07a1
 
1e3d5d5
6f46957
4452a6b
4ce32cc
adc07a1
 
1
2
3
4
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import math
import gradio as gr
from transformers import MarianTokenizer, MarianMTModel

###################################
# 1) โหลดโมเดล MarianMT (Thai->En)
###################################
model_name = "Helsinki-NLP/opus-mt-th-en"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)

def translate_th_to_en(text_th: str) -> str:
    """
    แปลไทย -> อังกฤษ ด้วย MarianMT บน CPU
    * เรียกเฉพาะส่วนที่ผู้ใช้พิมพ์ เช่น ชื่อดาว, ชื่อสิ่งมีชีวิต
    """
    text_th = text_th.strip()
    if not text_th:
        return ""
    inputs = tokenizer(text_th, return_tensors="pt", max_length=512, truncation=True)
    translation_tokens = model.generate(**inputs, max_length=512)
    en_text = tokenizer.decode(translation_tokens[0], skip_special_tokens=True)
    return en_text

###################################
# 2) สูตรอุณหภูมิ (Black Body) + Greenhouse
###################################
def approximate_temp_with_star(star_type, distance_au, albedo=0.3):
    """
    star_type: Red Dwarf / White Dwarf / Sun-like / Blue Giant / Supergiant
    distance_au: ระยะห่าง (AU)
    albedo: สะท้อนแสง (0.3)
    """
    if star_type == "Red Dwarf":
        lum_ratio = 0.02
    elif star_type == "White Dwarf":
        lum_ratio = 0.001
    elif star_type == "Sun-like":
        lum_ratio = 1.0
    elif star_type == "Blue Giant":
        lum_ratio = 10.0
    elif star_type == "Supergiant":
        lum_ratio = 100.0
    else:
        lum_ratio = 1.0

    luminosity = 3.828e26 * lum_ratio
    dist_m = distance_au * 1.496e11

    sigma = 5.67e-8
    T_k = ((1 - albedo) * luminosity / (16 * math.pi * sigma * dist_m**2))**0.25
    T_c = T_k - 273.15
    T_c += 15  # Greenhouse

    return round(T_c)

###################################
# 3) สูตรแรงโน้มถ่วง (สมมุติตามนิวตัน)
###################################
def approximate_gravity_nw(diameter_factor):
    """
    สมมุติว่ามวล ∝ R^3 => g ∝ R
    """
    return round(diameter_factor, 2)

###################################
# 4) Dictionary แปลล่วงหน้า (TH->EN)
###################################
# ลดการเรียก MarianMT สำหรับคำที่เกิดซ้ำ เช่น
# describe_distance, describe_temp, describe_gravity, describe_tilt, describe_moons, describe_oxygen

pretranslated_map = {
    # distance
    "โคจรใกล้ดาวฤกษ์มาก": "orbits extremely close to its star",
    "โคจรคล้ายโลกหรืออุ่นกว่านิดหน่อย": "orbits similarly to Earth or slightly warmer",
    "โคจรห่างพอประมาณ อากาศค่อนข้างเย็น": "orbits moderately far, quite cool climate",
    "โคจรไกลสุดขั้ว อากาศหนาวมาก": "orbits extremely far, very cold environment",

    # temp
    "อากาศหนาวแข็ง": "freezing cold",
    "เย็นสบาย": "mildly cool",
    "พอเหมาะกำลังดี": "pleasantly temperate",
    "ร้อนระอุ": "intensely hot",

    # gravity
    "โน้มถ่วงเบา (ลอยง่าย)": "light gravity (easy to float)",
    "คล้ายโลก": "similar to Earth",
    "หนักกว่าปกติ": "heavier than usual",
    "หนักมาก": "very heavy",

    # tilt
    "แทบไม่เอียง": "almost no axial tilt",
    "เอียงเล็กน้อย มีฤดูกาลเบาๆ": "slightly tilted, gentle seasons",
    "เอียงปานกลาง ฤดูกาลเปลี่ยนแปลง": "moderately tilted, noticeable seasonal changes",
    "เอียงมาก ฤดูกาลสุดขั้ว": "highly tilted, extreme seasonal shifts",

    # moons (prefix check some conditions)
    # => We'll dynamically generate string TH -> EN
}

def describe_distance_th_to_en(desc_th):
    return pretranslated_map.get(desc_th, "unknown distance environment")

def describe_temp_th_to_en(desc_th):
    return pretranslated_map.get(desc_th, "unknown temperature")

def describe_gravity_th_to_en(desc_th):
    return pretranslated_map.get(desc_th, "unknown gravity")

def describe_tilt_th_to_en(desc_th):
    return pretranslated_map.get(desc_th, "unknown axial tilt")

###################################
# ฟังก์ชันบรรยาย (ภาษาไทย)
###################################
def describe_distance(distance_au):
    if distance_au < 0.5:
        return "โคจรใกล้ดาวฤกษ์มาก"
    elif distance_au < 1.2:
        return "โคจรคล้ายโลกหรืออุ่นกว่านิดหน่อย"
    elif distance_au < 2.5:
        return "โคจรห่างพอประมาณ อากาศค่อนขั้วเย็น"  # อาจแก้เป็น "ค่อนข้างเย็น"
    else:
        return "โคจรไกลสุดขั้ว อากาศหนาวมาก"

def describe_temp(temp_c):
    if temp_c < -30:
        return "อากาศหนาวแข็ง"
    elif temp_c < 10:
        return "เย็นสบาย"
    elif temp_c < 35:
        return "พอเหมาะกำลังดี"
    else:
        return "ร้อนระอุ"

def describe_gravity(g):
    if g < 0.5:
        return "โน้มถ่วงเบา (ลอยง่าย)"
    elif g < 1.2:
        return "คล้ายโลก"
    elif g < 2.0:
        return "หนักกว่าปกติ"
    else:
        return "หนักมาก"

def describe_tilt(tilt_deg):
    if tilt_deg < 5:
        return "แทบไม่เอียง"
    elif tilt_deg < 25:
        return "เอียงเล็กน้อย มีฤดูกาลเบาๆ"
    elif tilt_deg < 45:
        return "เอียงปานกลาง ฤดูกาลเปลี่ยนแปลง"
    else:
        return "เอียงมาก ฤดูกาลสุดขั้ว"

def describe_moons(n):
    if n <= 0:
        return "ไม่มีดวงจันทร์"
    elif n == 1:
        return "มีดวงจันทร์หนึ่งดวง"
    else:
        if n > 2:
            return f"มีดวงจันทร์ {n} ดวง (ทำให้เกิดน้ำขึ้นน้ำลงรุนแรง)"
        else:
            return f"มีดวงจันทร์ {n} ดวง"

def describe_oxygen(oxygen_percent):
    if oxygen_percent < 1:
        return "ไม่มีออกซิเจน"
    elif oxygen_percent < 10:
        return "ออกซิเจนน้อย"
    elif oxygen_percent < 25:
        return "ออกซิเจนพอเหมาะ"
    else:
        return "ออกซิเจนสูง"

###################################
# 5) สร้าง Prompt 3 แบบ (Pre-translate dictionary)
###################################
def build_prompts_en(
    planet_name_en, star_type_en,
    dist_desc_en, temp_desc_en, grav_desc_en, tilt_desc_en, moon_desc_en, oxygen_desc_en, life_en
):
    # Prompt 1
    prompt1 = (
        f"A vibrant space painting of planet '{planet_name_en}' orbiting a {star_type_en}. "
        f"It is {dist_desc_en}, with {temp_desc_en} conditions and {grav_desc_en} gravity. "
        f"{tilt_desc_en}, {moon_desc_en}, atmosphere has {oxygen_desc_en}. Cinematic details."
    )

    # Prompt 2
    prompt2 = (
        f"On planet '{planet_name_en}', we discover {life_en} thriving in {temp_desc_en} weather, "
        f"{grav_desc_en} pull, and {oxygen_desc_en} in the air. Surreal alien ecosystem, rich concept art."
    )

    # Prompt 3
    prompt3 = (
        f"Exploring the surface of '{planet_name_en}': {temp_desc_en} climate, {grav_desc_en}, "
        f"{tilt_desc_en} tilt, and {moon_desc_en}. Epic environment design, atmospheric perspective."
    )

    prompt_all = (
        "--- Prompt #1 ---\n" + prompt1 + "\n\n"
        "--- Prompt #2 ---\n" + prompt2 + "\n\n"
        "--- Prompt #3 ---\n" + prompt3 + "\n"
    )
    return prompt_all

###################################
# 6) ฟังก์ชันหลัก generate_planet_info
###################################
def generate_planet_info(
    planet_name_th,
    star_type_en,       
    distance_str,
    diameter_str,
    tilt_value,
    moon_value,
    oxygen_percent,
    planet_type_th,  
    life_th
):
    # parse
    try:
        distance_au = float(distance_str)
    except:
        distance_au = 1.0

    try:
        diameter_factor = float(diameter_str)
    except:
        diameter_factor = 1.0

    try:
        tilt_deg = float(tilt_value)
    except:
        tilt_deg = 23.5

    try:
        moon_count = int(moon_value)
    except:
        moon_count = 1

    # คำนวณ
    temp_c = approximate_temp_with_star(star_type_en, distance_au)
    g_approx = approximate_gravity_nw(diameter_factor)

    # -----------------------------
    # (A) สรุปสำหรับเด็ก (ไทย)
    # -----------------------------
    child_summary = ""
    child_summary += f"สวัสดีหนูน้อยนักสำรวจ!\n\n"
    child_summary += f"ดาว {planet_name_th} ที่หนูสร้าง เป็น{planet_type_th} "
    child_summary += f"โคจรรอบดาวฤกษ์ {star_type_en}\n"

    child_summary += f"ระยะห่าง ~{distance_au} AU => ประเมินอุณหภูมิ ~{temp_c}°C\n"
    if temp_c < -20:
        child_summary += "หนาวจัดเลยทีเดียว!\n"
    elif temp_c > 35:
        child_summary += "ร้อนระอุทีเดียว!\n"
    else:
        child_summary += "อากาศดูพอเหมาะนะ!\n"

    child_summary += f"แรงโน้มถ่วง ~{g_approx}g"
    if g_approx < 0.5:
        child_summary += " (ลอยง่าย!)"
    elif g_approx > 1.5:
        child_summary += " (หนักเดินลำบาก!)"
    child_summary += "\n"

    child_summary += f"แกนเอียง {tilt_deg}° => "
    if tilt_deg > 45:
        child_summary += "ฤดูกาลแปรปรวนสุดขั้ว!\n"
    elif tilt_deg < 5:
        child_summary += "แทบไม่เปลี่ยนฤดูกาล\n"
    else:
        child_summary += "มีฤดูกาลตามองศาเอียง\n"

    child_summary += f"มีดวงจันทร์ {moon_count} ดวง\n"
    if moon_count > 2:
        child_summary += "น้ำขึ้นน้ำลงคงอลังการมาก\n"

    child_summary += f"ออกซิเจน ~{oxygen_percent}%\n"
    if oxygen_percent < 5:
        child_summary += "น้อยมาก ต้องใช้ชุดอวกาศเลย\n"
    elif oxygen_percent > 30:
        child_summary += "สูงมาก ระวังไฟติดง่ายนะ\n"

    child_summary += f"สิ่งมีชีวิต: {life_th}\n"
    child_summary += "ลองคิดดูสิว่าหน้าตาจะเป็นยังไง!\n\n"
    child_summary += "พร้อมลุยกันแล้วหรือยัง!?"

    # -----------------------------
    # (B) รายละเอียด (ไทย)
    # -----------------------------
    detail_th = (
        f"ชื่อดาวเคราะห์(ไทย): {planet_name_th}\n"
        f"ประเภทดาวฤกษ์: {star_type_en}\n"
        f"ระยะ (AU): {distance_au}\n"
        f"ขนาด (เท่าโลก): {diameter_factor}\n"
        f"แกนเอียง: {tilt_deg}°\n"
        f"จำนวนดวงจันทร์: {moon_count}\n"
        f"เปอร์เซ็นต์ O2: {oxygen_percent}%\n"
        f"ชนิดดาวเคราะห์(ไทย): {planet_type_th}\n"
        f"สิ่งมีชีวิต(ไทย): {life_th}\n"
        f"อุณหภูมิ: ~{temp_c} °C\n"
        f"แรงโน้มถ่วง (สมมุติ): ~{g_approx} g\n"
    )

    # -----------------------------
    # (C) สร้าง Prompt อังกฤษ 3 แบบ
    # -----------------------------
    # 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input)
    planet_name_en = translate_th_to_en(planet_name_th)
    life_en = translate_th_to_en(life_th)

    # 2) อธิบาย distance/temp/gravity/tilt/moon/oxygen เป็นภาษาไทย -> Map to EN
    #    ไม่ต้องเรียก MarianMT เพราะเราใช้ pretranslated map
    dist_desc_th = describe_distance(distance_au)
    temp_desc_th = describe_temp(temp_c)
    grav_desc_th = describe_gravity(g_approx)
    tilt_desc_th = describe_tilt(tilt_deg)
    moon_desc_th = describe_moons(moon_count)
    o2_desc_th = describe_oxygen(oxygen_percent)

    # แปลงผ่าน Dictionary (ถ้าไม่เจอ key, fallback "unknown")
    # distance
    dist_desc_en = pretranslated_map.get(dist_desc_th, "unknown distance")
    # temperature
    temp_desc_en = pretranslated_map.get(temp_desc_th, "unknown temperature")
    # gravity
    grav_desc_en = pretranslated_map.get(grav_desc_th, "unknown gravity")
    # tilt
    tilt_desc_en = pretranslated_map.get(tilt_desc_th, "unknown tilt")

    # moons (กรณีมีจำนวนระบุใน string)
    # "มีดวงจันทร์ x ดวง" -> Pre-translate ประโยคแบบ dynamic?
    # เราอาจ simplify: if "dวง" in moon_desc_th => split
    if "ไม่มีดวงจันทร์" in moon_desc_th:
        moon_desc_en = "no moons"
    elif "หนึ่งดวง" in moon_desc_th:
        moon_desc_en = "one moon"
    elif "ทำให้เกิดน้ำขึ้นน้ำลงรุนแรง" in moon_desc_th:
        # e.g. "มีดวงจันทร์ 4 ดวง (ทำให้เกิด...)"
        # split out the number
        import re
        match = re.search(r"(\d+) ดวง", moon_desc_th)
        if match:
            n_str = match.group(1)
            moon_desc_en = f"{n_str} moons causing powerful tides"
        else:
            moon_desc_en = "multiple moons causing stronger tides"
    elif "มีดวงจันทร์ " in moon_desc_th:
        # e.g. "มีดวงจันทร์ 2 ดวง"
        match = re.search(r"(\d+) ดวง", moon_desc_th)
        if match:
            n_str = match.group(1)
            moon_desc_en = f"{n_str} moons"
        else:
            moon_desc_en = "some moons"
    else:
        moon_desc_en = "unknown moons"

    # oxygen
    if o2_desc_th == "ไม่มีออกซิเจน":
        oxygen_desc_en = "no oxygen"
    elif o2_desc_th == "ออกซิเจนน้อย":
        oxygen_desc_en = "low oxygen"
    elif o2_desc_th == "ออกซิเจนพอเหมาะ":
        oxygen_desc_en = "moderate oxygen"
    else:
        oxygen_desc_en = "high oxygen"

    # 3) build prompts
    prompts_en = build_prompts_en(
        planet_name_en,
        star_type_en + " star",
        dist_desc_en,
        temp_desc_en,
        grav_desc_en,
        tilt_desc_en,
        moon_desc_en,
        oxygen_desc_en,
        life_en
    )

    return child_summary, detail_th, prompts_en

###################################
# สูตรโชว์
###################################
formula_text = r"""
**สูตรอุณหภูมิ (Stefan-Boltzmann) แบบง่าย**  
\\[
T = \left(\frac{(1 - A) \times L}{16 \pi \sigma \, d^2}\right)^{\frac{1}{4}} - 273.15 + 15^\circ\text{C (Greenhouse)}
\\]

- \\(A\\) = Albedo  
- \\(L\\) = ความสว่างของดาว (W)  
- \\(\sigma\\) = 5.67\\times10^{-8}  
- \\(d\\) = ระยะทาง (m)

**สูตรแรงโน้มถ่วงนิวตัน**  
\\(g = \frac{G M}{R^2}\\)  
(เราใช้สมมุติว่า \\(M \propto R^3\\) => \\(g \propto R\\))
"""

###################################
# สร้าง UI ด้วย Gradio
###################################
css_code = """
body {
    background-color: #F9FBFF;
    font-family: "Kanit", sans-serif;
}
#title {
    color: #4A90E2;
    text-align: center;
    font-size: 2rem;
    margin-top: 20px;
    margin-bottom: 10px;
    font-weight: bold;
}
.game-desc {
    margin: 0 auto;
    width: 90%;
    background-color: #ECF6FF;
    border: 2px dashed #B3DAFF;
    border-radius: 10px;
    padding: 15px;
    color: #333;
    margin-bottom: 20px;
}
.btn-main {
    background-color: #FFE066;
    border: 2px solid #FFCA28;
    font-weight: bold;
    font-size: 1.1rem;
    padding: 10px 30px;
    border-radius: 10px;
    margin-right: 10px;
}
#child-summary, #detail-th, #prompt-en, #formula-box {
    background-color: #FFFDF5;
    border: 2px solid #FFE082;
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 20px;
}
"""

def show_formula(state):
    new_state = not state
    return new_state, gr.update(visible=new_state)

def welcome_text():
    return "ยินดีต้อนรับสู่ Planetary Adventure++ (Optimized)! ลองกรอกข้อมูลแล้วกด 'สร้างโลกแฟนตาซี' สิ!"

with gr.Blocks(css=css_code) as demo:
    gr.Markdown("<h1 id='title'>ZenityX Planetary Adventure</h1>")
    gr.Markdown("""
    <div class="game-desc">
        <p>(พัฒนาโดยสถาบัน ZenityX AI Studio)</p>
        <p>ใส่ข้อมูล แล้วกด <strong>"สร้างโลกแฟนตาซี"</strong> เพื่อดู 
           <em>สรุปสำหรับเด็ก</em>, <em>รายละเอียด (ไทย)</em>, และ <em>3 Prompts อังกฤษ</em></p>
        <p>ถ้าอยากดูสูตรคำนวณ กดปุ่ม "โชว์สูตรคำนวณ" ได้เลย!</p>
    </div>
    """)

    formula_state = gr.State(value=False)
    formula_md = gr.Markdown(formula_text, visible=False, elem_id="formula-box")

    show_formula_btn = gr.Button("โชว์สูตรคำนวณ")
    show_formula_btn.click(fn=show_formula, inputs=formula_state, outputs=[formula_state, formula_md])

    with gr.Row():
        with gr.Column():
            planet_name_th = gr.Textbox(label="ชื่อดาวเคราะห์ (ไทย)", placeholder="ดาวซานาดา, ดาวโซลาริส...")
            star_type_en = gr.Dropdown(
                label="ประเภทดาวฤกษ์",
                choices=["Red Dwarf", "White Dwarf", "Sun-like", "Blue Giant", "Supergiant"],
                value="Sun-like"
            )
            distance_au = gr.Textbox(label="ระยะห่าง (AU)", placeholder="1, 0.5, 2...")
            diameter_factor = gr.Textbox(label="ขนาด (เท่าโลก)", placeholder="1, 2, 0.5...")

        with gr.Column():
            tilt_slider = gr.Slider(0, 90, step=1, value=23.5, label="แกนเอียง (องศา)")
            moon_slider = gr.Slider(0, 10, step=1, value=1, label="จำนวนดวงจันทร์")
            oxygen_slider = gr.Slider(0, 100, step=1, value=21, label="% ออกซิเจน")
            planet_type_th = gr.Dropdown(
                label="ชนิดดาวเคราะห์ (ไทย)",
                choices=["ดาวหิน", "ดาวก๊าซ", "ดาวน้ำแข็ง"],
                value="ดาวหิน"
            )
            life_th = gr.Textbox(label="สิ่งมีชีวิต (ไทย)", placeholder="แมลงยักษ์เรืองแสง...")

    create_btn = gr.Button("สร้างโลกแฟนตาซี", elem_classes="btn-main")

    child_summary_out = gr.Textbox(label="สรุปสำหรับเด็ก (ไทย)", interactive=False, elem_id="child-summary")
    detail_th_out = gr.Textbox(label="รายละเอียด (ไทย)", interactive=False, elem_id="detail-th")
    prompt_en_out = gr.Textbox(label="Prompts (English)", interactive=False, elem_id="prompt-en")

    copy_button_html = """
    <button style="background-color: #F06292; border: 2px solid #E91E63; font-weight: bold; 
                   font-size: 1.0rem; padding: 8px 20px; border-radius: 10px;"
            onclick="copyPromptText()">
      คัดลอก Prompt
    </button>
    <script>
    function copyPromptText() {
      const promptBox = document.querySelector('#prompt-en textarea');
      if (!promptBox) {
        alert('ไม่พบข้อความ Prompt!');
        return;
      }
      const promptText = promptBox.value;
      navigator.clipboard.writeText(promptText);
      alert('คัดลอก Prompt แล้ว! นำไปใช้กับ AI สร้างภาพได้เลย~');
    }
    </script>
    """
    gr.HTML(copy_button_html)

    def generate_wrapper(
        p_name_th, s_type_en, dist_au, dia_fac, tilt_val, moon_val, oxy_val, p_type_th, l_th
    ):
        return generate_planet_info(
            planet_name_th=p_name_th,
            star_type_en=s_type_en,
            distance_str=dist_au,
            diameter_str=dia_fac,
            tilt_value=str(tilt_val),
            moon_value=str(moon_val),
            oxygen_percent=oxy_val,
            planet_type_th=p_type_th,
            life_th=l_th
        )

    create_btn.click(
        fn=generate_wrapper,
        inputs=[
            planet_name_th,
            star_type_en,
            distance_au,
            diameter_factor,
            tilt_slider,
            moon_slider,
            oxygen_slider,
            planet_type_th,
            life_th
        ],
        outputs=[
            child_summary_out,
            detail_th_out,
            prompt_en_out
        ]
    )

    def welcome_text():
        return "ยินดีต้อนรับสู่ ZenityX Planetary Adventure! ลองกรอกข้อมูลแล้วกด 'สร้างโลกแฟนตาซี' สิ!"

    demo.load(fn=welcome_text, inputs=None, outputs=child_summary_out)

demo.launch()