Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
-
#
|
5 |
model_name = "Helsinki-NLP/opus-mt-th-en"
|
6 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
7 |
model = MarianMTModel.from_pretrained(model_name)
|
@@ -15,7 +15,7 @@ def translate_th_to_en(th_text: str) -> str:
|
|
15 |
en_text = tokenizer.decode(translation_tokens[0], skip_special_tokens=True)
|
16 |
return en_text
|
17 |
|
18 |
-
#
|
19 |
body_shapes_th = [
|
20 |
"ตัวเหนียวขยุกขยิก", "ตัวปุกปุยกลม", "ตัวเต็มไปด้วยหนาม",
|
21 |
"ตัวเล็กเหมือนแมลง", "ตัวโปร่งแสงลอยได้"
|
@@ -42,7 +42,7 @@ moods_th = [
|
|
42 |
"ซนเป็นลิง", "ขี้อาย"
|
43 |
]
|
44 |
|
45 |
-
#
|
46 |
def generate_monster_lab(
|
47 |
body_dd, body_tb,
|
48 |
head_dd, head_tb,
|
@@ -51,12 +51,7 @@ def generate_monster_lab(
|
|
51 |
ability_dd, ability_tb,
|
52 |
mood_dd, mood_tb,
|
53 |
):
|
54 |
-
|
55 |
-
ตรวจสอบว่าใน textbox แต่ละหมวดมีข้อความไหม
|
56 |
-
ถ้ามี ให้ใช้ข้อความนั้นแทนค่าใน dropdown
|
57 |
-
ถ้า textbox ว่าง ก็ใช้ค่าจาก dropdown
|
58 |
-
"""
|
59 |
-
# (a) ตัดสินใจเลือกค่า "ภาษาไทย" แต่ละหมวด
|
60 |
body_th = body_tb.strip() if body_tb.strip() else body_dd
|
61 |
head_th = head_tb.strip() if head_tb.strip() else head_dd
|
62 |
arms_th = arms_tb.strip() if arms_tb.strip() else arms_dd
|
@@ -64,14 +59,12 @@ def generate_monster_lab(
|
|
64 |
ability_th = ability_tb.strip() if ability_tb.strip() else ability_dd
|
65 |
mood_th = mood_tb.strip() if mood_tb.strip() else mood_dd
|
66 |
|
67 |
-
# (b) สร้าง “รายละเอียด (ไทย)”
|
68 |
desc_th = (
|
69 |
f"มอนสเตอร์อารมณ์{mood_th} มี{body_th} "
|
70 |
f"พร้อมด้วย{head_th}, {arms_th}, "
|
71 |
f"ผิว/ลายแบบ{skin_th}, และพลังพิเศษคือ{ability_th}!"
|
72 |
)
|
73 |
|
74 |
-
# (c) แปล -> อังกฤษ
|
75 |
body_en = translate_th_to_en(body_th)
|
76 |
head_en = translate_th_to_en(head_th)
|
77 |
arms_en = translate_th_to_en(arms_th)
|
@@ -79,7 +72,6 @@ def generate_monster_lab(
|
|
79 |
ability_en = translate_th_to_en(ability_th)
|
80 |
mood_en = translate_th_to_en(mood_th)
|
81 |
|
82 |
-
# (d) ประกอบ Prompt อังกฤษ
|
83 |
prompt_en = (
|
84 |
f"This is a {mood_en} monster with {body_en}, "
|
85 |
f"a {head_en}, {arms_en}, covered in {skin_en} skin, "
|
@@ -88,7 +80,7 @@ def generate_monster_lab(
|
|
88 |
|
89 |
return desc_th, prompt_en
|
90 |
|
91 |
-
#
|
92 |
css_code = """
|
93 |
body {
|
94 |
background-color: #FFF7EA;
|
@@ -133,16 +125,39 @@ body {
|
|
133 |
"""
|
134 |
|
135 |
def initial_text():
|
136 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
with gr.Blocks(css=css_code) as demo:
|
139 |
-
gr.Markdown("<h1 id='title'>ZenityX Monster Lab (Dropdown + Textbox)</h1>")
|
140 |
gr.Markdown("""
|
141 |
<div class="game-desc">
|
142 |
-
<p>หนูน้อยจ๊ะ
|
143 |
-
|
144 |
-
<p
|
145 |
-
<p>อย่าลืมกดปุ่ม <strong>"Copy Prompt"</strong> ถ้าอยากคัดลอกไปใช้ใน AI สร้างภาพด้วยล่ะ!</p>
|
146 |
</div>
|
147 |
""")
|
148 |
|
@@ -168,15 +183,14 @@ with gr.Blocks(css=css_code) as demo:
|
|
168 |
mood_dd = gr.Dropdown(moods_th, label="อารมณ์ (Dropdown)", value=moods_th[0])
|
169 |
mood_tb = gr.Textbox(label="หรือพิมพ์เอง (Mood)", placeholder="ขี้เล่นเป็นพิเศษ...")
|
170 |
|
171 |
-
|
172 |
-
with gr.Row():
|
173 |
-
create_btn = gr.Button("สร้างมอนสเตอร์!", elem_classes="monster-btn")
|
174 |
-
copy_btn = gr.Button("Copy Prompt", elem_classes="monster-btn")
|
175 |
|
176 |
monster_desc_th = gr.Textbox(label="รายละเอียด (ภาษาไทย)", interactive=False, elem_id="desc-th")
|
177 |
monster_prompt_en = gr.Textbox(label="Prompt (English)", interactive=False, elem_id="prompt-en")
|
178 |
|
179 |
-
#
|
|
|
|
|
180 |
create_btn.click(
|
181 |
fn=generate_monster_lab,
|
182 |
inputs=[
|
@@ -190,16 +204,6 @@ with gr.Blocks(css=css_code) as demo:
|
|
190 |
outputs=[monster_desc_th, monster_prompt_en]
|
191 |
)
|
192 |
|
193 |
-
# Event: กดปุ่ม "Copy Prompt" -> ใช้ JS copy to clipboard
|
194 |
-
# inputs: monster_prompt_en (string)
|
195 |
-
# no outputs
|
196 |
-
copy_btn.click(
|
197 |
-
fn=None,
|
198 |
-
_js="function(txt) {navigator.clipboard.writeText(txt);}",
|
199 |
-
inputs=monster_prompt_en,
|
200 |
-
outputs=[]
|
201 |
-
)
|
202 |
-
|
203 |
demo.load(fn=initial_text, inputs=None, outputs=monster_desc_th)
|
204 |
|
205 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
+
# 1) โหลดโมเดลแปล (MarianMT)
|
5 |
model_name = "Helsinki-NLP/opus-mt-th-en"
|
6 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
7 |
model = MarianMTModel.from_pretrained(model_name)
|
|
|
15 |
en_text = tokenizer.decode(translation_tokens[0], skip_special_tokens=True)
|
16 |
return en_text
|
17 |
|
18 |
+
# 2) ข้อมูล (ภาษาไทย) สำหรับ Dropdown
|
19 |
body_shapes_th = [
|
20 |
"ตัวเหนียวขยุกขยิก", "ตัวปุกปุยกลม", "ตัวเต็มไปด้วยหนาม",
|
21 |
"ตัวเล็กเหมือนแมลง", "ตัวโปร่งแสงลอยได้"
|
|
|
42 |
"ซนเป็นลิง", "ขี้อาย"
|
43 |
]
|
44 |
|
45 |
+
# 3) ฟังก์ชันสร้าง Prompt
|
46 |
def generate_monster_lab(
|
47 |
body_dd, body_tb,
|
48 |
head_dd, head_tb,
|
|
|
51 |
ability_dd, ability_tb,
|
52 |
mood_dd, mood_tb,
|
53 |
):
|
54 |
+
# เลือกค่าจาก textbox หากไม่ว่าง, มิฉะนั้นใช้ dropdown
|
|
|
|
|
|
|
|
|
|
|
55 |
body_th = body_tb.strip() if body_tb.strip() else body_dd
|
56 |
head_th = head_tb.strip() if head_tb.strip() else head_dd
|
57 |
arms_th = arms_tb.strip() if arms_tb.strip() else arms_dd
|
|
|
59 |
ability_th = ability_tb.strip() if ability_tb.strip() else ability_dd
|
60 |
mood_th = mood_tb.strip() if mood_tb.strip() else mood_dd
|
61 |
|
|
|
62 |
desc_th = (
|
63 |
f"มอนสเตอร์อารมณ์{mood_th} มี{body_th} "
|
64 |
f"พร้อมด้วย{head_th}, {arms_th}, "
|
65 |
f"ผิว/ลายแบบ{skin_th}, และพลังพิเศษคือ{ability_th}!"
|
66 |
)
|
67 |
|
|
|
68 |
body_en = translate_th_to_en(body_th)
|
69 |
head_en = translate_th_to_en(head_th)
|
70 |
arms_en = translate_th_to_en(arms_th)
|
|
|
72 |
ability_en = translate_th_to_en(ability_th)
|
73 |
mood_en = translate_th_to_en(mood_th)
|
74 |
|
|
|
75 |
prompt_en = (
|
76 |
f"This is a {mood_en} monster with {body_en}, "
|
77 |
f"a {head_en}, {arms_en}, covered in {skin_en} skin, "
|
|
|
80 |
|
81 |
return desc_th, prompt_en
|
82 |
|
83 |
+
# 4) สร้าง UI Gradio (ไม่มีปุ่ม Copy ใน .click() แต่ใช้ HTML ปุ่ม)
|
84 |
css_code = """
|
85 |
body {
|
86 |
background-color: #FFF7EA;
|
|
|
125 |
"""
|
126 |
|
127 |
def initial_text():
|
128 |
+
return "ยังไม่ได้สร้างมอนสเตอร์ ลองเลือกหรือพิมพ์ แล้วกด 'สร้างมอนสเตอร์!'"
|
129 |
+
|
130 |
+
# สร้างปุ่ม Copy ด้วย HTML + JS
|
131 |
+
copy_button_html = """
|
132 |
+
<button style="background-color: #F06292; border: 2px solid #E91E63; font-weight: bold;
|
133 |
+
font-size: 1.1rem; padding: 10px 20px; border-radius: 10px;"
|
134 |
+
onclick="copyPromptText()">
|
135 |
+
Copy Prompt
|
136 |
+
</button>
|
137 |
+
|
138 |
+
<script>
|
139 |
+
function copyPromptText() {
|
140 |
+
// หยิบ Textbox 'prompt-en' (ภายใน Gradio) โดย id
|
141 |
+
const promptBox = document.querySelector('#prompt-en textarea');
|
142 |
+
if (!promptBox) {
|
143 |
+
alert('ไม่พบข้อความ Prompt!');
|
144 |
+
return;
|
145 |
+
}
|
146 |
+
// Copy
|
147 |
+
const promptText = promptBox.value;
|
148 |
+
navigator.clipboard.writeText(promptText);
|
149 |
+
alert('คัดลอก Prompt แล้ว!');
|
150 |
+
}
|
151 |
+
</script>
|
152 |
+
"""
|
153 |
|
154 |
with gr.Blocks(css=css_code) as demo:
|
155 |
+
gr.Markdown("<h1 id='title'>ZenityX Monster Lab (Dropdown + Textbox + Copy HTML)</h1>")
|
156 |
gr.Markdown("""
|
157 |
<div class="game-desc">
|
158 |
+
<p>หนูน้อยจ๊ะ เลือกได้ว่าจะใช้ค่าใน <strong>Dropdown</strong> หรือจะ <strong>พิมพ์เอง</strong></p>
|
159 |
+
<p>เมื่อกด <strong>"สร้างมอนสเตอร์!"</strong> แล้วจะได้ <i>รายละเอียด (ภาษาไทย)</i> และ <i>Prompt ภาษาอังกฤษ</i></p>
|
160 |
+
<p>ปุ่ม <strong>"Copy Prompt"</strong> ด้านล่าง จะคัดลอกข้อความในช่อง Prompt เพื่อให้ไปวางใน AI อื่นได้ง่าย</p>
|
|
|
161 |
</div>
|
162 |
""")
|
163 |
|
|
|
183 |
mood_dd = gr.Dropdown(moods_th, label="อารมณ์ (Dropdown)", value=moods_th[0])
|
184 |
mood_tb = gr.Textbox(label="หรือพิมพ์เอง (Mood)", placeholder="ขี้เล่นเป็นพิเศษ...")
|
185 |
|
186 |
+
create_btn = gr.Button("สร้างมอนสเตอร์!", elem_classes="monster-btn")
|
|
|
|
|
|
|
187 |
|
188 |
monster_desc_th = gr.Textbox(label="รายละเอียด (ภาษาไทย)", interactive=False, elem_id="desc-th")
|
189 |
monster_prompt_en = gr.Textbox(label="Prompt (English)", interactive=False, elem_id="prompt-en")
|
190 |
|
191 |
+
# แทรกปุ่ม Copy ด้วย HTML
|
192 |
+
gr.HTML(copy_button_html)
|
193 |
+
|
194 |
create_btn.click(
|
195 |
fn=generate_monster_lab,
|
196 |
inputs=[
|
|
|
204 |
outputs=[monster_desc_th, monster_prompt_en]
|
205 |
)
|
206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
demo.load(fn=initial_text, inputs=None, outputs=monster_desc_th)
|
208 |
|
209 |
demo.launch()
|