youngtsai commited on
Commit
e485414
·
1 Parent(s): 6532063
Files changed (2) hide show
  1. app.py +24 -9
  2. initializer.py +23 -1
app.py CHANGED
@@ -1,6 +1,7 @@
1
  # -*- coding: utf-8 -*-
2
 
3
  from typing import Container
 
4
  import gradio as gr
5
  import os
6
  import shutil
@@ -8,12 +9,14 @@ import tempfile
8
  from google import genai
9
  from google.genai import types
10
 
11
- from initializer import initialize_clients
12
 
13
  # 初始化 Google Cloud Storage 服務和 GENAI 客戶端
14
  GCS_SERVICE, GENAI_CLIENT = initialize_clients()
15
  GCS_CLIENT = GCS_SERVICE.client
16
 
 
 
17
  def toggle_visibility(toggle_value):
18
  return gr.update(visible=toggle_value)
19
 
@@ -175,6 +178,14 @@ def process_all_files(file_list):
175
  print("\n=== 檔案處理完成 ===")
176
  return result_text, transcript_text
177
 
 
 
 
 
 
 
 
 
178
  def on_summary_click(transcript):
179
  if not transcript:
180
  return "請先上傳文件或輸入 YouTube 連結並處理完成後再生成摘要。"
@@ -183,7 +194,10 @@ def on_summary_click(transcript):
183
  return summary
184
 
185
  with gr.Blocks() as demo:
186
- gr.Markdown("# AI Notes Assistant")
 
 
 
187
 
188
  with gr.Row():
189
  source_toggle = gr.Checkbox(label="顯示來源選單", value=True)
@@ -209,8 +223,8 @@ with gr.Blocks() as demo:
209
  file_display = gr.CheckboxGroup(label="已上傳的文件", interactive=True)
210
 
211
  process_files_button = gr.Button("處理檔案")
212
- rag_result = gr.Textbox(label="處理結果", interactive=False)
213
-
214
  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)
215
 
216
  with gr.Column(visible=True) as chat_column:
@@ -244,13 +258,14 @@ with gr.Blocks() as demo:
244
  chat_toggle.change(toggle_visibility, inputs=chat_toggle, outputs=chat_column)
245
  feature_toggle.change(toggle_visibility, inputs=feature_toggle, outputs=feature_column)
246
 
247
-
248
-
249
  # 更新處理檔案按鈕的事件處理
250
  process_files_button.click(
251
- process_all_files,
252
- inputs=[file_list],
253
- outputs=[rag_result, transcript_display]
 
 
 
254
  ).then(
255
  fn=on_summary_click,
256
  inputs=[transcript_display],
 
1
  # -*- coding: utf-8 -*-
2
 
3
  from typing import Container
4
+ from config.config import PASSWORD
5
  import gradio as gr
6
  import os
7
  import shutil
 
9
  from google import genai
10
  from google.genai import types
11
 
12
+ from initializer import initialize_clients, initialize_password
13
 
14
  # 初始化 Google Cloud Storage 服務和 GENAI 客戶端
15
  GCS_SERVICE, GENAI_CLIENT = initialize_clients()
16
  GCS_CLIENT = GCS_SERVICE.client
17
 
18
+ PASSWORD = initialize_password()
19
+
20
  def toggle_visibility(toggle_value):
21
  return gr.update(visible=toggle_value)
22
 
 
178
  print("\n=== 檔案處理完成 ===")
179
  return result_text, transcript_text
180
 
181
+ def process_with_auth(password, file_list):
182
+ """包含密碼驗證的處理函數"""
183
+ if not password or password != PASSWORD:
184
+ return "請輸入正確的密碼", "", gr.update(visible=False)
185
+
186
+ result_text, transcript_text = process_all_files(file_list)
187
+ return result_text, transcript_text
188
+
189
  def on_summary_click(transcript):
190
  if not transcript:
191
  return "請先上傳文件或輸入 YouTube 連結並處理完成後再生成摘要。"
 
194
  return summary
195
 
196
  with gr.Blocks() as demo:
197
+
198
+ with gr.Row():
199
+ gr.Markdown("# AI Notes Assistant")
200
+ password_input = gr.Textbox(label="password")
201
 
202
  with gr.Row():
203
  source_toggle = gr.Checkbox(label="顯示來源選單", value=True)
 
223
  file_display = gr.CheckboxGroup(label="已上傳的文件", interactive=True)
224
 
225
  process_files_button = gr.Button("處理檔案")
226
+ rag_result = gr.Textbox(label="處理狀態", interactive=False)
227
+
228
  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)
229
 
230
  with gr.Column(visible=True) as chat_column:
 
258
  chat_toggle.change(toggle_visibility, inputs=chat_toggle, outputs=chat_column)
259
  feature_toggle.change(toggle_visibility, inputs=feature_toggle, outputs=feature_column)
260
 
 
 
261
  # 更新處理檔案按鈕的事件處理
262
  process_files_button.click(
263
+ fn=process_with_auth,
264
+ inputs=[password_input, file_list],
265
+ outputs=[
266
+ rag_result,
267
+ transcript_display
268
+ ]
269
  ).then(
270
  fn=on_summary_click,
271
  inputs=[transcript_display],
initializer.py CHANGED
@@ -67,9 +67,31 @@ def initialize_genai_client(google_credentials_key):
67
  location='us-central1'
68
  )
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def initialize_clients():
71
  google_credentials_key = initialize_google_credentials()
72
  gcs_service = initialize_gcs_service(google_credentials_key)
73
  genai_client = initialize_genai_client(google_credentials_key)
74
 
75
- return gcs_service, genai_client
 
 
67
  location='us-central1'
68
  )
69
 
70
+ def initialize_password():
71
+ is_env_local = os.getenv("IS_ENV_LOCAL", "false") == "true"
72
+ print(f"Environment is local: {is_env_local}")
73
+
74
+ try:
75
+ if is_env_local:
76
+ config_path = os.path.join(os.path.dirname(__file__), "local_config.json")
77
+ print(f"Trying to load config from: {config_path}")
78
+ if not os.path.exists(config_path):
79
+ print(f"Warning: {config_path} does not exist")
80
+ return None
81
+
82
+ with open(config_path) as f:
83
+ config = json.load(f)
84
+ return config["PASSWORD"]
85
+ else:
86
+ return os.getenv("PASSWORD")
87
+ except Exception as e:
88
+ print(f"Error initializing password: {str(e)}")
89
+ return None
90
+
91
  def initialize_clients():
92
  google_credentials_key = initialize_google_credentials()
93
  gcs_service = initialize_gcs_service(google_credentials_key)
94
  genai_client = initialize_genai_client(google_credentials_key)
95
 
96
+ return gcs_service, genai_client
97
+