lianghsun commited on
Commit
760a2df
·
1 Parent(s): 1703267

first commit

Browse files
Files changed (1) hide show
  1. app.py +54 -49
app.py CHANGED
@@ -125,37 +125,38 @@ with tab_jsonl:
125
  st.json(obj)
126
 
127
  # 上傳按鈕:會在送出前幫每一筆加上 metadata
128
- if st.button("上傳", disabled=not (jsonl_file and jsonl_valid or BACKEND_URL is None)):
129
  if BACKEND_URL is None:
130
  st.warning("尚未設定 BACKEND_URL,無法實際送出,請在 `st.secrets` 中配置。")
131
  else:
132
- # 準備 metadata(會附加在每一行 JSON 物件上)
133
- meta = {
134
- "uploaded_at": uploaded_at, # UTC+8 ISO 字串
135
- "contributor_email": contributor_email if contributor_email.strip() else None,
136
- "share_permission": bool(share_permission),
137
- }
138
-
139
- # 重新組一份帶 metadata 的 jsonl 內容
140
- enriched_lines = []
141
- for obj in parsed_lines:
142
- obj_with_meta = {
143
- **obj,
144
- "metadata": meta,
145
  }
146
- enriched_lines.append(json.dumps(obj_with_meta, ensure_ascii=False))
147
 
148
- payload = "\n".join(enriched_lines).encode("utf-8")
 
 
 
 
 
 
 
149
 
150
- files = {"file": ("contrib.jsonl", payload, "application/jsonl")}
151
- try:
152
- resp = requests.post(f"{BACKEND_URL}/upload-jsonl", files=files)
153
- if resp.ok:
154
- st.success("已成功送交後端伺服器,等待後端進一步檢查與處理。")
155
- else:
156
- st.error(f"後端回傳錯誤:{resp.status_code} {resp.text}")
157
- except Exception as e:
158
- st.error(f"送出時發生錯誤:{e}")
 
 
159
 
160
 
161
  # ---------- Tab 2: PDF ----------
@@ -209,32 +210,36 @@ with tab_pdf:
209
 
210
  any_valid_pdf = any(tlen >= 100 for _, tlen in pdf_results) if pdf_results else False
211
 
212
- if st.button("上傳文本", disabled=not (pdf_files and any_valid_pdf or BACKEND_URL is None)):
213
  if BACKEND_URL is None:
214
  st.warning("尚未設定 BACKEND_URL,無法實際送出,請在 `st.secrets` 中配置。")
215
  else:
216
- files = []
217
- for pdf, text_len in pdf_results:
218
- if text_len < 100:
219
- continue # 跳過疑似掃描檔
220
- pdf.seek(0)
221
- files.append(("files", (pdf.name, pdf.getvalue(), "application/pdf")))
222
-
223
- if not files:
224
- st.warning("沒有通過文字檢查的 PDF 檔案可送出。")
225
  else:
226
- # PDF 部分的 metadata 用 form data 一起送出,讓後端可以記錄
227
- meta = {
228
- "uploaded_at": uploaded_at,
229
- "contributor_email": contributor_email if contributor_email.strip() else "",
230
- "share_permission": json.dumps(bool(share_permission)),
231
- }
232
-
233
- try:
234
- resp = requests.post(f"{BACKEND_URL}/upload-pdf", files=files, data=meta)
235
- if resp.ok:
236
- st.success("已成功送交後端伺服器,等待後端進一步檢查與處理。")
237
  else:
238
- st.error(f"後端回傳錯誤:{resp.status_code} {resp.text}")
239
- except Exception as e:
240
- st.error(f"送出時發生錯誤:{e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  st.json(obj)
126
 
127
  # 上傳按鈕:會在送出前幫每一筆加上 metadata
128
+ if st.button("上傳對話資料", disabled=not (jsonl_file and jsonl_valid or BACKEND_URL is None)):
129
  if BACKEND_URL is None:
130
  st.warning("尚未設定 BACKEND_URL,無法實際送出,請在 `st.secrets` 中配置。")
131
  else:
132
+ with st.spinner("正在上傳對話資料並檢查,請稍候…"):
133
+ # 準備 metadata(會附加在每一行 JSON 物件上)
134
+ meta = {
135
+ "uploaded_at": uploaded_at, # UTC+8 ISO 字串
136
+ "contributor_email": contributor_email if contributor_email.strip() else None,
137
+ "share_permission": bool(share_permission),
 
 
 
 
 
 
 
138
  }
 
139
 
140
+ # 重新組一份帶 metadata 的 jsonl 內容
141
+ enriched_lines = []
142
+ for obj in parsed_lines:
143
+ obj_with_meta = {
144
+ **obj,
145
+ "metadata": meta,
146
+ }
147
+ enriched_lines.append(json.dumps(obj_with_meta, ensure_ascii=False))
148
 
149
+ payload = "\n".join(enriched_lines).encode("utf-8")
150
+
151
+ files = {"file": ("contrib.jsonl", payload, "application/jsonl")}
152
+ try:
153
+ resp = requests.post(f"{BACKEND_URL}/upload-jsonl", files=files)
154
+ if resp.ok:
155
+ st.success("已成功送交後端伺服器,等待後端進一步檢查與處理。")
156
+ else:
157
+ st.error(f"後端回傳錯誤:{resp.status_code} {resp.text}")
158
+ except Exception as e:
159
+ st.error(f"送出時發生錯誤:{e}")
160
 
161
 
162
  # ---------- Tab 2: PDF ----------
 
210
 
211
  any_valid_pdf = any(tlen >= 100 for _, tlen in pdf_results) if pdf_results else False
212
 
213
+ if st.button("上傳 PDF 檔案", disabled=not (pdf_files and any_valid_pdf or BACKEND_URL is None)):
214
  if BACKEND_URL is None:
215
  st.warning("尚未設定 BACKEND_URL,無法實際送出,請在 `st.secrets` 中配置。")
216
  else:
217
+ if not pdf_results:
218
+ st.warning("沒有可上傳的 PDF 檔案。")
 
 
 
 
 
 
 
219
  else:
220
+ with st.spinner("正在上傳 PDF 並進行檢查,請稍候…"):
221
+ files = []
222
+ for pdf, text_len in pdf_results:
223
+ if text_len < 100:
224
+ continue # 跳過疑似掃描檔
225
+ pdf.seek(0)
226
+ files.append(("files", (pdf.name, pdf.getvalue(), "application/pdf")))
227
+
228
+ if not files:
229
+ st.warning("沒有通過文字檢查的 PDF 檔案可送出。")
 
230
  else:
231
+ # PDF 部分的 metadata 用 form data 一起送出,讓後端可以記錄
232
+ meta = {
233
+ "uploaded_at": uploaded_at,
234
+ "contributor_email": contributor_email if contributor_email.strip() else "",
235
+ "share_permission": json.dumps(bool(share_permission)),
236
+ }
237
+
238
+ try:
239
+ resp = requests.post(f"{BACKEND_URL}/upload-pdf", files=files, data=meta)
240
+ if resp.ok:
241
+ st.success("已成功送交後端伺服器,等待後端進一步檢查與處理。")
242
+ else:
243
+ st.error(f"後端回傳錯誤:{resp.status_code} {resp.text}")
244
+ except Exception as e:
245
+ st.error(f"送出時發生錯誤:{e}")