zenityx commited on
Commit
4452a6b
·
verified ·
1 Parent(s): 1e3d5d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +209 -83
app.py CHANGED
@@ -10,6 +10,10 @@ tokenizer = MarianTokenizer.from_pretrained(model_name)
10
  model = MarianMTModel.from_pretrained(model_name)
11
 
12
  def translate_th_to_en(text_th: str) -> str:
 
 
 
 
13
  text_th = text_th.strip()
14
  if not text_th:
15
  return ""
@@ -19,19 +23,18 @@ def translate_th_to_en(text_th: str) -> str:
19
  return en_text
20
 
21
  ###################################
22
- # 2) สูตรคำนวณอุณหภูมิ (Black Body)
23
  ###################################
24
  def approximate_temp_with_star(star_type, distance_au, albedo=0.3):
25
  """
26
- - star_type: เพิ่ม White Dwarf, Supergiant
27
- - distance_au: ระยะห่าง (AU)
28
- - albedo: สัดส่วนสะท้อน (0.3)
29
- - กลไก: Stefan-Boltzmann + greenhouse + lum_ratio
30
  """
31
  if star_type == "Red Dwarf":
32
  lum_ratio = 0.02
33
  elif star_type == "White Dwarf":
34
- lum_ratio = 0.001 # สมมุติ (white dwarf มักมี luminosity ต่ำ)
35
  elif star_type == "Sun-like":
36
  lum_ratio = 1.0
37
  elif star_type == "Blue Giant":
@@ -39,7 +42,7 @@ def approximate_temp_with_star(star_type, distance_au, albedo=0.3):
39
  elif star_type == "Supergiant":
40
  lum_ratio = 100.0
41
  else:
42
- lum_ratio = 1.0 # default
43
 
44
  luminosity = 3.828e26 * lum_ratio
45
  dist_m = distance_au * 1.496e11
@@ -47,26 +50,68 @@ def approximate_temp_with_star(star_type, distance_au, albedo=0.3):
47
  sigma = 5.67e-8
48
  T_k = ((1 - albedo) * luminosity / (16 * math.pi * sigma * dist_m**2))**0.25
49
  T_c = T_k - 273.15
50
- T_c += 15 # สมมุติ Greenhouse
51
 
52
  return round(T_c)
53
 
54
  ###################################
55
- # 3) สูตรแรงโน้มถ่วงตามกฎนิวตัน (สมมุติ)
56
  ###################################
57
  def approximate_gravity_nw(diameter_factor):
58
  """
59
- ถ้าคิดว่าดาวเคราะห์มีความหนาแน่นเท่าโลก:
60
- - มวล (M) ~ (diameter_factor^3) (เพราะ R^3 => Volume)
61
- - รัศมี (R) ~ diameter_factor
62
- - g ~ GM/R^2 ~ R^3 / R^2 = R (ถ้า density คงที่)
63
- ดังนั้น g ~ diameter_factor (เหมือนเดิม)
64
- แต่เราจะใส่ “สูตรจริง” ในปุ่มโชว์สูตร
65
  """
66
  return round(diameter_factor, 2)
67
 
68
  ###################################
69
- # 4) ตีความเชิงบรรยาย (ภาษาไทย)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  ###################################
71
  def describe_distance(distance_au):
72
  if distance_au < 0.5:
@@ -74,7 +119,7 @@ def describe_distance(distance_au):
74
  elif distance_au < 1.2:
75
  return "โคจรคล้ายโลกหรืออุ่นกว่านิดหน่อย"
76
  elif distance_au < 2.5:
77
- return "โคจรห่างพอประมาณ อากาศค่อนข้างเย็น"
78
  else:
79
  return "โคจรไกลสุดขั้ว อากาศหนาวมาก"
80
 
@@ -114,27 +159,42 @@ def describe_moons(n):
114
  elif n == 1:
115
  return "มีดวงจันทร์หนึ่งดวง"
116
  else:
117
- # ถ้ามีเยอะ => tides effect
118
  if n > 2:
119
- return f"มีดวงจันทร์ {n} ดวง ซึ่งทำให้ปรากฏการณ์น้ำขึ้นน้ำลงรุนแรง"
120
  else:
121
  return f"มีดวงจันทร์ {n} ดวง"
122
 
 
 
 
 
 
 
 
 
 
 
123
  ###################################
