zenityx commited on
Commit
c3d8052
·
verified ·
1 Parent(s): 0009686

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -176
app.py CHANGED
@@ -2,126 +2,86 @@ import gradio as gr
2
  import random
3
 
4
  # ==========================
5
- # 1) ตัวอย่างคลังคำ 5 หมวด (หมวดละ 5 คำ)
6
- # หากต้องการหมวดละ 100 คำ ให้แทนที่ใน word_bank ได้เลย
7
  # ==========================
8
  word_bank = {
9
  "Character": [
10
- "cat", "dog", "fox", "lion", "tiger", "cheetah", "leopard", "panther", "jaguar", "wolf",
11
- "coyote", "bear", "polar bear", "panda", "zebra", "giraffe", "elephant", "rhinoceros", "hippopotamus", "donkey",
12
- "horse", "unicorn", "pegasus", "phoenix", "griffin", "gargoyle", "mermaid", "merman", "centaur", "minotaur",
13
- "faun", "satyr", "dwarf", "elf", "orc", "troll", "giant", "vampire", "werewolf", "ghost",
14
- "skeleton", "zombie", "alien", "robot", "cyborg", "superhero", "supervillain", "wizard", "witch", "warlock",
15
- "shaman", "druid", "necromancer", "pirate", "ninja", "samurai", "knight", "archer", "princess", "prince",
16
- "king", "queen", "soldier", "astronaut", "scientist", "detective", "clown", "clownfish", "goldfish", "shark",
17
- "whale", "dolphin", "crab", "lobster", "octopus", "monkey", "gorilla", "chimpanzee", "rabbit", "hamster",
18
- "mouse", "rat", "frog", "toad", "crocodile", "alligator", "turtle", "tortoise", "flamingo", "penguin",
19
- "reindeer", "kangaroo", "koala", "boar", "lizard", "snake", "eagle", "hawk", "raven", "parrot"
20
  ],
21
  "Adjective": [
22
- "adorable", "anxious", "artistic", "brave", "bright", "calm", "charming", "cheerful", "clever", "clumsy",
23
- "colorful", "courageous", "creative", "curious", "dazzling", "delightful", "detailed", "determined", "dynamic", "eager",
24
- "electric", "elegant", "enchanting", "energetic", "enthusiastic", "fantastic", "ferocious", "festive", "fiery", "fluffy",
25
- "glamorous", "glorious", "glowing", "graceful", "grand", "happy", "harmonious", "heroic", "hilarious", "hopeful",
26
- "hungry", "imaginative", "important", "impressive", "industrious", "joyful", "jolly", "jubilant", "lively", "lonely",
27
- "lovely", "magical", "magnificent", "marvelous", "mysterious", "natural", "nervous", "notable", "outstanding", "peaceful",
28
- "playful", "pleasant", "powerful", "precious", "pretty", "proud", "quick", "radiant", "remarkable", "robust",
29
- "shiny", "shy", "silly", "skillful", "sleepy", "smart", "sneaky", "sparkling", "speedy", "spooky",
30
- "splendid", "strange", "strong", "stunning", "surprising", "swift", "thoughtful", "tiny", "towering", "tricky",
31
- "vibrant", "victorious", "warm", "whimsical", "wild", "witty", "wonderful", "youthful", "zany", "zealous"
32
  ],
33
  "Activity": [
34
- "dancing", "singing", "running", "jumping", "swimming", "flying", "drawing", "painting", "cooking", "baking",
35
- "coding", "reading", "writing", "building", "crafting", "sculpting", "gardening", "fishing", "hiking", "camping",
36
- "skateboarding", "skiing", "snowboarding", "surfing", "kayaking", "canoeing", "sailing", "driving", "racing", "meditating",
37
- "yoga", "playing chess", "playing guitar", "playing piano", "playing violin", "playing drums", "teaching", "learning", "exploring", "adventuring",
38
- "juggling", "trick-or-treating", "stargazing", "birdwatching", "volunteering", "caroling", "collecting stamps", "collecting coins", "partying", "celebrating",
39
- "puzzle-solving", "gaming", "role-playing", "performing magic", "filming", "photographing", "breakdancing", "crocheting", "knitting", "weaving",
40
- "beekeeping", "traveling", "commuting", "drumming", "humming", "whistling", "daydreaming", "brainstorming", "ice-skating", "rollerblading",
41
- "writing poetry", "writing stories", "cooking dinner", "baking cookies", "sewing", "streaming", "vlogging", "cheerleading", "playing table tennis", "metalworking",
42
- "woodworking", "blacksmithing", "jogging", "weightlifting", "mind-mapping", "customizing", "forging", "greeting", "hugging", "high-fiving",
43
- "painting miniatures", "planting trees", "hosting", "celebrating a birthday", "watchmaking", "singing karaoke", "puzzle-designing", "baton-twirling", "samba dancing", "skipping rope"
44
  ],
45
  "Style": [
46
- "abstract style", "realistic style", "cartoon style", "pastel style", "watercolor style", "oil painting style", "impressionist style", "expressionist style", "cubist style", "futuristic style",
47
- "surreal style", "pop art style", "comic style", "manga style", "anime style", "steampunk style", "cyberpunk style", "fantasy style", "retro style", "vintage style",
48
- "noir style", "minimalist style", "modern style", "contemporary style", "baroque style", "rococo style", "gothic style", "vaporwave style", "pixel art style", "low poly style",
49
- "line art style", "chalk art style", "pencil sketch style", "collage style", "psychedelic style", "flat design style", "graffiti style", "doodle style", "glitch art style", "photorealistic style",
50
- "sci-fi style", "biopunk style", "dieselpunk style", "medieval style", "fantastic style", "art deco style", "art nouveau style", "cartoony shading style", "geometric style", "tribal style",
51
- "stained glass style", "claymation style", "paper cut style", "children's book style", "storybook style", "chibi style", "dark fantasy style", "vibrant color style", "monochrome style", "sepia style",
52
- "rainbow style", "comic strip style", "cartoony outlines style", "pop-up book style", "woodcut style", "ceramic style", "knitted style", "embroidered style", "mosaic style", "retro-futurism style",
53
- "diesel style", "pastel goth style", "grunge style", "tropical style", "beach style", "desert style", "arctic style", "mystical style", "fairy tale style", "cel-shaded style",
54
- "chalkboard style", "typographic style", "vector art style", "matte painting style", "tiling pattern style", "kawaii style", "spooky style", "sticker style", "origami style", "paper craft style",
55
- "mechanical style", "biomechanical style", "industrial style", "renaissance style", "victorian style", "ancient style", "mythological style", "oil pastel style", "pencil color style", "ink wash style",
56
- "splash art style", "glowing neon style", "bokeh style"
57
  ],
58
  "Location": [
59
- "in a castle", "on the moon", "under the sea", "in a flower garden", "in snowy mountains", "in a dense jungle", "at a bustling city", "on a pirate ship", "in a haunted house", "in an underwater city",
60
- "on a floating island", "in a futuristic metropolis", "on a deserted island", "in a hidden cave", "in a magical forest", "on a distant planet", "inside a giant library", "in an ancient temple", "on a giant lily pad", "in a steampunk workshop",
61
- "at a carnival fair", "on a roller coaster", "in a wizard's tower", "inside a video game", "in the wild west", "at a cozy campsite", "in a coral reef", "inside a volcano", "on a giant beanstalk", "in a dinosaur park",
62
- "on a cloud fortress", "inside a submarine", "at a music festival", "in a secret laboratory", "on a floating raft", "inside a storybook", "in a polar research station", "on a busy train station", "in a mushroom forest", "at a lavish ballroom",
63
- "on a desert oasis", "in a labyrinth", "on a giant chessboard", "under a starlit sky", "in a parallel universe", "in a dreamland", "inside a magical mirror", "on a mystical bridge", "in a suburban neighborhood", "at a medieval marketplace",
64
- "on a giant turtle's back", "in a futuristic theme park", "on a starlight cruise", "at an underwater restaurant", "in a fairy circle", "in a floating balloon house", "at a secret garden party", "on an alien mothership", "in a thunderstorm cloud", "in a crystal cave",
65
- "at a peaceful monastery", "on a ghost ship", "in a giant bird's nest", "on a magical train ride", "in a robotic factory", "on top of a mountain peak", "inside an enchanted tree", "in a city of glass", "on a colorful coral reef", "at a petting zoo",
66
- "in a sky palace", "on an ice floe", "at a candy factory", "in a hidden fairy village", "on a space station", "in a desert canyon", "at an ancient amphitheater", "on a floating lotus", "in a bustling harbor", "inside a clock tower",
67
- "at a carnival parade", "in an alien jungle", "on a shimmering lake", "at a deep-sea trench", "inside a spaceship hangar", "on a rainbow bridge", "in a labyrinthine library", "on a sunken shipwreck", "at a mountaintop temple", "in a mirror maze",
68
- "on a giant orchard", "in a neon-lit alley", "at a giant sandcastle", "on a train through time", "in an overgrown ruin", "at an ice sculpture festival", "on a futuristic racetrack", "inside a giant bubble", "under a magical waterfall", "at a giant flower festival"
69
  ]
70
  }
