Marathon23 commited on
Commit
28b6c57
·
verified ·
1 Parent(s): 3551e50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -14,7 +14,7 @@ import shutil # 用於文件複製
14
  # 獲取 OpenAI API 密鑰
15
  api_key = os.getenv("OPENAI_API_KEY")
16
  if not api_key:
17
- raise ValueError("未能獲取 OpenAI_API_KEY。請在 Hugging Face Spaces 的 Secrets 中設置它。")
18
  openai.api_key = api_key
19
  print("OpenAI API 密鑰已設置。")
20
 
@@ -145,11 +145,11 @@ def process_files(files, state):
145
  try:
146
  print(f"Received {len(files)} files")
147
  saved_file_paths = []
148
- for file_path in files:
149
- print(f"Processing file: {file_path}")
150
- save_path = os.path.join(VECTORDB_DIR, os.path.basename(file_path))
151
- # 複製文件到 VECTORDB_DIR
152
- shutil.copy(file_path, save_path)
153
  # 確認文件是否存在
154
  if os.path.exists(save_path):
155
  print(f"File successfully saved to: {save_path}")
@@ -194,7 +194,7 @@ with gr.Blocks() as demo:
194
  file_types=[".pdf"],
195
  label="上傳 PDF 文件",
196
  interactive=True,
197
- type="filepath" # 保持為 "filepath"
198
  )
199
  upload_btn = gr.Button("上傳並處理")
200
  upload_status = gr.Textbox(label="上傳狀態", interactive=False)
 
14
  # 獲取 OpenAI API 密鑰
15
  api_key = os.getenv("OPENAI_API_KEY")
16
  if not api_key:
17
+ raise ValueError("未能獲取 OPENAI_API_KEY。請在 Hugging Face Spaces 的 Secrets 中設置它。")
18
  openai.api_key = api_key
19
  print("OpenAI API 密鑰已設置。")
20
 
 
145
  try:
146
  print(f"Received {len(files)} files")
147
  saved_file_paths = []
148
+ for file in files:
149
+ print(f"Processing file: {file.name}")
150
+ save_path = os.path.join(VECTORDB_DIR, file.name)
151
+ with open(save_path, "wb") as f:
152
+ f.write(file.read())
153
  # 確認文件是否存在
154
  if os.path.exists(save_path):
155
  print(f"File successfully saved to: {save_path}")
 
194
  file_types=[".pdf"],
195
  label="上傳 PDF 文件",
196
  interactive=True,
197
+ type="file" # 修改為 'file'
198
  )
199
  upload_btn = gr.Button("上傳並處理")
200
  upload_status = gr.Textbox(label="上傳狀態", interactive=False)