zenityx's picture
Update app.py
adc07a1 verified
raw
history blame
10.5 kB
import gradio as gr
import random
#########################
# ฟังก์ชันคำนวณง่าย ๆ
#########################
def approximate_temperature(planet_type, distance_au):
"""
คำนวณอุณหภูมิ (สมมุติเบื้องต้น) ตามชนิดดาว + ระยะ AU
"""
# กำหนด base_temp
if planet_type == "ดาวหิน (Rocky Planet)":
base_temp = 15
elif planet_type == "ดาวก๊าซ (Gas Giant)":
base_temp = 30
elif planet_type == "ดาวน้ำแข็ง (Icy Planet)":
base_temp = -50
else:
base_temp = 0
# ปรับตามระยะทาง
if distance_au > 1.0:
diff = (distance_au - 1.0) * 20
approx_temp = base_temp - diff
else:
diff = (1.0 - distance_au) * 30
approx_temp = base_temp + diff
return round(approx_temp)
def approximate_gravity(diameter_earth):
"""
แรงโน้มถ่วงสมมุติ (g) = diameter_earth เท่า
"""
return round(1.0 * diameter_earth, 2)
#########################
# ฟังก์ชันสรุปข้อมูล (สำหรับเด็ก)
#########################
def child_friendly_summary(
name_th, planet_type_th, temp_c, gravity_g, atmosphere_th, life_th
):
"""
สร้างข้อความสรุป (ภาษาไทย) ให้เด็กอ่านเข้าใจง่าย
เช่น "ดาว ซานาดา เป็นดาวหิน อุณหภูมิใกล้ๆ 20°C มีบรรยากาศ..."
"""
if temp_c > 40:
temp_comment = "ค่อนข้างร้อนเลยนะ!"
elif temp_c < -10:
temp_comment = "หนาวมาก ๆ เลยนะ!"
else:
temp_comment = "ค่อนข้างสบาย ๆ น่าอยู่!"
if gravity_g > 1.5:
gravity_comment = f"แรงโน้มถ่วงประมาณ {gravity_g}g เลยนะ อาจจะเดินยากหน่อย!"
elif gravity_g < 0.5:
gravity_comment = f"แรงโน้มถ่วงเบาแค่ {gravity_g}g ระวังลอยได้นะ!"
else:
gravity_comment = f"แรงโน้มถ่วงกำลังดี ~{gravity_g}g"
summary_th = (
f"ดาว {name_th} เป็น{planet_type_th} "
f"อุณหภูมิราว ๆ {temp_c}°C {temp_comment}\n"
f"{gravity_comment}\n"
f"บรรยากาศ: {atmosphere_th}\n"
f"มีสิ่งมีชีวิต: {life_th}\n"
f"น่าสนุกจังเลย!"
)
return summary_th
#########################
# ฟังก์ชันสร้าง Prompt
#########################
def generate_planet_info(
planet_name_th,
planet_type_th,
distance_str,
diameter_str,
atmosphere_th,
life_th
):
# 1) แปลง string เป็น float
try:
distance_au = float(distance_str)
except:
distance_au = 1.0
try:
diameter_factor = float(diameter_str)
except:
diameter_factor = 1.0
# 2) คำนวณ
temp_c = approximate_temperature(planet_type_th, distance_au)
gravity_g = approximate_gravity(diameter_factor)
# 3) สรุปเด็กอ่านง่าย (child_friendly_summary)
child_summary = child_friendly_summary(
planet_name_th, planet_type_th, temp_c, gravity_g, atmosphere_th, life_th
)
# 4) รายละเอียดเชิงเทคนิค (ภาษาไทย)
detail_th = (
f"ชื่อดาวเคราะห์: {planet_name_th}\n"
f"ชนิดดาว: {planet_type_th}\n"
f"ระยะห่างจากดาวฤกษ์: ~{distance_au} AU\n"
f"ขนาด: ~{diameter_factor} เท่าโลก\n"
f"อุณหภูมิพื้นผิวโดยประมาณ: ~{temp_c} °C\n"
f"แรงโน้มถ่วง: ~{gravity_g} g\n"
f"บรรยากาศ: {atmosphere_th}\n"
f"สิ่งมีชีวิต: {life_th}\n"
)
# 5) Prompt ภาษาอังกฤษ (สำหรับ Midjourney)
# แปลง planet_type_th เป็นคำอังกฤษสั้น ๆ
type_map = {
"ดาวหิน (Rocky Planet)": "a rocky planet",
"ดาวก๊าซ (Gas Giant)": "a gas giant",
"ดาวน้ำแข็ง (Icy Planet)": "an icy planet",
}
type_en = type_map.get(planet_type_th, "a mysterious planet")
prompt_en = (
f"Planet Name: '{planet_name_th}' | "
f"{type_en}, orbiting at {distance_au} AU, "
f"size about {diameter_factor}x Earth diameter, "
f"with atmosphere '{atmosphere_th}', "
f"hosting '{life_th}'. "
f"Surface temperature ~{temp_c} C, "
f"gravity ~{gravity_g} g. "
f"Highly detailed, awe-inspiring, cinematic."
)
return child_summary, detail_th, prompt_en
#########################
# สร้าง 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 {
background-color: #FFFDF5;
border: 2px solid #FFE082;
border-radius: 10px;
padding: 10px;
margin-bottom: 20px;
}
"""
def welcome_text():
return "ยินดีต้อนรับสู่ Planetary Adventure! ลองกรอกข้อมูลแล้วกด 'สร้างโลกแฟนตาซี' ดูสิ!"
# ปุ่ม Copy Prompt (HTML + JS)
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 แล้ว! นำไปใช้ใน Midjourney ได้เลย~');
}
</script>
"""
with gr.Blocks(css=css_code) as demo:
gr.Markdown("<h1 id='title'>Planetary Adventure (Thai) + สรุปข้อมูลดวงดาว</h1>")
gr.Markdown("""
<div class="game-desc">
<p>สร้างดาวเคราะห์ในจินตนาการ พร้อมข้อความสรุปสนุก ๆ สำหรับเด็ก และ Prompt ภาษาอังกฤษเพื่อนำไปเจนภาพ!</p>
<ol>
<li>กรอกข้อมูลด้านล่างเป็นภาษาไทย เช่น ชื่อดาว, ชนิดดาว, ฯลฯ</li>
<li>กดปุ่ม <strong>"สร้างโลกแฟนตาซี"</strong></li>
<li>ดู <em>ข้อความสรุป (สำหรับเด็ก)</em>, <em>รายละเอียด (เทคนิค)</em> และ <em>Prompt อังกฤษ</em></li>
<li>กด "<strong>คัดลอก Prompt</strong>" เพื่อเอาไปใช้ใน AI สร้างภาพ เช่น Midjourney</li>
</ol>
</div>
""")
planet_name = gr.Textbox(label="ชื่อดาวเคราะห์ (ภาษาไทย)", placeholder="ตัวอย่าง: ดาวซานาดา")
planet_type = gr.Dropdown(
label="ชนิดดาว",
choices=["ดาวหิน (Rocky Planet)", "ดาวก๊าซ (Gas Giant)", "ดาวน้ำแข็ง (Icy Planet)"],
value="ดาวหิน (Rocky Planet)"
)
distance_au = gr.Textbox(label="ระยะห่างจากดาวฤกษ์ (หน่วย AU)", placeholder="เช่น 0.8 หรือ 1 หรือ 2")
diameter_factor = gr.Textbox(label="ขนาดเส้นผ่านศูนย์กลาง (เท่าโลก)", placeholder="เช่น 1, 2, 0.5")
atmosphere_th = gr.Textbox(label="ลักษณะบรรยากาศ", placeholder="ตัวอย่าง: ออกซิเจนบาง ๆ, มีคาร์บอนไดออกไซด์สูง")
life_th = gr.Textbox(label="สิ่งมีชีวิตบนดาว", placeholder="ตัวอย่าง: สัตว์เลื้อยคลานขนาดยักษ์")
create_btn = gr.Button("สร้างโลกแฟนตาซี", elem_classes="btn-main")
child_summary_output = gr.Textbox(
label="ข้อความสรุป (อ่านง่ายสำหรับเด็ก)",
interactive=False,
elem_id="child-summary"
)
detail_th_output = gr.Textbox(
label="รายละเอียด (เทคนิค)",
interactive=False,
elem_id="detail-th"
)
prompt_en_output = gr.Textbox(
label="Prompt ภาษาอังกฤษ",
interactive=False,
elem_id="prompt-en"
)
gr.HTML(copy_button_html)
# เมื่อกดปุ่ม
create_btn.click(
fn=generate_planet_info,
inputs=[planet_name, planet_type, distance_au, diameter_factor, atmosphere_th, life_th],
outputs=[child_summary_output, detail_th_output, prompt_en_output]
)
demo.load(fn=welcome_text, inputs=None, outputs=child_summary_output)
demo.launch()