71
 
 
72
  # ==========================
73
  # 2) ฟังก์ชันหลัก
74
  # ==========================
75
  def generate_words():
76
- """สุ่มคำจากแต่ละหมวด อย่างละ 1 คำ (รวม 5 คำ)"""
 
 
77
  words = []
78
  for category in word_bank:
79
  words.append(random.choice(word_bank[category]))
80
  return words
81
 
82
- def get_label_values(state):
83
- """ดึงค่าคำ (words) จาก state มาคืนเป็น list of strings"""
84
- return state["words"]
85
-
86
- def move_up(state, index):
87
- """เลื่อนตำแหน่งคำ���ึ้น 1"""
88
- if index > 0:
89
- state["words"][index], state["words"][index-1] = state["words"][index-1], state["words"][index]
90
- label_values = get_label_values(state)
91
- return [state] + label_values
92
-
93
- def move_down(state, index):
94
- """เลื่อนตำแหน่งคำลง 1"""
95
- if index < len(state["words"]) - 1:
96
- state["words"][index], state["words"][index+1] = state["words"][index+1], state["words"][index]
97
- label_values = get_label_values(state)
98
- return [state] + label_values
99
 
100
  def shuffle_words(state):
101
- """สุ่มคำใหม่ 5 คำ"""
102
- new_list = generate_words()
103
- state["words"] = new_list
104
- label_values = get_label_values(state)
105
- return [state] + label_values
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  def confirm_prompt(state):
108
- """สร้าง Prompt สุดท้ายจากลำดับคำ"""
 
 
109
  final_prompt = " ".join(state["words"])
