tsengiii commited on
Commit
dfb2496
·
verified ·
1 Parent(s): 23bffea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -47
app.py CHANGED
@@ -8,7 +8,6 @@ from langchain.vectorstores import Chroma
8
  from langchain_community.embeddings import OpenAIEmbeddings
9
  from langchain_community.chat_models import ChatOpenAI
10
  import shutil # 用於文件複製
11
- import logging
12
 
13
  # 獲取 OpenAI API 密鑰(初始不使用固定密鑰)
14
  api_key_env = os.getenv("OPENAI_API_KEY")
@@ -116,7 +115,7 @@ def process_files(files, state):
116
  try:
117
  api_key = state.get('api_key', None)
118
  if not api_key:
119
- return "請先在「設定 API 密鑰」標籤中輸入並保存您的 OpenAI API 密鑰。", state
120
 
121
  saved_file_paths = []
122
  for idx, file_data in enumerate(files):
@@ -139,7 +138,7 @@ def chat_interface(user_message, chat_history, state):
139
  if not vectordb:
140
  return chat_history, state, "請先上傳 PDF 文件以進行處理。"
141
  if not api_key:
142
- return chat_history, state, "請先在「設定 API 密鑰」標籤中輸入並保存您的 OpenAI API 密鑰。"
143
 
144
  updated_history = handle_query(user_message, chat_history, vectordb, api_key)
145
  return updated_history, state, ""
@@ -150,52 +149,36 @@ with gr.Blocks() as demo:
150
 
151
  state = gr.State({"vectordb": None, "api_key": None})
152
 
153
- with gr.Tab("設定 API 密鑰"):
154
- with gr.Row():
155
- with gr.Column(scale=1):
156
- api_key_input = gr.Textbox(
157
- label="輸入您的 OpenAI API 密鑰",
158
- placeholder="sk-...",
159
- type="password",
160
- interactive=True
161
- )
162
- save_api_key_btn = gr.Button("保存 API 密鑰")
163
- api_key_status = gr.Textbox(label="狀態", interactive=False)
164
-
165
- with gr.Tab("上傳勞動法 PDF 文件"):
166
- with gr.Row():
167
- with gr.Column(scale=1):
168
- upload = gr.File(
169
- file_count="multiple",
170
- file_types=[".pdf"],
171
- label="上傳勞動法 PDF 文件",
172
- interactive=True,
173
- type="binary"
174
- )
175
- upload_btn = gr.Button("上傳並處理")
176
- upload_status = gr.Textbox(label="上傳狀態", interactive=False)
177
-
178
- with gr.Tab("智能諮詢"):
179
- chatbot = gr.Chatbot()
180
-
181
- with gr.Row():
182
- with gr.Column(scale=0.85):
183
- txt = gr.Textbox(show_label=False, placeholder="請輸入您的法律問題...")
184
- with gr.Column(scale=0.15, min_width=0):
185
- submit_btn = gr.Button("提問")
186
-
187
- submit_btn.click(
188
- chat_interface,
189
- inputs=[txt, chatbot, state],
190
- outputs=[chatbot, state, txt]
191
- )
192
 
193
- txt.submit(
194
- chat_interface,
195
- inputs=[txt, chatbot, state],
196
- outputs=[chatbot, state, txt]
197
- )
 
198
 
 
199
  save_api_key_btn.click(
200
  save_api_key,
201
  inputs=[api_key_input, state],
@@ -208,5 +191,17 @@ with gr.Blocks() as demo:
208
  outputs=[upload_status, state]
209
  )
210
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  # 啟動 Gradio 應用
212
  demo.launch()
 
8
  from langchain_community.embeddings import OpenAIEmbeddings
9
  from langchain_community.chat_models import ChatOpenAI
10
  import shutil # 用於文件複製
 
11
 
12
  # 獲取 OpenAI API 密鑰(初始不使用固定密鑰)
13
  api_key_env = os.getenv("OPENAI_API_KEY")
 
115
  try:
116
  api_key = state.get('api_key', None)
117
  if not api_key:
118
+ return "請先輸入並保存您的 OpenAI API 密鑰。", state
119
 
120
  saved_file_paths = []
121
  for idx, file_data in enumerate(files):
 
138
  if not vectordb:
139
  return chat_history, state, "請先上傳 PDF 文件以進行處理。"
140
  if not api_key:
141
+ return chat_history, state, "請先輸入並保存您的 OpenAI API 密鑰。"
142
 
143
  updated_history = handle_query(user_message, chat_history, vectordb, api_key)
144
  return updated_history, state, ""
 
149
 
150
  state = gr.State({"vectordb": None, "api_key": None})
151
 
152
+ # API 密鑰輸入框
153
+ api_key_input = gr.Textbox(
154
+ label="輸入您的 OpenAI API 密鑰",
155
+ placeholder="sk-...",
156
+ type="password",
157
+ interactive=True
158
+ )
159
+ save_api_key_btn = gr.Button("保存 API 密鑰")
160
+ api_key_status = gr.Textbox(label="狀態", interactive=False)
161
+
162
+ # 上傳 PDF 文件
163
+ gr.Markdown("請上傳勞動法相關文檔,讓我協助解決您的職場問題!🤖")
164
+ upload = gr.File(
165
+ file_count="multiple",
166
+ file_types=[".pdf"],
167
+ label="上傳勞動法 PDF 文件",
168
+ interactive=True,
169
+ type="binary"
170
+ )
171
+ upload_btn = gr.Button("上傳並處理")
172
+ upload_status = gr.Textbox(label="上傳狀態", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
+ # 智能諮詢
175
+ gr.Markdown("### 勞動法小幫手")
176
+ chatbot = gr.Chatbot()
177
+
178
+ txt = gr.Textbox(show_label=False, placeholder="請輸入您的法律問題...")
179
+ submit_btn = gr.Button("提問")
180
 
181
+ # 綁定事件
182
  save_api_key_btn.click(
183
  save_api_key,
184
  inputs=[api_key_input, state],
 
191
  outputs=[upload_status, state]
192
  )
193
 
194
+ submit_btn.click(
195
+ chat_interface,
196
+ inputs=[txt, chatbot, state],
197
+ outputs=[chatbot, state, txt]
198
+ )
199
+
200
+ txt.submit(
201
+ chat_interface,
202
+ inputs=[txt, chatbot, state],
203
+ outputs=[chatbot, state, txt]
204
+ )
205
+
206
  # 啟動 Gradio 應用
207
  demo.launch()