zenityx commited on
Commit
92ff7ee
·
verified ·
1 Parent(s): d4b0c11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -21
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
- # 5) สร้าง 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
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}. Cinematic details."
 
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. Surreal alien ecosystem, rich concept art."
 
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}. Epic environment design, atmospheric perspective."
 
258
  )
259
 
260
  return prompt1, prompt2, prompt3
261
 
262
  ###################################
263
- # 6) ฟังก์ชันหลัก generate_planet_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,18 +435,18 @@ 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
- - \\(A\\) = Albedo
416
- - \\(L\\) = ความสว่างของดาว (W)
417
- - \\(\sigma\\) = 5.67\\times10^{-8}
418
- - \\(d\\) = ระยะทาง (m)
419
 
420
  **สูตรแรงโน้มถ่วงนิวตัน**
421
- \\(g = \frac{G M}{R^2}\\)
422
- (เราใช้สมมุติว่า \\(M \propto R^3\\) => \\(g \propto R\\))
423
  """
424
 
425
  ###################################
@@ -522,6 +549,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 +587,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 +598,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 +613,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
  ###################################
 
549
  oxygen_slider = gr.Slider(0, 100, step=1, value=21, label="% ออกซิเจน")
550
  life_th = gr.Textbox(label="สิ่งมีชีวิต (ไทย)", placeholder="เช่น แมลงยักษ์เรืองแสง...")
551
 
552
+ with gr.Row():
553
+ style_selected = gr.Dropdown(
554
+ label="เลือกสไตล์ของพรอมพ์",
555
+ choices=style_options,
556
+ value="Standard"
557
+ )
558
+
559
  create_btn = gr.Button("สร้างโลกแฟนตาซี", elem_classes="btn-main")
560
 
561
  child_summary_out = gr.Textbox(label="สรุปสำหรับเด็ก (ไทย)", interactive=False, elem_id="child-summary")
 
587
  gr.HTML(copy_button_html)
588
 
589
  def generate_wrapper(
590
+ p_name_th, s_type_en, dist_au, dia_fac, tilt_val, moon_val, oxy_val, p_type_th, l_th, style_sel
591
  ):
592
  return generate_planet_info(
593
  planet_name_th=p_name_th,
 
598
  moon_value=str(moon_val),
599
  oxygen_percent=oxy_val,
600
  planet_type_th=p_type_th,
601
+ life_th=l_th,
602
+ style_selected=style_sel
603
  )
604
 
605
  create_btn.click(
 
613
  moon_slider,
614
  oxygen_slider,
615
  planet_type_th,
616
+ life_th,
617
+ style_selected
618
  ],
619
  outputs=[
620
  child_summary_out,