124
- # 5) สร้าง Prompt 3 แบบ (ไม่มีตัวเลข)
125
  ###################################
126
- 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):
 
 
 
 
127
  prompt1 = (
128
  f"A vibrant space painting of planet '{planet_name_en}' orbiting a {star_type_en}. "
129
  f"It is {dist_desc_en}, with {temp_desc_en} conditions and {grav_desc_en} gravity. "
130
  f"{tilt_desc_en}, {moon_desc_en}, atmosphere has {oxygen_desc_en}. Cinematic details."
131
  )
132
 
 
133
  prompt2 = (
134
  f"On planet '{planet_name_en}', we discover {life_en} thriving in {temp_desc_en} weather, "
135
  f"{grav_desc_en} pull, and {oxygen_desc_en} in the air. Surreal alien ecosystem, rich concept art."
136
  )
137
 
 
138
  prompt3 = (
139
  f"Exploring the surface of '{planet_name_en}': {temp_desc_en} climate, {grav_desc_en}, "
140
  f"{tilt_desc_en} tilt, and {moon_desc_en}. Epic environment design, atmospheric perspective."
@@ -148,17 +208,17 @@ def build_prompts_en(planet_name_en, star_type_en, dist_desc_en, temp_desc_en, g
148
  return prompt_all
149
 
150
  ###################################
151
- # 6) ฟังก์ชันหลัก
152
  ###################################
153
  def generate_planet_info(
154
  planet_name_th,
155
- star_type_en, # Red Dwarf / White Dwarf / Sun-like / Blue Giant / Supergiant
156
  distance_str,
157
  diameter_str,
158
  tilt_value,
159
  moon_value,
160
  oxygen_percent,
161
- planet_type_th, # (ดาวหิน, ดาวก๊าซ, ดาวน้ำแข็ง)
162
  life_th
163
  ):
164
  # parse
@@ -182,22 +242,58 @@ def generate_planet_info(
182
  except:
183
  moon_count = 1
184
 
185
- # คำนวณอุณหภูมิ
186
  temp_c = approximate_temp_with_star(star_type_en, distance_au)
187
- # คำนวณแรงโน้มถ่วงแบบนิวตันสมมุติ (R^3 => M, g => M/R^2 => R)
188
  g_approx = approximate_gravity_nw(diameter_factor)
189
 
190
- # สรุปสำหรับเด็ก (ภาษาไทย)
191
- child_summary = (
192
- f"ดาว {planet_name_th} เป็��{planet_type_th} โคจรรอบดาวฤกษ์ {star_type_en}\n"
193
- f"ระยะ ~{distance_au} AU => อุณหภูมิราว {temp_c}°C,\n"
194
- f"แกนเอียง {tilt_deg}°, ดวงจันทร์ {moon_count}, "
195
- f"แรงโน้มถ่วง ~{g_approx}g, O2 ~{oxygen_percent}%\n"
196
- f"มีสิ่งมีชีวิต: {life_th}\n"
197
- f"น่าตื่นเต้นมาก!"
198
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
- # รายละเอียด
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  detail_th = (
202
  f"ชื่อดาวเคราะห์(ไทย): {planet_name_th}\n"
203
  f"ประเภทดาวฤกษ์: {star_type_en}\n"
@@ -208,42 +304,75 @@ def generate_planet_info(
208
  f"เปอร์เซ็นต์ O2: {oxygen_percent}%\n"
209
  f"ชนิดดาวเคราะห์(ไทย): {planet_type_th}\n"
210
  f"สิ่งมีชีวิต(ไทย): {life_th}\n"
211
- f"อุณหภูมิ (สูตร SB): {temp_c} °C\n"
212
- f"แรงโน้มถ่วง (สมมุติ R^3 => M => g): ~{g_approx} g\n"
213
  )
214
 
215
- # แปลชื่อดาว, สิ่งมีชีวิต
 
 
 
216
  planet_name_en = translate_th_to_en(planet_name_th)
217
  life_en = translate_th_to_en(life_th)
218
 
219
- # ตีความ distance, temp, grav, tilt, moon, oxygen
 
220
  dist_desc_th = describe_distance(distance_au)
221
- dist_desc_en = translate_th_to_en(dist_desc_th)
222
-
223
  temp_desc_th = describe_temp(temp_c)
224
- temp_desc_en = translate_th_to_en(temp_desc_th)
225
-
226
  grav_desc_th = describe_gravity(g_approx)
227
- grav_desc_en = translate_th_to_en(grav_desc_th)
228
-
229
  tilt_desc_th = describe_tilt(tilt_deg)
230
- tilt_desc_en = translate_th_to_en(tilt_desc_th)
231
-
232
  moon_desc_th = describe_moons(moon_count)
233
- moon_desc_en = translate_th_to_en(moon_desc_th)
234
-
235
- # ตีความ O2
236
- if oxygen_percent < 1:
237
- o2_desc_th = "ไม่มีออกซิเจน"
238
- elif oxygen_percent < 10:
239
- o2_desc_th = "ออกซิเจนน้อย"
240
- elif oxygen_percent < 25:
241
- o2_desc_th = "ออกซิเจนพอเหมาะ"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  else:
243
- o2_desc_th = "ออกซิเจนสูง"
244
- oxygen_desc_en = translate_th_to_en(o2_desc_th)
 
 
 
 
 
 
 
 
 
245
 
246
- # สร้าง 3 Prompts
247
  prompts_en = build_prompts_en(
248
  planet_name_en,
249
  star_type_en + " star",
@@ -259,29 +388,26 @@ def generate_planet_info(
259
  return child_summary, detail_th, prompts_en
260
 
261
  ###################################
262
- # สูตร (Markdown) เพิ่มเนื้อหา
263
  ###################################
264
  formula_text = r"""
265
- **สูตรอุณหภูมิ (Stefan-Boltzmann)**
266
-
267
  \\[
268
  T = \left(\frac{(1 - A) \times L}{16 \pi \sigma \, d^2}\right)^{\frac{1}{4}} - 273.15 + 15^\circ\text{C (Greenhouse)}
269
  \\]
270
 
271
- - \\(A\\) = Albedo
272
- - \\(L\\) = ความสว่างของดาว (W)
273
- - \\(\sigma\\) = 5.67\\times10^{-8} (ค่าคงที่ Stefan-Boltzmann)
274
  - \\(d\\) = ระยะทาง (m)
275
 
276
- **สูตรแรงโน้มถ่วงนิวตัน**: \\(g = \frac{GM}{R^2}\\)
277
-
278
- ถ้าสมมติความหนาแน่นเท่าโลก => \\(M \propto R^3\\) => \\(g \propto \frac{R^3}{R^2} = R\\)
279
-
280
- *(ในโค้ดเราประยุกต์ใช้ค่าสัดส่วนง่าย ๆ)*
281
  """
282
 
283
  ###################################
284
- # 7) สร้าง UI (Gradio)
285
  ###################################
286
  css_code = """
287
  body {
@@ -329,16 +455,17 @@ def show_formula(state):
329
  return new_state, gr.update(visible=new_state)
330
 
331
  def welcome_text():
332
- return "ยินดีต้อนรับสู่ Planetary Adventure++! ลองกรอกข้อมูลแล้วกด 'สร้างโลกแฟนตาซี' สิ!"
333
 
334
  with gr.Blocks(css=css_code) as demo:
335
- gr.Markdown("<h1 id='title'>ZenityX Planetary Adventure</h1>")
336
  gr.Markdown("""
337
  <div class="game-desc">
338
- <p>เพิ่มประเภทดาวฤกษ์ (White Dwarf, Supergiant), ใส่เงื่อนไขน้ำขึ้นน้ำลง (tides) ถ้าดวงจันทร์เยอะ,
339
- และใช้สูตรแรงโน้มถ่วงนิวตัน (สมมุติ).</p>
340
- <p>กรอกข้อมูลแล้วกด <strong>"สร้างโลกแฟนตาซี"</strong>!</p>
341
- <p>หากอยากดู <strong>สูตรคำนวณ</strong> ให้กดปุ่ม "โชว์สูตรคำนวณ" ด้านล่าง</p>
 
342
  </div>
343
  """)
344
 
@@ -350,7 +477,7 @@ with gr.Blocks(css=css_code) as demo:
350
 
351
  with gr.Row():
352
  with gr.Column():
353
- planet_name_th = gr.Textbox(label="ชื่อดาวเคราะห์ (ไทย)", placeholder="เช่น: ดาวซานาดา")
354
  star_type_en = gr.Dropdown(
355
  label="ประเภทดาวฤกษ์",
356
  choices=["Red Dwarf", "White Dwarf", "Sun-like", "Blue Giant", "Supergiant"],
@@ -363,7 +490,6 @@ with gr.Blocks(css=css_code) as demo:
363
  tilt_slider = gr.Slider(0, 90, step=1, value=23.5, label="แกนเอียง (องศา)")
364
  moon_slider = gr.Slider(0, 10, step=1, value=1, label="จำนวนดวงจันทร์")
365
  oxygen_slider = gr.Slider(0, 100, step=1, value=21, label="% ออกซิเจน")
366
-
367
  planet_type_th = gr.Dropdown(
368
  label="ชนิดดาวเคราะห์ (ไทย)",
369
  choices=["ดาวหิน", "ดาวก๊าซ", "ดาวน้ำแข็ง"],
@@ -392,7 +518,7 @@ with gr.Blocks(css=css_code) as demo:
392
  }
393
  const promptText = promptBox.value;
394
  navigator.clipboard.writeText(promptText);
395
- alert('คัดลอก Prompt แล้ว! ใช้งานกับ AI สร้างภาพได้เลย~');
396
  }
397
  </script>
398
  """
@@ -434,8 +560,8 @@ with gr.Blocks(css=css_code) as demo:
434
  )
435
 
436
  def welcome_text():
437
- return "ยินดีต้อนรับสู่ Planetary Adventure++! ลองกรอกข้อมูลแล้วกด 'สร้างโลกแฟนตาซี' สิ!"
438
-
439
  demo.load(fn=welcome_text, inputs=None, outputs=child_summary_out)
440
 
441
  demo.launch()
 
10
  model = MarianMTModel.from_pretrained(model_name)
11
 
12
  def translate_th_to_en(text_th: str) -> str:
13
+ """
14
+ แปลไทย -> อังกฤษ ด้วย MarianMT บน CPU
15
+ * เรียกเฉพาะส่วนที่ผู้ใช้พิมพ์ เช่น ชื่อดาว, ชื่อสิ่งมีชีวิต
16
+ """
17
  text_th = text_th.strip()
18
  if not text_th:
19
  return ""
 
23
  return en_text
24
 
25
  ###################################
26
+ # 2) สูตรอุณหภูมิ (Black Body) + Greenhouse
27
  ###################################
28
  def approximate_temp_with_star(star_type, distance_au, albedo=0.3):
29
  """
30
+ star_type: Red Dwarf / White Dwarf / Sun-like / Blue Giant / Supergiant
31
+ distance_au: ระยะห่าง (AU)
32
+ albedo: สะท้อนแสง (0.3)
 
33
  """
34
  if star_type == "Red Dwarf":
35
  lum_ratio = 0.02
36
  elif star_type == "White Dwarf":
37
+ lum_ratio = 0.001
38
  elif star_type == "Sun-like":
39
  lum_ratio = 1.0
40
  elif star_type == "Blue Giant":
 
42
  elif star_type == "Supergiant":
43
  lum_ratio = 100.0
44
  else:
45
+ lum_ratio = 1.0
46
 
47
  luminosity = 3.828e26 * lum_ratio
48
  dist_m = distance_au * 1.496e11
 
50
  sigma = 5.67e-8
51
  T_k = ((1 - albedo) * luminosity / (16 * math.pi * sigma * dist_m**2))**0.25
52
  T_c = T_k - 273.15
53
+ T_c += 15 # Greenhouse
54
 
55
  return round(T_c)
56
 
57
  ###################################
58
+ # 3) สูตรแรงโน้มถ่วง (สมมุติตามนิวตัน)
59
  ###################################
60
  def approximate_gravity_nw(diameter_factor):
61
  """
62
+ สมมุติว่ามวล ∝ R^3 => g ∝ R
 
 
 
 
 
63
  """
64
  return round(diameter_factor, 2)
65
 
66
  ###################################
67
+ # 4) Dictionary แปลล่วงหน้า (TH->EN)
68
+ ###################################
69
+ # ลดการเรียก MarianMT สำหรับคำที่เกิดซ้ำ เช่น
70
+ # describe_distance, describe_temp, describe_gravity, describe_tilt, describe_moons, describe_oxygen
71
+
72
+ pretranslated_map = {
73
+ # distance
74
+ "โคจรใกล้ดาวฤกษ์มาก": "orbits extremely close to its star",
75
+ "โคจรคล้ายโลกหรืออุ่นกว่านิดหน่อย": "orbits similarly to Earth or slightly warmer",
76
+ "โคจรห่างพอประมาณ อากาศค่อนข้างเย็น": "orbits moderately far, quite cool climate",
77
+ "โคจรไกลสุดขั้ว อากาศหนาวมาก": "orbits extremely far, very cold environment",
78
+
79
+ # temp
80
+ "อากาศหนาวแข็ง": "freezing cold",
81
+ "เย็นสบาย": "mildly cool",
82
+ "พอเหมาะกำลังดี": "pleasantly temperate",
83
+ "ร้อนระอุ": "intensely hot",
84
+
85
+ # gravity
86
+ "โน้มถ่วงเบา (ลอยง่าย)": "light gravity (easy to float)",
87
+ "คล้ายโลก": "similar to Earth",
88
+ "หนักกว่าปกติ": "heavier than usual",
89
+ "หนักมาก": "very heavy",
90
+
91
+ # tilt
92
+ "แทบไม่เอียง": "almost no axial tilt",
93
+ "เอียงเล็กน้อย มีฤดูกาลเบาๆ": "slightly tilted, gentle seasons",
94
+ "เอียงปานกลาง ฤดูกาลเปลี่ยนแปลง": "moderately tilted, noticeable seasonal changes",
95
+ "เอียงมาก ฤดูกาลสุดขั้ว": "highly tilted, extreme seasonal shifts",
96
+
97
+ # moons (prefix check some conditions)
98
+ # => We'll dynamically generate string TH -> EN
99
+ }
100
+
101
+ def describe_distance_th_to_en(desc_th):
102
+ return pretranslated_map.get(desc_th, "unknown distance environment")
103
+
104
+ def describe_temp_th_to_en(desc_th):
105
+ return pretranslated_map.get(desc_th, "unknown temperature")
106
+
107
+ def describe_gravity_th_to_en(desc_th):
108
+ return pretranslated_map.get(desc_th, "unknown gravity")
109
+
110
+ def describe_tilt_th_to_en(desc_th):
111
+ return pretranslated_map.get(desc_th, "unknown axial tilt")
112
+
113
+ ###################################
114
+ # ฟังก์ชันบรรยาย (ภาษาไทย)
115
  ###################################
116
  def describe_distance(distance_au):
117
  if distance_au < 0.5:
 
119
  elif distance_au < 1.2:
120
  return "โคจรคล้ายโลกหรืออุ่นกว่านิดหน่อย"
121
  elif distance_au < 2.5:
122
+ return "โคจรห่างพอประมาณ อากาศค่อนขั้วเย็น" # อาจแก้เป็น "ค่อนข้างเย็น"
123
  else:
124
  return "โคจรไกลสุดขั้ว อากาศหนาวมาก"
125
 
 
159
  elif n == 1:
160
  return "มีดวงจันทร์หนึ่งดวง"
161
  else:
 
162
  if n > 2:
163
+ return f"มีดวงจันทร์ {n} ดวง (ทำให้เกิดน้ำขึ้นน้ำลงรุนแรง)"
164
  else:
165
  return f"มีดวงจันทร์ {n} ดวง"
166
 
167
+ def describe_oxygen(oxygen_percent):
168
+ if oxygen_percent < 1:
169
+ return "ไม่มีออกซิเจน"
170
+ elif oxygen_percent < 10:
171
+ return "ออกซิเจนน้อย"
172
+ elif oxygen_percent < 25:
173
+ return "ออกซิเจนพอเหมาะ"
174
+ else:
175
+ return "ออกซิเจนสูง"
176
+
177
  ###################################
178
+ # 5) สร้าง Prompt 3 แบบ (Pre-translate dictionary)
179
  ###################################
180
+ def build_prompts_en(
181
+ planet_name_en, star_type_en,
182
+ dist_desc_en, temp_desc_en, grav_desc_en, tilt_desc_en, moon_desc_en, oxygen_desc_en, life_en
183
+ ):
184
+ # Prompt 1
185
  prompt1 = (
186
  f"A vibrant space painting of planet '{planet_name_en}' orbiting a {star_type_en}. "
187
  f"It is {dist_desc_en}, with {temp_desc_en} conditions and {grav_desc_en} gravity. "
188
  f"{tilt_desc_en}, {moon_desc_en}, atmosphere has {oxygen_desc_en}. Cinematic details."
189
  )
190
 
191
+ # Prompt 2
192
  prompt2 = (
193
  f"On planet '{planet_name_en}', we discover {life_en} thriving in {temp_desc_en} weather, "
194
  f"{grav_desc_en} pull, and {oxygen_desc_en} in the air. Surreal alien ecosystem, rich concept art."
195
  )
196
 
197
+ # Prompt 3
198
  prompt3 = (
199
  f"Exploring the surface of '{planet_name_en}': {temp_desc_en} climate, {grav_desc_en}, "
200
  f"{tilt_desc_en} tilt, and {moon_desc_en}. Epic environment design, atmospheric perspective."
 
208
  return prompt_all
209
 
210
  ###################################
211
+ # 6) ฟังก์ชันหลัก generate_planet_info
212
  ###################################
213
  def generate_planet_info(
214
  planet_name_th,
215
+ star_type_en,
216
  distance_str,
217
  diameter_str,
218
  tilt_value,
219
  moon_value,
220
  oxygen_percent,
221
+ planet_type_th,
222
  life_th
223
  ):
224
  # parse
 
242
  except:
243
  moon_count = 1
244
 
245
+ # คำนวณ
246
  temp_c = approximate_temp_with_star(star_type_en, distance_au)
 
247
  g_approx = approximate_gravity_nw(diameter_factor)
248
 
249
+ # -----------------------------
250
+ # (A) สรุปสำหรับเด็ก (ไทย)
251
+ # -----------------------------
252
+ child_summary = ""
253
+ child_summary += f"สวัสดีหนูน้อยนักสำรวจ!\n\n"
254
+ child_summary += f"ดาว **{planet_name_th}** ที่หนูสร้าง เป็น{planet_type_th} "
255
+ child_summary += f"โคจรรอบดาวฤกษ์ **{star_type_en}**\n"
256
+
257
+ child_summary += f"ระยะห่าง ~{distance_au} AU => ประเมินอุณหภูมิ ~{temp_c}°C\n"
258
+ if temp_c < -20:
259
+ child_summary += "หนาวจัดเลยทีเดียว!\n"
260
+ elif temp_c > 35:
261
+ child_summary += "ร้อนระอุทีเดียว!\n"
262
+ else:
263
+ child_summary += "อากาศดูพอเหมาะนะ!\n"
264
+
265
+ child_summary += f"แรงโน้มถ่วง ~{g_approx}g"
266
+ if g_approx < 0.5:
267
+ child_summary += " (ลอยง่าย!)"
268
+ elif g_approx > 1.5:
269
+ child_summary += " (หนักเดินลำบาก!)"
270
+ child_summary += "\n"
271
+
272
+ child_summary += f"แกนเอียง {tilt_deg}° => "
273
+ if tilt_deg > 45:
274
+ child_summary += "ฤดูกาลแปรปรวนสุดขั้ว!\n"
275
+ elif tilt_deg < 5:
276
+ child_summary += "แทบไม่เปลี่ยนฤดูกาล\n"
277
+ else:
278
+ child_summary += "มีฤดูกาลตามองศาเอียง\n"
279
 
280
+ child_summary += f"มีดวงจันทร์ {moon_count} ดวง\n"
281
+ if moon_count > 2:
282
+ child_summary += "น้ำขึ้นน้ำลงคงอลังการมาก\n"
283
+
284
+ child_summary += f"ออกซิเจน ~{oxygen_percent}%\n"
285
+ if oxygen_percent < 5:
286
+ child_summary += "น้อยมาก ต้องใช้ชุดอวกาศเลย\n"
287
+ elif oxygen_percent > 30:
288
+ child_summary += "สูงมาก ระวังไฟติดง่ายนะ\n"
289
+
290
+ child_summary += f"สิ่งมีชีวิต: **{life_th}**\n"
291
+ child_summary += "ลองคิดดูสิว่าหน้าตาจะเป็นยังไง!\n\n"
292
+ child_summary += "พร้อมลุยกันแล้วหรือยัง!?"
293
+
294
+ # -----------------------------
295
+ # (B) รายละเอียด (ไทย)
296
+ # -----------------------------
297
  detail_th = (
298
  f"ชื่อดาวเคราะห์(ไทย): {planet_name_th}\n"
299
  f"ประเภทดาวฤกษ์: {star_type_en}\n"
 
304
  f"เปอร์เซ็นต์ O2: {oxygen_percent}%\n"
305
  f"ชนิดดาวเคราะห์(ไทย): {planet_type_th}\n"
306
  f"สิ่งมีชีวิต(ไทย): {life_th}\n"
307
+ f"อุณหภูมิ: ~{temp_c} °C\n"
308
+ f"แรงโน้มถ่วง (สมมุติ): ~{g_approx} g\n"
309
  )
310
 
311
+ # -----------------------------
312
+ # (C) สร้าง Prompt อังกฤษ 3 แบบ
313
+ # -----------------------------
314
+ # 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input)
315
  planet_name_en = translate_th_to_en(planet_name_th)
316
  life_en = translate_th_to_en(life_th)
317
 
318
+ # 2) อธิบาย distance/temp/gravity/tilt/moon/oxygen เป็นภาษาไทย -> Map to EN
319
+ # ไม่ต้องเรียก MarianMT เพราะเราใช้ pretranslated map
320
  dist_desc_th = describe_distance(distance_au)
 
 
321
  temp_desc_th = describe_temp(temp_c)
 
 
322
  grav_desc_th = describe_gravity(g_approx)
 
 
323
  tilt_desc_th = describe_tilt(tilt_deg)
 
 
324
  moon_desc_th = describe_moons(moon_count)
325
+ o2_desc_th = describe_oxygen(oxygen_percent)
326
+
327
+ # แปลงผ่าน Dictionary (ถ้าไม่เจอ key, fallback "unknown")
328
+ # distance
329
+ dist_desc_en = pretranslated_map.get(dist_desc_th, "unknown distance")
330
+ # temperature
331
+ temp_desc_en = pretranslated_map.get(temp_desc_th, "unknown temperature")
332
+ # gravity
333
+ grav_desc_en = pretranslated_map.get(grav_desc_th, "unknown gravity")
334
+ # tilt
335
+ tilt_desc_en = pretranslated_map.get(tilt_desc_th, "unknown tilt")
336
+
337
+ # moons (กรณีมีจำนวนระบุใน string)
338
+ # "มีดวงจันทร์ x ดวง" -> Pre-translate ประโยคแบบ dynamic?
339
+ # เราอาจ simplify: if "dวง" in moon_desc_th => split
340
+ if "ไม่มีดวงจันทร์" in moon_desc_th:
341
+ moon_desc_en = "no moons"
342
+ elif "หนึ่งดวง" in moon_desc_th:
343
+ moon_desc_en = "one moon"
344
+ elif "ทำให้เกิดน้ำขึ้นน้ำลงรุนแรง" in moon_desc_th:
345
+ # e.g. "มีดวงจันทร์ 4 ดวง (ทำให้เกิด...)"
346
+ # split out the number
347
+ import re
348
+ match = re.search(r"(\d+) ดวง", moon_desc_th)
349
+ if match:
350
+ n_str = match.group(1)
351
+ moon_desc_en = f"{n_str} moons causing powerful tides"
352
+ else:
353
+ moon_desc_en = "multiple moons causing stronger tides"
354
+ elif "มีดวงจันทร์ " in moon_desc_th:
355
+ # e.g. "มีดวงจันทร์ 2 ดวง"
356
+ match = re.search(r"(\d+) ดวง", moon_desc_th)
357
+ if match:
358
+ n_str = match.group(1)
359
+ moon_desc_en = f"{n_str} moons"
360
+ else:
361
+ moon_desc_en = "some moons"
362
  else:
363
+ moon_desc_en = "unknown moons"
364
+
365
+ # oxygen
366
+ if o2_desc_th == "ไม่มีออกซิเจน":
367
+ oxygen_desc_en = "no oxygen"
368
+ elif o2_desc_th == "ออกซิเจนน้อย":
369
+ oxygen_desc_en = "low oxygen"
370
+ elif o2_desc_th == "ออกซิเจนพอเหมาะ":
371
+ oxygen_desc_en = "moderate oxygen"
372
+ else:
373
+ oxygen_desc_en = "high oxygen"
374
 
375
+ # 3) build prompts
376
  prompts_en = build_prompts_en(
377
  planet_name_en,
378
  star_type_en + " star",
 
388
  return child_summary, detail_th, prompts_en
389
 
390
  ###################################
391
+ # สูตรโชว์
392
  ###################################
393
  formula_text = r"""
394
+ **สูตรอุณหภูมิ (Stefan-Boltzmann) แบบง่าย**
 
395
  \\[
396
  T = \left(\frac{(1 - A) \times L}{16 \pi \sigma \, d^2}\right)^{\frac{1}{4}} - 273.15 + 15^\circ\text{C (Greenhouse)}
397
  \\]
398
 
399
+ - \\(A\\) = Albedo
400
+ - \\(L\\) = ความสว่างของดาว (W)
401
+ - \\(\sigma\\) = 5.67\\times10^{-8}
402
  - \\(d\\) = ระยะทาง (m)
403
 
404
+ **สูตรแรงโน้มถ่วงนิวตัน**
405
+ \\(g = \frac{G M}{R^2}\\)
406
+ (เราใช้สมมุติว่า \\(M \propto R^3\\) => \\(g \propto R\\))
 
 
407
  """
408
 
409
  ###################################
410
+ # สร้าง UI ด้วย Gradio
411
  ###################################
412
  css_code = """
413
  body {
 
455
  return new_state, gr.update(visible=new_state)
456
 
457
  def welcome_text():
458
+ return "ยินดีต้อนรับสู่ Planetary Adventure++ (Optimized)! ลองกรอกข้อมูลแล้วกด 'สร้างโลกแฟนตาซี' สิ!"
459
 
460
  with gr.Blocks(css=css_code) as demo:
461
+ gr.Markdown("<h1 id='title'>Planetary Adventure++ (Optimized for Fewer Translations)</h1>")
462
  gr.Markdown("""
463
  <div class="game-desc">
464
+ <p>เวอร์ชันนี้แปลเฉพาะชื่อดาวและสิ่งมีชีวิต (Thai->Eng) เพื่อประหยัดเวลา!
465
+ ส่วนคำบรรยายอื่นๆ เก็บคำแปลล่วงหน้า</p>
466
+ <p>ใส่ข้อมูล แล้วกด <strong>"สร้างโลกแฟนตาซี"</strong> เพื่อดู
467
+ <em>สรุปสำหรับเด็ก</em>, <em>รายละเอียด (ไทย)</em>, และ <em>3 Prompts อังกฤษ</em></p>
468
+ <p>ถ้าอยากดูสูตรคำนวณ กดปุ่ม "โชว์สูตรคำนวณ" ได้เลย!</p>
469
  </div>
470
  """)
471
 
 
477
 
478
  with gr.Row():
479
  with gr.Column():
480
+ planet_name_th = gr.Textbox(label="ชื่อดาวเคราะห์ (ไทย)", placeholder="ดาวซานาดา, ดาวโซลาริส...")
481
  star_type_en = gr.Dropdown(
482
  label="ประเภทดาวฤกษ์",
483
  choices=["Red Dwarf", "White Dwarf", "Sun-like", "Blue Giant", "Supergiant"],
 
490
  tilt_slider = gr.Slider(0, 90, step=1, value=23.5, label="แกนเอียง (องศา)")
491
  moon_slider = gr.Slider(0, 10, step=1, value=1, label="จำนวนดวงจันทร์")
492
  oxygen_slider = gr.Slider(0, 100, step=1, value=21, label="% ออกซิเจน")
 
493
  planet_type_th = gr.Dropdown(
494
  label="ชนิดดาวเคราะห์ (ไทย)",
495
  choices=["ดาวหิน", "ดาวก๊าซ", "ดาวน้ำแข็ง"],
 
518
  }
519
  const promptText = promptBox.value;
520
  navigator.clipboard.writeText(promptText);
521
+ alert('คัดลอก Prompt แล้ว! นำไปใช้กับ AI สร้างภาพได้เลย~');
522
  }
523
  </script>
524
  """
 
560
  )
561
 
562
  def welcome_text():
563
+ return "ยินดีต้อนรับสู่ Planetary Adventure++ (Optimized)! ลองกรอกข้อมูลแล้วกด 'สร้างโลกแฟนตาซี' สิ!"
564
+
565
  demo.load(fn=welcome_text, inputs=None, outputs=child_summary_out)
566
 
567
  demo.launch()