Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ from sentences import generate_sentences
|
|
5 |
from ai_sentence import MODEL_LIST
|
6 |
from quiz import generate_fill_in_blank_exam, check_exam
|
7 |
|
|
|
|
|
|
|
8 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
9 |
try:
|
10 |
if mode == '查詢單字':
|
@@ -28,6 +31,9 @@ def process_sentence(mode, word, source, num, use_ai, model_name):
|
|
28 |
except Exception as e:
|
29 |
return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
|
30 |
|
|
|
|
|
|
|
31 |
def project_description():
|
32 |
return """
|
33 |
# 📖 VocabLine 單字例句工具
|
@@ -49,6 +55,21 @@ def project_description():
|
|
49 |
- GitHub:[https://github.com/dayuian](https://github.com/dayuian)
|
50 |
"""
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
with gr.Blocks(css="""
|
53 |
#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; }
|
54 |
.gradio-container { max-width: 800px; margin: auto; }
|
@@ -83,14 +104,12 @@ with gr.Blocks(css="""
|
|
83 |
label="選擇 AI 模型",
|
84 |
visible=False
|
85 |
)
|
86 |
-
gr.
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
label=""
|
93 |
-
)
|
94 |
|
95 |
result_output = gr.HTML(label="結果")
|
96 |
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
@@ -99,14 +118,9 @@ with gr.Blocks(css="""
|
|
99 |
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
100 |
gr.HTML("<a href='https://你的空白quiz網址' target='_blank'><button style='background-color:#ff5722;color:white;'>📝 生成小考</button></a>")
|
101 |
|
102 |
-
def switch_mode(mode):
|
103 |
-
return gr.update(visible=(mode == '查詢單字')), gr.update(visible=(mode == '隨機抽單字'))
|
104 |
-
|
105 |
-
def toggle_ai_button(use_ai):
|
106 |
-
return gr.update(visible=use_ai), gr.update(visible=use_ai)
|
107 |
-
|
108 |
mode_radio.change(switch_mode, inputs=[mode_radio], outputs=[word_input, num_input])
|
109 |
use_ai_checkbox.change(toggle_ai_button, inputs=[use_ai_checkbox], outputs=[model_dropdown, ai_warning])
|
110 |
generate_button.click(process_sentence, inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown], outputs=[result_output, status_output])
|
111 |
|
112 |
-
|
|
|
|
5 |
from ai_sentence import MODEL_LIST
|
6 |
from quiz import generate_fill_in_blank_exam, check_exam
|
7 |
|
8 |
+
# ================================
|
9 |
+
# 處理邏輯:生成單字例句
|
10 |
+
# ================================
|
11 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
12 |
try:
|
13 |
if mode == '查詢單字':
|
|
|
31 |
except Exception as e:
|
32 |
return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
|
33 |
|
34 |
+
# ================================
|
35 |
+
# 專案說明段落
|
36 |
+
# ================================
|
37 |
def project_description():
|
38 |
return """
|
39 |
# 📖 VocabLine 單字例句工具
|
|
|
55 |
- GitHub:[https://github.com/dayuian](https://github.com/dayuian)
|
56 |
"""
|
57 |
|
58 |
+
# ================================
|
59 |
+
# 模式切換:查詢 or 抽單字
|
60 |
+
# ================================
|
61 |
+
def switch_mode(mode):
|
62 |
+
return gr.update(visible=(mode == '查詢單字')), gr.update(visible=(mode == '隨機抽單字'))
|
63 |
+
|
64 |
+
# ================================
|
65 |
+
# 切換 AI 控制區塊
|
66 |
+
# ================================
|
67 |
+
def toggle_ai_button(use_ai):
|
68 |
+
return gr.update(visible=use_ai), gr.update(visible=use_ai)
|
69 |
+
|
70 |
+
# ================================
|
71 |
+
# 介面配置
|
72 |
+
# ================================
|
73 |
with gr.Blocks(css="""
|
74 |
#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; }
|
75 |
.gradio-container { max-width: 800px; margin: auto; }
|
|
|
104 |
label="選擇 AI 模型",
|
105 |
visible=False
|
106 |
)
|
107 |
+
ai_warning = gr.Textbox(
|
108 |
+
value="⚠️ 使用 AI 生成句子為功能測試,每一個單字的生成過程可能需要 30 秒以上,請耐心等待。",
|
109 |
+
visible=False,
|
110 |
+
interactive=False,
|
111 |
+
label=""
|
112 |
+
)
|
|
|
|
|
113 |
|
114 |
result_output = gr.HTML(label="結果")
|
115 |
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
|
|
118 |
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
119 |
gr.HTML("<a href='https://你的空白quiz網址' target='_blank'><button style='background-color:#ff5722;color:white;'>📝 生成小考</button></a>")
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
mode_radio.change(switch_mode, inputs=[mode_radio], outputs=[word_input, num_input])
|
122 |
use_ai_checkbox.change(toggle_ai_button, inputs=[use_ai_checkbox], outputs=[model_dropdown, ai_warning])
|
123 |
generate_button.click(process_sentence, inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown], outputs=[result_output, status_output])
|
124 |
|
125 |
+
# 啟動
|
126 |
+
demo.launch()
|