110
  return final_prompt
111
 
112
- def init_game():
113
- """กำหนด state เริ่มต้น"""
114
- return {
115
- "words": generate_words()
116
- }
117
 
118
  # ==========================
119
- # 3) สร้าง UI + เพิ่ม CSS
120
  # ==========================
121
 
 
122
  custom_css = """
123
  body {
124
- background-color: #F9FAFF; /* สีพื้นหลังอ่อน ๆ */
125
  font-family: "Comic Sans MS", "Comic Sans", cursive;
126
  }
127
 
@@ -140,44 +100,17 @@ body {
140
  margin-bottom: 30px;
141
  }
142
 
143
- .word-row {
144
- display: flex;
145
- flex-direction: row;
146
- align-items: center;
147
- justify-content: center;
148
- margin-bottom: 10px;
149
- }
150
-
151
- /* ปุ่ม Up/Down */
152
- .word-row .gr-button {
153
- min-width: 60px;
154
- height: 60px;
155
- margin: 0 5px;
156
- font-size: 25px;
157
- border-radius: 12px;
158
- border: 2px solid #cccccc;
159
- cursor: pointer;
160
- background-color: #FFFFFF;
161
- }
162
- .word-row .gr-button:hover {
163
- background-color: #E0F7FF;
164
  }
165
-
166
- /* กล่องแสดงคำ */
167
- .word-textbox {
168
- background-color: #F8F8FF;
169
- border: 2px solid #ddd;
170
- border-radius: 8px;
171
- text-align: center;
172
- /* เพิ่ม min-height และ padding เพื่อให้ช่องใหญ่ขึ้น */
173
- min-height: 60px;
174
- padding: 5px 10px;
175
- width: 250px;
176
  }
177
 
178
- /* ปุ่มสุ่มคำใหม่ */
179
  .shuffle-btn {
180
- background-color: #FFE066; /* สีเหลืองอ่อน */
181
  border: 2px solid #FFCA28;
182
  font-weight: bold;
183
  }
@@ -185,9 +118,8 @@ body {
185
  background-color: #FFEA9A;
186
  }
187
 
188
- /* ปุ่มยืนยัน Prompt */
189
  .confirm-btn {
190
- background-color: #A5D6A7; /* สีเขียวอ่อน */
191
  border: 2px solid #66BB6A;
192
  font-weight: bold;
193
  }
@@ -195,7 +127,17 @@ body {
195
  background-color: #C8E6C9;
196
  }
197
 
198
- /* Prompt box */
 
 
 
 
 
 
 
 
 
 
199
  #prompt-box {
200
  font-size: 20px;
201
  min-height: 60px;
@@ -207,65 +149,23 @@ body {
207
  """
208
 
209
  with gr.Blocks(css=custom_css) as demo:
