File size: 5,465 Bytes
b55d531
b0bbdeb
 
e501f08
b55d531
9ddb9de
b55d531
 
 
 
 
 
9ddb9de
7ed7122
 
 
b55d531
 
 
 
 
0bba696
 
e501f08
 
 
b0bbdeb
 
 
0bba696
b0bbdeb
 
0bba696
b0bbdeb
0bba696
c74a768
b023203
7ed7122
d60ae3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da05110
 
 
 
 
 
 
 
 
b55d531
 
 
f3231bf
20111b1
 
 
f3231bf
7377ef6
 
 
 
 
 
 
 
 
 
ff365f4
7377ef6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b55d531
c74a768
 
 
5737001
48312c4
9ddb9de
b55d531
 
ff365f4
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
115
116
117
import gradio as gr
import os
import shutil
import tempfile

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 add_to_file_list(file, file_list):
    if file:
        temp_dir = tempfile.gettempdir()
        temp_path = os.path.join(temp_dir, os.path.basename(file.name))
        shutil.copy(file.name, temp_path)  # 將文件存儲到臨時目錄
        file_list.append(temp_path)
    display_list = [os.path.basename(path) for path in file_list]
    return gr.update(choices=display_list), None  # 清空文件選擇框

def process_selected_files(selected_files, file_list):
    selected_paths = [path for path in file_list if os.path.basename(path) in selected_files]
    # 假資料模擬處理 RAG
    return f"已處理的文件: {', '.join(selected_paths)}"

def toggle_visibility(toggle_value):
    return gr.update(visible=toggle_value)

def get_youtube_playlist():
    # 假資料模擬 YouTube 播放清單
    return [
        {"id": "yPmgHBRUdns", "title": "【觀念】比與比值"},
        {"id": "CgLdZpnr_h8", "title": "【觀念】相等的比"},
        {"id": "-7HVxER-rb0", "title": "【觀念】比例式的運算性質"},
    ]

def format_youtube_choices(youtube_data):
    return [f"{item['id']} - {item['title']}" for item in youtube_data]

def process_selected_videos(selected_videos):
    # 假資料模擬處理選擇的影片
    return f"已選擇的影片: {', '.join(selected_videos)}"

def add_youtube_to_list(youtube_link, file_list):
  if youtube_link:
    file_list.append(youtube_link)
    display_list = [os.path.basename(path) if os.path.basename(path) else path for path in file_list]
  return gr.update(choices=display_list), ""

def process_all_files(file_list):
  return f"已處理的文件: {', '.join(file_list)}"

with gr.Blocks() as demo:
    gr.Markdown("# AI Notes Assistant")

    with gr.Row():
        source_toggle = gr.Checkbox(label="顯示來源選單", value=True)
        chat_toggle = gr.Checkbox(label="顯示對話區域", value=True)
        feature_toggle = gr.Checkbox(label="顯示功能卡片", value=True)

    with gr.Row():
        with gr.Column(visible=True) as source_column:
            gr.Markdown("### 來源選單")
            
            file_list = gr.State([])
            
            with gr.Tab("上傳檔案"):
                upload_file = gr.File(label="從電腦添加文件", file_types=[".txt", ".pdf", ".docx"])
                add_file_button = gr.Button("添加到來源列表")
                add_file_button.click(add_to_file_list, inputs=[upload_file, file_list], outputs=[file_list, upload_file])
        
            with gr.Tab("YouTube 連結"):
                youtube_link = gr.Textbox(label="輸入 YouTube 連結")
                add_youtube_button = gr.Button("添加到來源列表")
                add_youtube_button.click(add_youtube_to_list, inputs=[youtube_link, file_list], outputs=[file_list, youtube_link])

            file_display = gr.CheckboxGroup(label="已上傳的文件", interactive=True)

            process_files_button = gr.Button("處理檔案")
            rag_result = gr.Textbox(label="處理結果", interactive=False)

            process_files_button.click(process_all_files, inputs=[file_list], outputs=[rag_result])
            file_list.change(lambda x: gr.update(choices = [os.path.basename(path) if os.path.basename(path) else path for path in x]), inputs=file_list, outputs=file_display)

        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_toggle.change(toggle_visibility, inputs=source_toggle, outputs=source_column)
    chat_toggle.change(toggle_visibility, inputs=chat_toggle, outputs=chat_column)
    feature_toggle.change(toggle_visibility, inputs=feature_toggle, outputs=feature_column)

    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()