File size: 3,507 Bytes
f6bff5d
ebb3019
6180cc7
 
13a1b41
2a9478d
6180cc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebb3019
6180cc7
 
13a1b41
6180cc7
13a1b41
6180cc7
 
ebb3019
6180cc7
 
 
 
 
 
 
 
 
ebb3019
 
 
 
 
 
 
 
 
 
 
 
 
 
6180cc7
ebb3019
 
 
6180cc7
 
 
 
 
 
 
 
ebb3019
 
6180cc7
 
 
 
 
 
 
ebb3019
 
6180cc7
 
 
 
 
 
 
 
 
 
 
 
ebb3019
6180cc7
ebb3019
6180cc7
ebb3019
 
6180cc7
ebb3019
6180cc7
ebb3019
 
2a9478d
f6bff5d
6180cc7
 
 
 
 
 
 
f6bff5d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import gradio as gr
from vocab import get_sources, get_words_from_source, get_word_info
from sentences import generate_sentences
from ai_sentence import MODEL_LIST


# 核心:處理單字模式 & 句子生成方式
def process_sentence(mode, word, source, num, use_ai, model_name):
    try:
        # 1️⃣ 根據模式獲取單字列表
        if mode == 'query':
            if not word:
                return "<p style='color:red;'>❌ 請輸入單字</p>", "未輸入單字"
            words = [word.strip()]

        elif mode == 'random':
            num = int(num)
            if num <= 0:
                return "<p style='color:red;'>❌ 抽取數量須大於0</p>", "數量錯誤"
            words_data = get_words_from_source(source)
            words = [w['word'] for w in words_data]
            words = random.sample(words, num)

        else:
            return "<p style='color:red;'>❌ 模式錯誤</p>", "模式選擇異常"

        # 2️⃣ 呼叫 sentences.py 核心處理句子的函數
        result_display, status_log = generate_sentences(words, source, use_ai, model_name)

        return result_display, status_log

    except Exception as e:
        return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"


# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown(
        """
        # 📖 VocabLine 單字例句工具

        ## 📝 簡介
        支援單字例句查詢,AI 自動生成句子。適合作為 LINE 單字推播、英文學習輔助工具。

        ## 👨‍💻 開發者資訊
        - 開發者:余彦志 (大宇 ian)
        - 信箱:[email protected]
        - GitHub:[https://github.com/dayuian](https://github.com/dayuian)
        """
    )

    # 模式選擇
    mode_radio = gr.Radio(
        ["query", "random"],
        label="選擇模式",
        choices=["query", "random"],
        value="query",
        interactive=True
    )

    # 查詢模式區塊
    word_input = gr.Textbox(label="輸入單字", visible=True)
    num_input = gr.Number(label="抽取單字數量", value=5, visible=False)

    # 單字庫選擇
    source_dropdown = gr.Dropdown(
        choices=get_sources(),
        value="common3000",
        label="選擇單字庫"
    )

    # AI 控制區塊
    use_ai_checkbox = gr.Checkbox(label="使用 AI 生成句子")
    model_dropdown = gr.Dropdown(
        choices=MODEL_LIST,
        value=MODEL_LIST[0],
        label="選擇 AI 模型",
        visible=False
    )

    # 按鈕 & 結果顯示
    submit_btn = gr.Button("生成句子")
    result_output = gr.HTML(label="結果")
    status_output = gr.Textbox(label="處理狀態", interactive=False)

    # 模式切換時,顯示不同輸入框
    def switch_mode(mode):
        if mode == 'query':
            return gr.update(visible=True), gr.update(visible=False)
        else:
            return gr.update(visible=False), gr.update(visible=True)

    mode_radio.change(
        switch_mode,
        inputs=[mode_radio],
        outputs=[word_input, num_input]
    )

    # 勾選 AI 時,顯示模型選擇
    use_ai_checkbox.change(
        lambda x: gr.update(visible=x),
        inputs=[use_ai_checkbox],
        outputs=[model_dropdown]
    )

    # 按鈕綁定處理函數
    submit_btn.click(
        process_sentence,
        inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown],
        outputs=[result_output, status_output]
    )

demo.launch()