210
- gr.Markdown("<h1 id='title'>ZenityX Game Prompt นี้ใช่ไหมนะ?</h1>")
211
  gr.Markdown("""
212
  <p id='subtitle'>
213
- - กดปุ่ม <strong>"สุ่มคำใหม่"</strong> เพื่อดึงคำจาก 5 หมวด (หมวดละ 1 คำ)<br>
214
- - ใช้ป��่มลูกศร <strong>Up/Down</strong> เพื่อจัดเรียงลำดับคำตามต้องการ<br>
215
- - กดปุ่ม <strong>"ยืนยัน Prompt"</strong> เพื่อดูผลลัพธ์ Prompt สุดท้าย
216
  </p>
217
  """)
218
-
219
- state = gr.State(init_game)
220
 
221
- # ปุ่มสุ่มคำ
222
- btn_shuffle = gr.Button("🔄 สุ่มคำใหม่", elem_classes="shuffle-btn")
223
-
224
- labels = []
225
- for i in range(5):
226
- with gr.Row(elem_id=f"row_{i}", elem_classes="word-row"):
227
- btn_up = gr.Button("⬆️")
228
- # เพิ่ม lines=2 เพื่อให้แสดง 2 บรรทัด, interactive=False เพราะไม่ให้แก้ไข
229
- lbl = gr.Textbox(
230
- label=f"Word {i+1}",
231
- interactive=False,
232
- lines=2, # ลองปรับเป็น 3 หรือ 4 ตามต้องการ
233
- elem_classes="word-textbox"
234
- )
235
- btn_down = gr.Button("⬇️")
236
-
237
- # Event ปุ่ม Up/Down
238
- btn_up.click(fn=move_up, inputs=[state, gr.Number(i, visible=False)], outputs=[state] + labels)
239
- btn_down.click(fn=move_down, inputs=[state, gr.Number(i, visible=False)], outputs=[state] + labels)
240
- labels.append(lbl)
241
 
242
- # ปุ่มยืนยัน
243
- btn_confirm = gr.Button("✅ ยืนยัน Prompt", elem_classes="confirm-btn")
244
- final_prompt_out = gr.Textbox(
245
- label="Prompt สุดท้าย",
246
- interactive=False,
247
- elem_id="prompt-box"
248
- )
249
 
250
- # เมื่อกดปุ่มสุ่มคำ
251
- btn_shuffle.click(
252
- fn=shuffle_words,
253
- inputs=state,
254
- outputs=[state] + labels
255
- )
256
-
257
- # เมื่อกดปุ่มยืนยัน Prompt
258
- btn_confirm.click(
259
- fn=confirm_prompt,
260
- inputs=state,
261
- outputs=final_prompt_out
262
- )
263
-
264
- # โหลดค่าเริ่มต้น
265
- def on_load(s):
266
- vals = get_label_values(s)
267
- return [s] + vals
268
-
269
- demo.load(fn=on_load, inputs=state, outputs=[state] + labels)
270
-
271
- demo.launch()
 
2
  import random
3
 
4
  # ==========================
5
+ # 1) ตัวอย่างคลังคำ (หมวดละ 5 คำ)
6
+ # หากต้องการหมวดละ 100 คำ ใส่เพิ่มใน word_bank ได้เลย
7
  # ==========================
8
  word_bank = {
9
  "Character": [
10
+ "cat", "dog", "dragon", "fairy", "robot"
 
 
 
 
 
 
 
 
 
11
  ],
12
  "Adjective": [
13
+ "cute", "powerful", "strange", "magical", "bright"
 
 
 
 
 
 
 
 
 
14
  ],
15
  "Activity": [
16
+ "jumping", "playing soccer", "eating ice cream", "reading", "riding a bike"
 
 
 
 
 
 
 
 
 
17
  ],
18
  "Style": [
19
+ "cartoon style", "realistic style", "pastel style", "anime style", "pop art style"
 
 
 
 
 
 
 
 
 
 
20
  ],
21
  "Location": [
22
+ "in a castle", "on the moon", "under the sea", "in a flower garden", "in snowy mountains"
 
 
 
 
 
 
 
 
 
23
  ]
24
  }
25
 
26
+
27
  # ==========================
28
  # 2) ฟังก์ชันหลัก
29
  # ==========================
30
  def generate_words():
31
+ """
32
+ สุ่มคำจากแต่ละหมวด อย่างละ 1 คำ (รวม 5 คำ)
33
+ """
34
  words = []
35
  for category in word_bank:
36
  words.append(random.choice(word_bank[category]))
37
  return words
38
 
