|
import gradio as gr |
|
|
|
def mock_question_answer(question, history): |
|
|
|
answers = { |
|
"文件的核心觀點是什麼?": "這份文件的核心觀點是關於人工智慧如何提升工作效率。", |
|
"有哪些關鍵詞或數據?": "關鍵詞包括:人工智慧、工作效率、數據分析。", |
|
"文件的摘要是什麼?": "這份文件討論了如何利用人工智慧工具,提升企業的運營效率和決策速度。" |
|
} |
|
response = answers.get(question, "抱歉,我無法回答這個問題。請嘗試其他問題!") |
|
history.append({"role": "user", "content": question}) |
|
history.append({"role": "assistant", "content": response}) |
|
return history, "" |
|
|
|
def mock_summary(): |
|
|
|
return "這份文件主要討論人工智慧在工作效率提升方面的應用,並提供了實際案例來說明其價值。" |
|
|
|
def mock_sources(): |
|
|
|
return ["來源一:時間的四則問題", "來源二:新文章"] |
|
|
|
def toggle_visibility(current_state): |
|
return gr.update(visible=not current_state) |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# AI Notes Assistant") |
|
|
|
with gr.Row(): |
|
toggle_sources = gr.Button("顯示/隱藏 來源選單") |
|
toggle_chat = gr.Button("顯示/隱藏 對話區域") |
|
toggle_features = gr.Button("顯示/隱藏 功能卡片") |
|
|
|
with gr.Row(): |
|
with gr.Column(visible=True) as source_column: |
|
gr.Markdown("### 來源選單") |
|
sources = gr.CheckboxGroup( |
|
choices=mock_sources(), label="選取所有來源", interactive=True |
|
) |
|
upload_file = gr.File(label="從電腦添加文件", file_types=[".txt", ".pdf", ".docx"]) |
|
|
|
with gr.Column(visible=True) as chat_column: |
|
gr.Markdown("### 對話區域") |
|
chatbot = gr.Chatbot(label="聊天記錄", type="messages") |
|
question = gr.Textbox(label="輸入問題,例如:文件的核心觀點是什麼?") |
|
ask_button = gr.Button("提問") |
|
|
|
with gr.Column(visible=True) as feature_column: |
|
gr.Markdown("### 功能卡片") |
|
with gr.Tab("摘要生成"): |
|
summary_button = gr.Button("生成摘要") |
|
summary = gr.Textbox(label="摘要", interactive=False) |
|
with gr.Tab("其他功能"): |
|
gr.Markdown("此處可以添加更多功能卡片") |
|
|
|
source_visible = gr.State(True) |
|
chat_visible = gr.State(True) |
|
feature_visible = gr.State(True) |
|
|
|
toggle_sources.click(toggle_visibility, inputs=source_visible, outputs=[source_column, source_visible]) |
|
toggle_chat.click(toggle_visibility, inputs=chat_visible, outputs=[chat_column, chat_visible]) |
|
toggle_features.click(toggle_visibility, inputs=feature_visible, outputs=[feature_column, feature_visible]) |
|
|
|
history = gr.State([]) |
|
ask_button.click(mock_question_answer, inputs=[question, history], outputs=[chatbot, chatbot]) |
|
summary_button.click(mock_summary, inputs=[], outputs=[summary]) |
|
|
|
demo.launch() |
|
|