File size: 10,515 Bytes
adc07a1 2a4265e adc07a1 2a4265e adc07a1 2a4265e adc07a1 2a4265e 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 |
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()
|