Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,11 @@ from ai_sentence import MODEL_LIST
|
|
6 |
|
7 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
8 |
try:
|
9 |
-
if mode == '
|
10 |
if not word:
|
11 |
return "<p style='color:red;'>❌ 請輸入單字</p>", "未輸入單字"
|
12 |
words = [word.strip()]
|
13 |
-
elif mode == '
|
14 |
num = int(num)
|
15 |
if num <= 0:
|
16 |
return "<p style='color:red;'>❌ 抽取數量須大於0</p>", "數量錯誤"
|
@@ -55,17 +55,16 @@ def project_description():
|
|
55 |
with gr.Blocks(css="""
|
56 |
#card-group { padding: 15px; border-radius: 12px; background-color: rgba(255, 255, 255, 0.05); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); margin-bottom: 15px; }
|
57 |
.gradio-container { max-width: 800px; margin: auto; }
|
58 |
-
#generate-button.ai-active { background-color: #ff9800 !important; color: white !important; }
|
59 |
""") as demo:
|
60 |
gr.Markdown(project_description())
|
61 |
|
62 |
with gr.Group():
|
63 |
with gr.Row():
|
64 |
mode_radio = gr.Radio(
|
65 |
-
["
|
66 |
label="選擇模式",
|
67 |
-
choices=["
|
68 |
-
value="
|
69 |
interactive=True
|
70 |
)
|
71 |
|
@@ -79,14 +78,14 @@ with gr.Blocks(css="""
|
|
79 |
)
|
80 |
|
81 |
with gr.Group(elem_id="card-group"):
|
82 |
-
use_ai_checkbox = gr.Checkbox(label="使用 AI
|
83 |
model_dropdown = gr.Dropdown(
|
84 |
choices=MODEL_LIST,
|
85 |
value=MODEL_LIST[0],
|
86 |
label="選擇 AI 模型",
|
87 |
visible=False
|
88 |
)
|
89 |
-
ai_warning = gr.
|
90 |
|
91 |
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
92 |
|
@@ -94,7 +93,7 @@ with gr.Blocks(css="""
|
|
94 |
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
95 |
|
96 |
def switch_mode(mode):
|
97 |
-
if mode == '
|
98 |
return gr.update(visible=True), gr.update(visible=False)
|
99 |
else:
|
100 |
return gr.update(visible=False), gr.update(visible=True)
|
@@ -108,23 +107,12 @@ with gr.Blocks(css="""
|
|
108 |
def toggle_ai_button(use_ai):
|
109 |
model_visibility = gr.update(visible=use_ai)
|
110 |
warning_visibility = gr.update(visible=use_ai)
|
111 |
-
|
112 |
-
function toggleButtonClass() {{
|
113 |
-
const btn = document.getElementById('generate-button');
|
114 |
-
if ({'true' if use_ai else 'false'}) {{
|
115 |
-
btn.classList.add('ai-active');
|
116 |
-
}} else {{
|
117 |
-
btn.classList.remove('ai-active');
|
118 |
-
}}
|
119 |
-
}}
|
120 |
-
toggleButtonClass();
|
121 |
-
"""
|
122 |
-
return model_visibility, gr.update(_js=js_toggle_class), warning_visibility
|
123 |
|
124 |
use_ai_checkbox.change(
|
125 |
toggle_ai_button,
|
126 |
inputs=[use_ai_checkbox],
|
127 |
-
outputs=[model_dropdown,
|
128 |
)
|
129 |
|
130 |
generate_button.click(
|
@@ -133,4 +121,4 @@ with gr.Blocks(css="""
|
|
133 |
outputs=[result_output, status_output]
|
134 |
)
|
135 |
|
136 |
-
demo.launch()
|
|
|
6 |
|
7 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
8 |
try:
|
9 |
+
if mode == '查詢單字':
|
10 |
if not word:
|
11 |
return "<p style='color:red;'>❌ 請輸入單字</p>", "未輸入單字"
|
12 |
words = [word.strip()]
|
13 |
+
elif mode == '隨機抽單字':
|
14 |
num = int(num)
|
15 |
if num <= 0:
|
16 |
return "<p style='color:red;'>❌ 抽取數量須大於0</p>", "數量錯誤"
|
|
|
55 |
with gr.Blocks(css="""
|
56 |
#card-group { padding: 15px; border-radius: 12px; background-color: rgba(255, 255, 255, 0.05); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); margin-bottom: 15px; }
|
57 |
.gradio-container { max-width: 800px; margin: auto; }
|
|
|
58 |
""") as demo:
|
59 |
gr.Markdown(project_description())
|
60 |
|
61 |
with gr.Group():
|
62 |
with gr.Row():
|
63 |
mode_radio = gr.Radio(
|
64 |
+
["查詢單字", "隨機抽單字"],
|
65 |
label="選擇模式",
|
66 |
+
choices=["查詢單字", "隨機抽單字"],
|
67 |
+
value="查詢單字",
|
68 |
interactive=True
|
69 |
)
|
70 |
|
|
|
78 |
)
|
79 |
|
80 |
with gr.Group(elem_id="card-group"):
|
81 |
+
use_ai_checkbox = gr.Checkbox(label="使用 AI 生成句子(較慢,約 30 秒)", elem_id="use-ai-checkbox")
|
82 |
model_dropdown = gr.Dropdown(
|
83 |
choices=MODEL_LIST,
|
84 |
value=MODEL_LIST[0],
|
85 |
label="選擇 AI 模型",
|
86 |
visible=False
|
87 |
)
|
88 |
+
ai_warning = gr.Textbox(value="⚠️ 若使用 AI 生成句子,生成過程可能需要 30 秒以上,請耐心等待。", visible=False, interactive=False, label="")
|
89 |
|
90 |
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
91 |
|
|
|
93 |
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
94 |
|
95 |
def switch_mode(mode):
|
96 |
+
if mode == '查詢單字':
|
97 |
return gr.update(visible=True), gr.update(visible=False)
|
98 |
else:
|
99 |
return gr.update(visible=False), gr.update(visible=True)
|
|
|
107 |
def toggle_ai_button(use_ai):
|
108 |
model_visibility = gr.update(visible=use_ai)
|
109 |
warning_visibility = gr.update(visible=use_ai)
|
110 |
+
return model_visibility, warning_visibility
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
use_ai_checkbox.change(
|
113 |
toggle_ai_button,
|
114 |
inputs=[use_ai_checkbox],
|
115 |
+
outputs=[model_dropdown, ai_warning]
|
116 |
)
|
117 |
|
118 |
generate_button.click(
|
|
|
121 |
outputs=[result_output, status_output]
|
122 |
)
|
123 |
|
124 |
+
demo.launch()
|