tsengiii commited on
Commit
469385b
·
verified ·
1 Parent(s): cc86ade

Upload 2 files

Browse files
Files changed (2) hide show
  1. app (3).py +293 -0
  2. requirements (4).txt +9 -0
app (3).py ADDED
@@ -0,0 +1,293 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import os
4
+ import openai
5
+ import gradio as gr
6
+ from langchain.chains import ConversationalRetrievalChain
7
+ from langchain.text_splitter import CharacterTextSplitter
8
+ from langchain_community.document_loaders import PyMuPDFLoader, PyPDFLoader
9
+ from langchain.vectorstores import Chroma
10
+ from langchain_community.embeddings import OpenAIEmbeddings
11
+ from langchain_community.chat_models import ChatOpenAI
12
+ import shutil # 用於文件複製
13
+ import logging
14
+
15
+ # 設置日誌配置
16
+ logging.basicConfig(level=logging.INFO)
17
+ logger = logging.getLogger(__name__)
18
+
19
+ # 獲取 OpenAI API 密鑰(初始不使用固定密鑰)
20
+ api_key_env = os.getenv("OPENAI_API_KEY")
21
+ if api_key_env:
22
+ openai.api_key = api_key_env
23
+ logger.info("OpenAI API 密鑰已設置。")
24
+ else:
25
+ logger.info("未設置固定的 OpenAI API 密鑰。將使用使用者提供的密鑰。")
26
+
27
+ # 確保向量資料庫目錄存在且有寫入權限
28
+ VECTORDB_DIR = os.path.abspath("./data")
29
+ os.makedirs(VECTORDB_DIR, exist_ok=True)
30
+ os.chmod(VECTORDB_DIR, 0o755) # 設置適當的權限
31
+ logger.info(f"VECTORDB_DIR set to: {VECTORDB_DIR}")
32
+
33
+ # 定義測試 PDF 加載器的函數
34
+ def test_pdf_loader(file_path, loader_type='PyMuPDFLoader'):
35
+ logger.info(f"Testing PDF loader ({loader_type}) with file: {file_path}")
36
+ try:
37
+ if loader_type == 'PyMuPDFLoader':
38
+ loader = PyMuPDFLoader(file_path)
39
+ elif loader_type == 'PyPDFLoader':
40
+ loader = PyPDFLoader(file_path)
41
+ else:
42
+ logger.error(f"Unknown loader type: {loader_type}")
43
+ return
44
+ loaded_docs = loader.load()
45
+ if loaded_docs:
46
+ logger.info(f"Successfully loaded {file_path} with {len(loaded_docs)} documents.")
47
+ logger.info(f"Document content (first 500 chars): {loaded_docs[0].page_content[:500]}")
48
+ else:
49
+ logger.error(f"No documents loaded from {file_path}.")
50
+ except Exception as e:
51
+ logger.error(f"Error loading {file_path} with {loader_type}: {e}")
52
+
53
+ # 定義載入和處理 PDF 文件的函數
54
+ def load_and_process_documents(file_paths, loader_type='PyMuPDFLoader', api_key=None):
55
+ if not api_key:
56
+ raise ValueError("未提供 OpenAI API 密鑰。")
57
+ documents = []
58
+ logger.info("開始載入上傳的 PDF 文件。")
59
+
60
+ for file_path in file_paths:
61
+ logger.info(f"載入 PDF 文件: {file_path}")
62
+ if not os.path.exists(file_path):
63
+ logger.error(f"文件不存在: {file_path}")
64
+ continue
65
+ try:
66
+ if loader_type == 'PyMuPDFLoader':
67
+ loader = PyMuPDFLoader(file_path)
68
+ elif loader_type == 'PyPDFLoader':
69
+ loader = PyPDFLoader(file_path)
70
+ else:
71
+ logger.error(f"Unknown loader type: {loader_type}")
72
+ continue
73
+ loaded_docs = loader.load()
74
+ if loaded_docs:
75
+ logger.info(f"載入 {file_path} 成功,包含 {len(loaded_docs)} 個文檔。")
76
+ # 打印第一個文檔的部分內容以確認
77
+ logger.info(f"第一個文檔內容: {loaded_docs[0].page_content[:500]}")
78
+ documents.extend(loaded_docs)
79
+ else:
80
+ logger.error(f"載入 {file_path} 但未找到任何文檔。")
81
+ except Exception as e:
82
+ logger.error(f"載入 {file_path} 時出現錯誤: {e}")
83
+
84
+ if not documents:
85
+ raise ValueError("沒有找到任何 PDF 文件或 PDF 文件無法載入。")
86
+ else:
87
+ logger.info(f"總共載入了 {len(documents)} 個文檔。")
88
+
89
+ # 分割長文本
90
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=50)
91
+ documents = text_splitter.split_documents(documents)
92
+ logger.info(f"分割後的文檔數量: {len(documents)}")
93
+
94
+ if not documents:
95
+ raise ValueError("分割後的文檔列表為空。請檢查 PDF 文件內容。")
96
+
97
+ # 初始化向量資料庫
98
+ try:
99
+ embeddings = OpenAIEmbeddings(openai_api_key=api_key) # 使用使用者的 API 密鑰
100
+ logger.info("初始化 OpenAIEmbeddings 成功。")
101
+ except Exception as e:
102
+ raise ValueError(f"初始化 OpenAIEmbeddings 時出現錯誤: {e}")
103
+
104
+ try:
105
+ vectordb = Chroma.from_documents(
106
+ documents,
107
+ embedding=embeddings,
108
+ persist_directory=VECTORDB_DIR
109
+ )
110
+ logger.info("初始化 Chroma 向量資料庫成功。")
111
+ except Exception as e:
112
+ raise ValueError(f"初始化 Chroma 向量資料庫時出現錯誤: {e}")
113
+
114
+ return vectordb
115
+
116
+ # 定義聊天處理函數
117
+ def handle_query(user_message, chat_history, vectordb, api_key):
118
+ try:
119
+ if not user_message:
120
+ return chat_history
121
+
122
+ # 添加角色指令前綴
123
+ preface = """
124
+ 指令: 以繁體中文回答問題,200字以內。你是一位專業心理學家與調酒師,專精於 MBTI 人格與經典調酒主題。
125
+ 非相關問題,請回應:「目前僅支援 MBTI 分析與經典調酒主題。」。
126
+ """
127
+ query = f"{preface} 查詢內容:{user_message}"
128
+
129
+ # 初始化 ConversationalRetrievalChain,並傳遞 openai_api_key
130
+ pdf_qa = ConversationalRetrievalChain.from_llm(
131
+ ChatOpenAI(temperature=0.7, model="gpt-4", openai_api_key=api_key),
132
+ retriever=vectordb.as_retriever(search_kwargs={'k': 6}),
133
+ return_source_documents=True
134
+ )
135
+
136
+ # 呼叫模型並處理查詢
137
+ result = pdf_qa.invoke({"question": query, "chat_history": chat_history})
138
+
139
+ # 檢查結果並更新聊天歷史
140
+ if "answer" in result:
141
+ chat_history = chat_history + [(user_message, result["answer"])]
142
+ else:
143
+ chat_history = chat_history + [(user_message, "抱歉,未能獲得有效回應。")]
144
+ return chat_history
145
+
146
+ except Exception as e:
147
+ logger.error(f"Error in handle_query: {e}")
148
+ return chat_history + [("系統", f"出現錯誤: {str(e)}")]
149
+
150
+ # 定義保存 API 密鑰的函數
151
+ def save_api_key(api_key, state):
152
+ if not api_key.startswith("sk-"):
153
+ return "請輸入有效的 OpenAI API 密鑰。", state
154
+ state['api_key'] = api_key
155
+ logger.info("使用者已保存自己的 OpenAI API 密鑰。")
156
+ return "API 密鑰已成功保存。您現在可以上傳 PDF 文件並開始提問。", state
157
+
158
+ # 定義 Gradio 的處理函數
159
+ def process_files(files, state):
160
+ logger.info("process_files called")
161
+ if files:
162
+ try:
163
+ # 檢查是否已保存 API 密鑰
164
+ api_key = state.get('api_key', None)
165
+ if not api_key:
166
+ logger.error("使用者未提供 OpenAI API 密鑰。")
167
+ return "請先在「設定 API 密鑰」標籤中輸入並保存您的 OpenAI API 密鑰。", state
168
+
169
+ logger.info(f"Received {len(files)} files")
170
+ saved_file_paths = []
171
+ for idx, file_data in enumerate(files):
172
+ # 為每個文件分配唯一的文件名
173
+ filename = f"uploaded_{idx}.pdf"
174
+ save_path = os.path.join(VECTORDB_DIR, filename)
175
+ with open(save_path, "wb") as f:
176
+ f.write(file_data)
177
+ # 確認文件是否存在並檢查文件大小
178
+ if os.path.exists(save_path):
179
+ file_size = os.path.getsize(save_path)
180
+ if file_size > 0:
181
+ logger.info(f"File successfully saved to: {save_path} (Size: {file_size} bytes)")
182
+ else:
183
+ logger.error(f"File saved to {save_path} is empty.")
184
+ raise ValueError(f"上傳的文件 {filename} 為空。")
185
+ else:
186
+ logger.error(f"Failed to save file to: {save_path}")
187
+ raise FileNotFoundError(f"無法保存文件到 {save_path}")
188
+ saved_file_paths.append(save_path)
189
+ # 測試 PDF 加載器,先用 PyMuPDFLoader,再用 PyPDFLoader
190
+ try:
191
+ test_pdf_loader(save_path, loader_type='PyMuPDFLoader')
192
+ except Exception as e:
193
+ logger.error(f"PyMuPDFLoader failed: {e}")
194
+ logger.info("Attempting to load with PyPDFLoader...")
195
+ test_pdf_loader(save_path, loader_type='PyPDFLoader')
196
+ # 列出 VECTORDB_DIR 中的所有文件
197
+ saved_files = os.listdir(VECTORDB_DIR)
198
+ logger.info(f"Files in VECTORDB_DIR ({VECTORDB_DIR}): {saved_files}")
199
+ # 列出文件大小
200
+ file_sizes = {file: os.path.getsize(os.path.join(VECTORDB_DIR, file)) for file in saved_files}
201
+ logger.info(f"File sizes in VECTORDB_DIR: {file_sizes}")
202
+ vectordb = load_and_process_documents(saved_file_paths, loader_type='PyMuPDFLoader', api_key=api_key)
203
+ state['vectordb'] = vectordb
204
+ return "PDF 文件已成功上傳並處理。您現在可以開始提問。", state
205
+ except Exception as e:
206
+ logger.error(f"Error in process_files: {e}")
207
+ return f"處理文件時出現錯誤: {e}", state
208
+ else:
209
+ return "請上傳至少一個 PDF 文件。", state
210
+
211
+ def chat_interface(user_message, chat_history, state):
212
+ vectordb = state.get('vectordb', None)
213
+ api_key = state.get('api_key', None)
214
+ if not vectordb:
215
+ return chat_history, state, "請先上傳 PDF 文件以進行處理。"
216
+ if not api_key:
217
+ return chat_history, state, "請先在「設定 API 密鑰」標籤中輸入並保存您的 OpenAI API 密鑰。"
218
+
219
+ # 處理查詢
220
+ updated_history = handle_query(user_message, chat_history, vectordb, api_key)
221
+ return updated_history, state, ""
222
+
223
+ # 設計 Gradio 介面
224
+ with gr.Blocks() as demo:
225
+ gr.Markdown("<h1 style='text-align: center;'>MBTI 與經典調酒 AI 助理</h1>")
226
+
227
+ # 定義共享的 state
228
+ state = gr.State({"vectordb": None, "api_key": None})
229
+
230
+ with gr.Tab("設定 API 密���"):
231
+ with gr.Row():
232
+ with gr.Column(scale=1):
233
+ api_key_input = gr.Textbox(
234
+ label="輸入您的 OpenAI API 密鑰",
235
+ placeholder="sk-...",
236
+ type="password",
237
+ interactive=True
238
+ )
239
+ save_api_key_btn = gr.Button("保存 API 密鑰")
240
+ api_key_status = gr.Textbox(label="狀態", interactive=False)
241
+
242
+ with gr.Tab("上傳 PDF 文件"):
243
+ with gr.Row():
244
+ with gr.Column(scale=1):
245
+ upload = gr.File(
246
+ file_count="multiple",
247
+ file_types=[".pdf"],
248
+ label="上傳 PDF 文件",
249
+ interactive=True,
250
+ type="binary" # 修改為 'binary'
251
+ )
252
+ upload_btn = gr.Button("上傳並處理")
253
+ upload_status = gr.Textbox(label="上傳狀態", interactive=False)
254
+
255
+ with gr.Tab("聊天機器人"):
256
+ chatbot = gr.Chatbot()
257
+
258
+ with gr.Row():
259
+ with gr.Column(scale=0.85):
260
+ txt = gr.Textbox(show_label=False, placeholder="請輸入您的問題...")
261
+ with gr.Column(scale=0.15, min_width=0):
262
+ submit_btn = gr.Button("提問")
263
+
264
+ # 綁定提問按鈕
265
+ submit_btn.click(
266
+ chat_interface,
267
+ inputs=[txt, chatbot, state],
268
+ outputs=[chatbot, state, txt]
269
+ )
270
+
271
+ # 綁定輸入框的提交事件
272
+ txt.submit(
273
+ chat_interface,
274
+ inputs=[txt, chatbot, state],
275
+ outputs=[chatbot, state, txt]
276
+ )
277
+
278
+ # 綁定保存 API 密鑰按鈕
279
+ save_api_key_btn.click(
280
+ save_api_key,
281
+ inputs=[api_key_input, state],
282
+ outputs=[api_key_status, state]
283
+ )
284
+
285
+ # 綁定上傳按鈕
286
+ upload_btn.click(
287
+ process_files,
288
+ inputs=[upload, state],
289
+ outputs=[upload_status, state]
290
+ )
291
+
292
+ # 啟動 Gradio 應用
293
+ demo.launch()
requirements (4).txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ langchain
3
+ langchain-community
4
+ chromadb
5
+ pymupdf
6
+ python-dotenv
7
+ tiktoken
8
+ openai==0.27.8
9
+ PyPDF2