zenityx commited on
Commit
36179e4
·
verified ·
1 Parent(s): 65a96d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -113
app.py CHANGED
@@ -153,7 +153,7 @@ def describe_oxygen_th_to_en(desc_th):
153
  return "high oxygen"
154
 
155
  ###################################
156
- # 5) ฟังก์ชันบรรยาย (ภาษาไทย)
157
  ###################################
158
  def describe_distance(distance_au):
159
  if distance_au < 0.5:
@@ -232,60 +232,35 @@ def describe_oxygen(oxygen_percent):
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,8 +271,7 @@ def generate_planet_info(
296
  moon_value,
297
  oxygen_percent,
298
  planet_type_th,
299
- life_th,
300
- style_selected
301
  ):
302
  # parse
303
  try:
@@ -392,7 +366,7 @@ def generate_planet_info(
392
  )
393
 
394
  # -----------------------------
395
- # (C) สร้าง Prompt อังกฤษ 3 แบบ พร้อมสไตล์
396
  # -----------------------------
397
  # 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input) ผ่านแคช
398
  planet_name_en = translate_th_to_en(planet_name_th)
@@ -424,8 +398,7 @@ def generate_planet_info(
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,113 +408,116 @@ def generate_planet_info(
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;
497
  margin-bottom: 10px;
498
  font-weight: bold;
499
  }
 
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,13 +568,6 @@ with gr.Blocks(css=css_code) as demo:
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,7 +599,7 @@ with gr.Blocks(css=css_code) as demo:
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,8 +610,7 @@ with gr.Blocks(css=css_code) as demo:
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,8 +624,7 @@ with gr.Blocks(css=css_code) as demo:
656
  moon_slider,
657
  oxygen_slider,
658
  planet_type_th,
659
- life_th,
660
- style_selected
661
  ],
662
  outputs=[
663
  child_summary_out,
 
153
  return "high oxygen"
154
 
155
  ###################################
156
+ # ฟังก์ชันบรรยาย (ภาษาไทย)
157
  ###################################
158
  def describe_distance(distance_au):
159
  if distance_au < 0.5:
 
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
  moon_value,
272
  oxygen_percent,
273
  planet_type_th,
274
+ life_th
 
275
  ):
276
  # parse
277
  try:
 
366
  )
367
 
368
  # -----------------------------
369
+ # (C) สร้าง Prompt อังกฤษ 3 แบบ
370
  # -----------------------------
371
  # 1) แปลชื่อดาว, สิ่งมีชีวิต (เฉพาะ user input) ผ่านแคช
372
  planet_name_en = translate_th_to_en(planet_name_th)
 
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
  ###################################
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
+ - \\(A\\) = Albedo
415
+ - \\(L\\) = ความสว่างของดาว (W)
416
+ - \\(\sigma\\) = 5.67\\times10^{-8}
417
+ - \\(d\\) = ระยะทาง (m)
 
 
418
  **สูตรแรงโน้มถ่วงนิวตัน**
419
+ \\(g = \frac{G M}{R^2}\\)
420
+ (เราใช้สมมุติว่า \\(M \propto R^3\\) => \\(g \propto R\\))
421
  """
422
 
423
  ###################################
424
+ # สร้าง UI ด้วย Gradio
425
  ###################################
426
  css_code = """
427
+ /* โหมดสว่าง (Light Theme) */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  body {
429
+ background-color: #F9FBFF;
 
430
  font-family: "Kanit", sans-serif;
431
+ color: #333333; /* สีข้อความทั่วไป */
432
  }
433
+
434
  #title {
435
+ color: #4A90E2;
436
  text-align: center;
437
  font-size: 2rem;
438
  margin-top: 20px;
439
  margin-bottom: 10px;
440
  font-weight: bold;
441
  }
