NBLM / app.py
youngtsai's picture
gcs
a8f6954
raw
history blame
5.67 kB
import gradio as gr
import os
import shutil
import tempfile
from openai import OpenAI
from storage_service import GoogleCloudStorage
from google.oauth2.service_account import Credentials
import vertexai
from vertexai.generative_models import GenerativeModel, Part
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()