Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
from sentences import generate_sentences
|
| 4 |
from ai_sentence import MODEL_LIST
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
-
# 核心:處理單字模式 & 句子生成方式
|
| 8 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
| 9 |
try:
|
| 10 |
-
# 1️⃣ 根據模式獲取單字列表
|
| 11 |
if mode == 'query':
|
| 12 |
if not word:
|
| 13 |
return "<p style='color:red;'>❌ 請輸入單字</p>", "未輸入單字"
|
| 14 |
words = [word.strip()]
|
| 15 |
-
|
| 16 |
elif mode == 'random':
|
| 17 |
num = int(num)
|
| 18 |
if num <= 0:
|
|
@@ -20,11 +19,9 @@ def process_sentence(mode, word, source, num, use_ai, model_name):
|
|
| 20 |
words_data = get_words_from_source(source)
|
| 21 |
words = [w['word'] for w in words_data]
|
| 22 |
words = random.sample(words, num)
|
| 23 |
-
|
| 24 |
else:
|
| 25 |
return "<p style='color:red;'>❌ 模式錯誤</p>", "模式選擇異常"
|
| 26 |
|
| 27 |
-
# 2️⃣ 呼叫 sentences.py 核心處理句子的函數
|
| 28 |
result_display, status_log = generate_sentences(words, source, use_ai, model_name)
|
| 29 |
|
| 30 |
return result_display, status_log
|
|
@@ -33,13 +30,14 @@ def process_sentence(mode, word, source, num, use_ai, model_name):
|
|
| 33 |
return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
|
| 34 |
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
gr.Markdown(
|
| 39 |
"""
|
| 40 |
# 📖 VocabLine 單字例句工具
|
| 41 |
-
|
| 42 |
-
## 📝 簡介
|
| 43 |
支援單字例句查詢,AI 自動生成句子。適合作為 LINE 單字推播、英文學習輔助工具。
|
| 44 |
|
| 45 |
## 👨💻 開發者資訊
|
|
@@ -49,41 +47,41 @@ with gr.Blocks() as demo:
|
|
| 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 |
def switch_mode(mode):
|
| 88 |
if mode == 'query':
|
| 89 |
return gr.update(visible=True), gr.update(visible=False)
|
|
@@ -96,15 +94,31 @@ with gr.Blocks() as demo:
|
|
| 96 |
outputs=[word_input, num_input]
|
| 97 |
)
|
| 98 |
|
| 99 |
-
# 勾選 AI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
use_ai_checkbox.change(
|
| 101 |
-
|
| 102 |
inputs=[use_ai_checkbox],
|
| 103 |
-
outputs=[model_dropdown]
|
| 104 |
)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
submit_btn.click(
|
| 108 |
process_sentence,
|
| 109 |
inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown],
|
| 110 |
outputs=[result_output, status_output]
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
from vocab import get_sources, get_words_from_source
|
| 4 |
from sentences import generate_sentences
|
| 5 |
from ai_sentence import MODEL_LIST
|
| 6 |
+
import random
|
| 7 |
|
| 8 |
|
|
|
|
| 9 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
| 10 |
try:
|
|
|
|
| 11 |
if mode == 'query':
|
| 12 |
if not word:
|
| 13 |
return "<p style='color:red;'>❌ 請輸入單字</p>", "未輸入單字"
|
| 14 |
words = [word.strip()]
|
|
|
|
| 15 |
elif mode == 'random':
|
| 16 |
num = int(num)
|
| 17 |
if num <= 0:
|
|
|
|
| 19 |
words_data = get_words_from_source(source)
|
| 20 |
words = [w['word'] for w in words_data]
|
| 21 |
words = random.sample(words, num)
|
|
|
|
| 22 |
else:
|
| 23 |
return "<p style='color:red;'>❌ 模式錯誤</p>", "模式選擇異常"
|
| 24 |
|
|
|
|
| 25 |
result_display, status_log = generate_sentences(words, source, use_ai, model_name)
|
| 26 |
|
| 27 |
return result_display, status_log
|
|
|
|
| 30 |
return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
|
| 31 |
|
| 32 |
|
| 33 |
+
with gr.Blocks(css="""
|
| 34 |
+
.card { 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; }
|
| 35 |
+
.gradio-container { max-width: 800px; margin: auto; }
|
| 36 |
+
#generate-button.ai-active { background-color: #ff9800 !important; color: white !important; }
|
| 37 |
+
""") as demo:
|
| 38 |
gr.Markdown(
|
| 39 |
"""
|
| 40 |
# 📖 VocabLine 單字例句工具
|
|
|
|
|
|
|
| 41 |
支援單字例句查詢,AI 自動生成句子。適合作為 LINE 單字推播、英文學習輔助工具。
|
| 42 |
|
| 43 |
## 👨💻 開發者資訊
|
|
|
|
| 47 |
"""
|
| 48 |
)
|
| 49 |
|
| 50 |
+
with gr.Group():
|
| 51 |
+
with gr.Row():
|
| 52 |
+
mode_radio = gr.Radio(
|
| 53 |
+
["query", "random"],
|
| 54 |
+
label="選擇模式",
|
| 55 |
+
choices=["query", "random"],
|
| 56 |
+
value="query",
|
| 57 |
+
interactive=True
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
with gr.Box(elem_classes=["card"]):
|
| 61 |
+
word_input = gr.Textbox(label="輸入單字", visible=True)
|
| 62 |
+
num_input = gr.Number(label="抽取單字數量", value=5, visible=False)
|
| 63 |
+
source_dropdown = gr.Dropdown(
|
| 64 |
+
choices=get_sources(),
|
| 65 |
+
value="common3000",
|
| 66 |
+
label="選擇單字庫"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
with gr.Box(elem_classes=["card"]):
|
| 70 |
+
use_ai_checkbox = gr.Checkbox(label="使用 AI 生成句子", elem_id="use-ai-checkbox")
|
| 71 |
+
model_dropdown = gr.Dropdown(
|
| 72 |
+
choices=MODEL_LIST,
|
| 73 |
+
value=MODEL_LIST[0],
|
| 74 |
+
label="選擇 AI 模型",
|
| 75 |
+
visible=False
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
| 79 |
+
gr.Markdown("⚠️ 若使用 AI 生成句子,生成過程可能需要 30 秒以上,請耐心等待。")
|
| 80 |
+
|
| 81 |
+
result_output = gr.HTML(label="結果")
|
| 82 |
+
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
| 83 |
+
|
| 84 |
+
# 模式切換
|
| 85 |
def switch_mode(mode):
|
| 86 |
if mode == 'query':
|
| 87 |
return gr.update(visible=True), gr.update(visible=False)
|
|
|
|
| 94 |
outputs=[word_input, num_input]
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# 勾選 AI → 模型選擇顯示 + 按鈕變色
|
| 98 |
+
def toggle_ai_button(use_ai):
|
| 99 |
+
# 控制模型選單顯示
|
| 100 |
+
model_visibility = gr.update(visible=use_ai)
|
| 101 |
+
# 自定義 JS 變色
|
| 102 |
+
js_toggle_class = f"""
|
| 103 |
+
function toggleButtonClass() {{
|
| 104 |
+
const btn = document.getElementById('generate-button');
|
| 105 |
+
if ({'true' if use_ai else 'false'}) {{
|
| 106 |
+
btn.classList.add('ai-active');
|
| 107 |
+
}} else {{
|
| 108 |
+
btn.classList.remove('ai-active');
|
| 109 |
+
}}
|
| 110 |
+
}}
|
| 111 |
+
toggleButtonClass();
|
| 112 |
+
"""
|
| 113 |
+
return model_visibility, gr.update(_js=js_toggle_class)
|
| 114 |
+
|
| 115 |
use_ai_checkbox.change(
|
| 116 |
+
toggle_ai_button,
|
| 117 |
inputs=[use_ai_checkbox],
|
| 118 |
+
outputs=[model_dropdown, generate_button]
|
| 119 |
)
|
| 120 |
|
| 121 |
+
generate_button.click(
|
|
|
|
| 122 |
process_sentence,
|
| 123 |
inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown],
|
| 124 |
outputs=[result_output, status_output]
|