zenityx commited on
Commit
9859dae
·
verified ·
1 Parent(s): bf1efb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -2,9 +2,7 @@ import gradio as gr
2
  from transformers import MarianMTModel, MarianTokenizer
3
  import random
4
 
5
- # ---------------------------
6
- # 1) โหลดโมเดลแปล (MarianMT)
7
- # ---------------------------
8
  model_name = "Helsinki-NLP/opus-mt-th-en"
9
  tokenizer = MarianTokenizer.from_pretrained(model_name)
10
  model = MarianMTModel.from_pretrained(model_name)
@@ -18,9 +16,7 @@ def translate_th_to_en(th_text: str) -> str:
18
  en_text = tokenizer.decode(translation_tokens[0], skip_special_tokens=True)
19
  return en_text
20
 
21
- # ---------------------------
22
- # 2) ข้อมูล (ภาษาไทย) สำหรับ Dropdown
23
- # ---------------------------
24
  body_shapes_th = [
25
  "ตัวเหนียวขยุกขยิก", "ตัวปุกปุยกลม", "ตัวเต็มไปด้วยหนาม",
26
  "ตัวเล็กเหมือนแมลง", "ตัวโปร่งแสงลอยได้", "ตัวโคลนยืดหยุ่น",
@@ -53,9 +49,6 @@ moods_th = [
53
  "เศร้าสร้อย", "จริงจัง", "มีความสุข", "ขี้ตกใจ"
54
  ]
55
 
56
- # ---------------------------
57
- # 3) ฟังก์ชันหลัก
58
- # ---------------------------
59
  def generate_monster_lab(
60
  body_dd, body_tb,
61
  head_dd, head_tb,
@@ -64,7 +57,7 @@ def generate_monster_lab(
64
  ability_dd, ability_tb,
65
  mood_dd, mood_tb,
66
  ):
67
- # เลือกค่าจาก textbox หากไม่ว่าง, มิฉะนั้นใช้ dropdown
68
  body_th = body_tb.strip() if body_tb.strip() else body_dd
69
  head_th = head_tb.strip() if head_tb.strip() else head_dd
70
  arms_th = arms_tb.strip() if arms_tb.strip() else arms_dd
@@ -94,27 +87,22 @@ def generate_monster_lab(
94
  return desc_th, prompt_en
95
 
96
  def random_monster():
97
- """
98
- สุ่มค่า Dropdown ทั้ง 6 หมวด, พร้อมเคลียร์ช่อง Textbox ทั้ง 6
99
- """
100
- import random
101
  bd = random.choice(body_shapes_th)
102
  hd = random.choice(heads_th)
103
  ar = random.choice(arms_legs_th)
104
  sk = random.choice(skin_patterns_th)
105
  ab = random.choice(abilities_th)
106
  md = random.choice(moods_th)
107
-
108
  return (bd, "", hd, "", ar, "", sk, "", ab, "", md, "")
109
 
110
- # ---------------------------
111
- # 4) สร้าง UI Gradio
112
- # ---------------------------
113
  css_code = """
114
  body {
115
  background-color: #FFF7EA;
116
  font-family: "Kanit", sans-serif;
117
  }
 
118
  #title {
119
  color: #FF6F91;
120
  text-align: center;
@@ -133,28 +121,33 @@ body {
133
  color: #333;
134
  margin-bottom: 20px;
135
  }
 
 
136
  .btn-main {
137
  background-color: #FFC107;
138
  border: 2px solid #FFA000;
139
  font-weight: bold;
140
  font-size: 1.1rem;
141
- padding: 10px 20px;
 
142
  border-radius: 10px;
143
  margin-right: 10px;
144
  }
145
 
 
146
  .btn-random {
147
- /* เล็กกว่า, สีอ่อนลง */
148
- background-color: #FFE08E; /* สีเหลืองซอฟต์ */
149
  border: 2px solid #FFC107;
150
  font-weight: normal;
151
- font-size: 0.9rem;
152
- padding: 6px 15px;
 
153
  border-radius: 8px;
154
  }
155
  .btn-random:hover {
156
  background-color: #FFF3C4;
157
  }
 
158
  #desc-th, #prompt-en {
159
  background-color: #FFFAE6;
160
  border: 2px solid #FFE082;
@@ -167,6 +160,7 @@ body {
167
  def initial_text():
168
  return "ยังไม่ได้สร้างมอนสเตอร์ ลองเลือกหรือพิมพ์ แล้วกด 'สร้างมอนสเตอร์!' หรือจะกดสุ่มดูก็ได้จ้า"
169
 
 
170
  copy_button_html = """
171
  <button style="background-color: #F06292; border: 2px solid #E91E63; font-weight: bold;
172
  font-size: 1.1rem; padding: 10px 20px; border-radius: 10px;"
@@ -192,9 +186,8 @@ with gr.Blocks(css=css_code) as demo:
192
  gr.Markdown("""
193
  <div class="game-desc">
194
  <p>หนูน้อยจ๊ะ เลือกได้ว่าจะใช้ค่าใน <strong>Dropdown</strong> หรือจะ <strong>พิมพ์เอง</strong></p>
195
- <p>หากอยากลุ้นโชค ให้กดปุ่ม <strong>"สุ่���มอนสเตอร์สุดเซอร์ไพรส์!"</strong> (ปุ่มเล็ก)</p>
196
- <p>เมื่อพร้อมแล้ว กดปุ่ม <strong>"สร้างมอนสเตอร์!"</strong> (ปุ่มหลักด้านซ้าย) เพื่อดูรายละเอียด (ไทย) และ Prompt (อังกฤษ)</p>
197
- <p>กดปุ่ม <strong>"Copy Prompt"</strong> เพื่อคัดลอก Prompt ไปใช้กับ AI สร้างภาพได้เลย!</p>
198
  </div>
199
  """)
200
 
@@ -221,9 +214,9 @@ with gr.Blocks(css=css_code) as demo:
221
  mood_tb = gr.Textbox(label="หรือพิมพ์เอง", placeholder="ขี้เล่นเป็นพิเศษ...")
222
 
223
  with gr.Row():
224
- # ปุ่มหลัก (สร้างมอนสเตอร์) -> อยู่ฝั่งซ้าย
225
  create_btn = gr.Button("สร้างมอนสเตอร์!", elem_classes="btn-main")
226
- # ปุ่มสุ่ม (เล็ก, สีอ่อน) -> อยู่ฝั่งขวา
227
  random_btn = gr.Button("สุ่มมอนสเตอร์สุดเซอร์ไพรส์!", elem_classes="btn-random")
228
 
229
  monster_desc_th = gr.Textbox(label="รายละเอียด (ภาษาไทย)", interactive=False, elem_id="desc-th")
@@ -231,7 +224,7 @@ with gr.Blocks(css=css_code) as demo:
231
 
232
  gr.HTML(copy_button_html)
233
 
234
- # กดสุ่ม -> คืน 12 ค่า (6 Dropdown, 6 Textbox)
235
  random_btn.click(
236
  fn=random_monster,
237
  inputs=[],
@@ -245,7 +238,7 @@ with gr.Blocks(css=css_code) as demo:
245
  ]
246
  )
247
 
248
- # กดสร้าง -> generate_monster_lab
249
  create_btn.click(
250
  fn=generate_monster_lab,
251
  inputs=[
 
2
  from transformers import MarianMTModel, MarianTokenizer
3
  import random
4
 
5
+ # โหลดโมเดลแปล (MarianMT)
 
 
6
  model_name = "Helsinki-NLP/opus-mt-th-en"
7
  tokenizer = MarianTokenizer.from_pretrained(model_name)
8
  model = MarianMTModel.from_pretrained(model_name)
 
16
  en_text = tokenizer.decode(translation_tokens[0], skip_special_tokens=True)
17
  return en_text
18
 
19
+ # ตัวเลือก (ภาษาไทย) สำหรับ Dropdown
 
 
20
  body_shapes_th = [
21
  "ตัวเหนียวขยุกขยิก", "ตัวปุกปุยกลม", "ตัวเต็มไปด้วยหนาม",
22
  "ตัวเล็กเหมือนแมลง", "ตัวโปร่งแสงลอยได้", "ตัวโคลนยืดหยุ่น",
 
49
  "เศร้าสร้อย", "จริงจัง", "มีความสุข", "ขี้ตกใจ"
50
  ]
51
 
 
 
 
52
  def generate_monster_lab(
53
  body_dd, body_tb,
54
  head_dd, head_tb,
 
57
  ability_dd, ability_tb,
58
  mood_dd, mood_tb,
59
  ):
60
+ # ถ้า textbox มีข้อความ -> ใช้ค่านั้น, ถ้าว่าง -> ใช้ dropdown
61
  body_th = body_tb.strip() if body_tb.strip() else body_dd
62
  head_th = head_tb.strip() if head_tb.strip() else head_dd
63
  arms_th = arms_tb.strip() if arms_tb.strip() else arms_dd
 
87
  return desc_th, prompt_en
88
 
89
  def random_monster():
 
 
 
 
90
  bd = random.choice(body_shapes_th)
91
  hd = random.choice(heads_th)
92
  ar = random.choice(arms_legs_th)
93
  sk = random.choice(skin_patterns_th)
94
  ab = random.choice(abilities_th)
95
  md = random.choice(moods_th)
96
+ # Clear textbox
97
  return (bd, "", hd, "", ar, "", sk, "", ab, "", md, "")
98
 
99
+ # ---- CSS ----
 
 
100
  css_code = """
101
  body {
102
  background-color: #FFF7EA;
103
  font-family: "Kanit", sans-serif;
104
  }
105
+
106
  #title {
107
  color: #FF6F91;
108
  text-align: center;
 
121
  color: #333;
122
  margin-bottom: 20px;
123
  }
124
+
125
+ /* ปุ่มหลัก (สร้างมอนสเตอร์) */
126
  .btn-main {
127
  background-color: #FFC107;
128
  border: 2px solid #FFA000;
129
  font-weight: bold;
130
  font-size: 1.1rem;
131
+ /* กำหนดความสูงและความยาว (horizontal padding) เยอะๆ */
132
+ padding: 10px 40px;
133
  border-radius: 10px;
134
  margin-right: 10px;
135
  }
136
 
137
+ /* ปุ่มสุ่ม (รอง) => ตัวเล็กลงในความยาว (horizontal padding) แต่ความสูงเท่ากัน */
138
  .btn-random {
139
+ background-color: #FFE08E;
 
140
  border: 2px solid #FFC107;
141
  font-weight: normal;
142
+ font-size: 1.1rem;
143
+ /* ใช้ padding vertical เท่ากัน แต่ลด horizontal padding ลง */
144
+ padding: 10px 15px;
145
  border-radius: 8px;
146
  }
147
  .btn-random:hover {
148
  background-color: #FFF3C4;
149
  }
150
+
151
  #desc-th, #prompt-en {
152
  background-color: #FFFAE6;
153
  border: 2px solid #FFE082;
 
160
  def initial_text():
161
  return "ยังไม่ได้สร้างมอนสเตอร์ ลองเลือกหรือพิมพ์ แล้วกด 'สร้างมอนสเตอร์!' หรือจะกดสุ่มดูก็ได้จ้า"
162
 
163
+ # ปุ่ม copy
164
  copy_button_html = """
165
  <button style="background-color: #F06292; border: 2px solid #E91E63; font-weight: bold;
166
  font-size: 1.1rem; padding: 10px 20px; border-radius: 10px;"
 
186
  gr.Markdown("""
187
  <div class="game-desc">
188
  <p>หนูน้อยจ๊ะ เลือกได้ว่าจะใช้ค่าใน <strong>Dropdown</strong> หรือจะ <strong>พิมพ์เอง</strong></p>
189
+ <p>หากอยากลุ้นโชค กดปุ่ม (ขวามือ) <strong>"สุ่มมอนสเตอร์สุดเซอร์ไพรส์!"</strong></p>
190
+ <p>เมื่อพร้อมแล้ว กดปุ่ม (ซ้าย) <strong>"สร้างมอนสเตอร์!"</strong> เพื่อดูรายละเอียดและ Prompt</p>
 
191
  </div>
192
  """)
193
 
 
214
  mood_tb = gr.Textbox(label="หรือพิมพ์เอง", placeholder="ขี้เล่นเป็นพิเศษ...")
215
 
216
  with gr.Row():
217
+ # ปุ่มหลัก (สร้างมอนสเตอร์) -> อยู่ซ้าย
218
  create_btn = gr.Button("สร้างมอนสเตอร์!", elem_classes="btn-main")
219
+ # ปุ่มรอง (สุ่ม) -> อยู่ขวา, ตัวสั้นลง
220
  random_btn = gr.Button("สุ่มมอนสเตอร์สุดเซอร์ไพรส์!", elem_classes="btn-random")
221
 
222
  monster_desc_th = gr.Textbox(label="รายละเอียด (ภาษาไทย)", interactive=False, elem_id="desc-th")
 
224
 
225
  gr.HTML(copy_button_html)
226
 
227
+ # ปุ่มสุ่ม
228
  random_btn.click(
229
  fn=random_monster,
230
  inputs=[],
 
238
  ]
239
  )
240
 
241
+ # ปุ่มสร้าง
242
  create_btn.click(
243
  fn=generate_monster_lab,
244
  inputs=[