39
+ def init_game():
40
+ """
41
+ สร้าง state เริ่มต้นเก็บลิสต์คำ 5 คำ
42
+ """
43
+ return {
44
+ "words": generate_words()
45
+ }
 
 
 
 
 
 
 
 
 
 
46
 
47
  def shuffle_words(state):
48
+ """
49
+ สุ่มคำใหม่ทั้งหมด (5 คำ)
50
+ """
51
+ state["words"] = generate_words()
52
+ return state, state["words"]
53
+
54
+ def swap_positions(state, pos1, pos2):
55
+ """
56
+ สลับตำแหน่งคำใน state["words"] ระหว่าง pos1 กับ pos2
57
+ (pos1, pos2 รับมาจาก Dropdown ซึ่งเป็น 1..5)
58
+ """
59
+ # แปลง pos1, pos2 เป็น index (0..4)
60
+ i1 = int(pos1) - 1
61
+ i2 = int(pos2) - 1
62
+
63
+ words_list = state["words"]
64
+ # สลับค่าใน list
65
+ words_list[i1], words_list[i2] = words_list[i2], words_list[i1]
66
+
67
+ return state, words_list
68
 
69
  def confirm_prompt(state):
70
+ """
71
+ รวมคำใน state["words"] เป็น Prompt สุดท้าย
72
+ """
73
  final_prompt = " ".join(state["words"])
74
  return final_prompt
75
 
 
 
 
 
 
76
 
77
  # ==========================
78
+ # 3) สร้าง UI Gradio (แบบ Swap)
79
  # ==========================
80
 
81
+ # -- ตัวอย่าง CSS ตกแต่ง --
82
  custom_css = """
83
  body {
84
+ background-color: #F9FAFF;
85
  font-family: "Comic Sans MS", "Comic Sans", cursive;
86
  }
87
 
 
100
  margin-bottom: 30px;
101
  }
102
 
103
+ .swap-btn {
104
+ background-color: #FFCC80; /* ส้มอ่อน */
105
+ border: 2px solid #FFB74D;
106
+ font-weight: bold;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  }
108
+ .swap-btn:hover {
109
+ background-color: #FFE0B2;
 
 
 
 
 
 
 
 
 
110
  }
111
 
 
112
  .shuffle-btn {
113
+ background-color: #FFE066; /* เหลืองอ่อน */
114
  border: 2px solid #FFCA28;
115
  font-weight: bold;
116
  }
 
118
  background-color: #FFEA9A;
119
  }
120
 
 
121
  .confirm-btn {
122
+ background-color: #A5D6A7; /* เขียวอ่อน */
123
  border: 2px solid #66BB6A;
124
  font-weight: bold;
125
  }
 
127
  background-color: #C8E6C9;
128
  }
129
 
130
+ .word-box {
131
+ background-color: #F8F8FF;
132
+ border: 2px solid #ddd;
133
+ border-radius: 8px;
134
+ text-align: center;
135
+ min-height: 60px;
136
+ padding: 10px;
137
+ font-size: 18px;
138
+ width: 280px; /* ปรับความกว้างให้เห็นคำชัดเจน */
139
+ }
140
+
141
  #prompt-box {
142
  font-size: 20px;
143
  min-height: 60px;
 
149
  """
150
 
151
  with gr.Blocks(css=custom_css) as demo:
152
+ gr.Markdown("<h1 id='title'>เกมสลับตำแหน่งคำ (Swap) เพื่อสร้าง Prompt</h1>")
153
  gr.Markdown("""
154
  <p id='subtitle'>
155
+ - กดปุ่ม <strong>"สุ่มคำใหม่"</strong> เพื่อสุ่ม 5 คำ (5 หมวด)<br>
156
+ - เลือก <strong>ตำแหน่งที่ต้องการสลับ</strong> (Dropdown) แล้วกด <strong>"Swap"</strong><br>
157
+ - ทำซ้ำจนได้ลำดับที่ต้องการ จากนั้นกด <strong>"ยืนยัน Prompt"</strong>
158
  </p>
159
  """)
 
 
160
 
161
+ # สร้าง State แบบถูกต้อง
162
+ state = gr.State(value=init_game())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
+ # ปุ่มสุ่มคำใหม่
165
+ shuffle_button = gr.Button("🔄 สุ่มคำใหม่", elem_classes="shuffle-btn")
 
 
 
 
 
166
 
167
+ # แสดง 5 กล่องคำ (read-only)
168
+ word_boxes = []
169
+ for i in range(5):
170
+ word_box = gr.Textbox(label=f"Word {i+1}", interactive=False, lines=2, elem_classes="word-box")
171
+ word_boxes.append(word_b