442
+
443
  .game-desc {
444
  margin: 0 auto;
445
  width: 90%;
446
+ background-color: #ECF6FF;
447
+ border: 2px dashed #B3DAFF;
448
  border-radius: 10px;
449
  padding: 15px;
450
+ color: #333333;
451
  margin-bottom: 20px;
452
  }
453
+
454
  .btn-main {
455
+ background-color: #FFE066;
456
+ border: 2px solid #FFCA28;
457
  font-weight: bold;
458
  font-size: 1.1rem;
459
  padding: 10px 30px;
460
  border-radius: 10px;
461
  margin-right: 10px;
462
+ color: #000000; /* สีข้อความปุ่ม */
 
 
 
 
463
  }
464
+
465
  #child-summary, #detail-th, #prompt1-en, #prompt2-en, #prompt3-en, #formula-box {
466
+ background-color: #FFFDF5;
467
+ border: 2px solid #FFE082;
468
  border-radius: 10px;
469
  padding: 10px;
470
  margin-bottom: 20px;
471
+ color: #333333; /* สีข้อความภายในกล่อง */
472
  }
473
+
474
  .copy-btn {
475
+ background-color: #F06292;
476
+ border: 2px solid #E91E63;
477
  font-weight: bold;
478
  font-size: 0.8rem;
479
  padding: 5px 10px;
480
  border-radius: 5px;
481
  margin-top: 5px;
482
+ color: #FFFFFF; /* สีข้อความปุ่มคัดลอก */
 
483
  }
484
+
485
+ /* โหมดมืด (Dark Theme) */
486
+ @media (prefers-color-scheme: dark) {
487
+ body {
488
+ background-color: #1E1E1E;
489
+ color: #FFFFFF; /* สีข้อความทั่วไปในโหมดมืด */
490
+ }
491
+
492
+ #title {
493
+ color: #6EB1FF;
494
+ }
495
+
496
+ .game-desc {
497
+ background-color: #2E2E2E;
498
+ border: 2px dashed #555555;
499
+ color: #FFFFFF;
500
+ }
501
+
502
+ .btn-main {
503
+ background-color: #FFA500;
504
+ border: 2px solid #FF8C00;
505
+ color: #FFFFFF;
506
+ }
507
+
508
+ #child-summary, #detail-th, #prompt1-en, #prompt2-en, #prompt3-en, #formula-box {
509
+ background-color: #333333;
510
+ border: 2px solid #555555;
511
+ color: #FFFFFF;
512
+ }
513
+
514
+ .copy-btn {
515
+ background-color: #FF4081;
516
+ border: 2px solid #F50057;
517
+ color: #FFFFFF;
518
+ }
519
+
520
+ /* ปรับสีอื่นๆ ตามต้องการ */
521
  }
522
  """
523
 
 
568
  oxygen_slider = gr.Slider(0, 100, step=1, value=21, label="% ออกซิเจน")
569
  life_th = gr.Textbox(label="สิ่งมีชีวิต (ไทย)", placeholder="เช่น แมลงยักษ์เรืองแสง...")
570
 
 
 
 
 
 
 
 
571
  create_btn = gr.Button("สร้างโลกแฟนตาซี", elem_classes="btn-main")
572
 
573
  child_summary_out = gr.Textbox(label="สรุปสำหรับเด็ก (ไทย)", interactive=False, elem_id="child-summary")
 
599
  gr.HTML(copy_button_html)
600
 
601
  def generate_wrapper(
602
+ p_name_th, s_type_en, dist_au, dia_fac, tilt_val, moon_val, oxy_val, p_type_th, l_th
603
  ):
604
  return generate_planet_info(
605
  planet_name_th=p_name_th,
 
610
  moon_value=str(moon_val),
611
  oxygen_percent=oxy_val,
612
  planet_type_th=p_type_th,
613
+ life_th=l_th
 
614
  )
615
 
616
  create_btn.click(
 
624
  moon_slider,
625
  oxygen_slider,
626
  planet_type_th,
627
+ life_th
 
628
  ],
629
  outputs=[
630
  child_summary_out,