CHUNYU0505 commited on
Commit
f90da5a
·
verified ·
1 Parent(s): b867ef7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -37,6 +37,8 @@ if os.path.exists(os.path.join(DB_PATH, "index.faiss")):
37
  else:
38
  print("沒有資料庫,開始建立新向量資料庫...")
39
  txt_files = glob.glob(f"{TXT_FOLDER}/*.txt")
 
 
40
  docs = []
41
  for filepath in txt_files:
42
  with open(filepath, "r", encoding="utf-8") as f:
@@ -59,7 +61,6 @@ llm = HuggingFaceEndpoint(
59
  max_new_tokens=512,
60
  )
61
 
62
-
63
  qa_chain = RetrievalQA.from_chain_type(
64
  llm=llm,
65
  retriever=retriever,
@@ -114,14 +115,17 @@ def generate_article_with_rate(query, segments=5):
114
  with gr.Blocks() as demo:
115
  gr.Markdown("# 佛教經論 RAG 系統 (HF API)")
116
  gr.Markdown("使用 Hugging Face Endpoint LLM + FAISS RAG,生成文章並提示 API 剩餘額度。")
117
-
118
  query_input = gr.Textbox(lines=2, placeholder="請輸入文章主題", label="文章主題")
119
  segments_input = gr.Slider(minimum=1, maximum=10, step=1, value=5, label="段落數")
120
  output_text = gr.Textbox(label="生成文章 + API 剩餘次數")
121
  output_file = gr.File(label="下載 DOCX")
122
-
123
- query_input.submit(generate_article_with_rate, [query_input, segments_input], [output_text, output_file])
124
- segments_input.change(generate_article_with_rate, [query_input, segments_input], [output_text, output_file])
125
 
 
 
 
 
 
 
126
  if __name__ == "__main__":
127
- demo.launch()
 
37
  else:
38
  print("沒有資料庫,開始建立新向量資料庫...")
39
  txt_files = glob.glob(f"{TXT_FOLDER}/*.txt")
40
+ if not txt_files:
41
+ print("注意:TXT 資料夾中沒有任何文字檔,向量資料庫將為空。")
42
  docs = []
43
  for filepath in txt_files:
44
  with open(filepath, "r", encoding="utf-8") as f:
 
61
  max_new_tokens=512,
62
  )
63
 
 
64
  qa_chain = RetrievalQA.from_chain_type(
65
  llm=llm,
66
  retriever=retriever,
 
115
  with gr.Blocks() as demo:
116
  gr.Markdown("# 佛教經論 RAG 系統 (HF API)")
117
  gr.Markdown("使用 Hugging Face Endpoint LLM + FAISS RAG,生成文章並提示 API 剩餘額度。")
118
+
119
  query_input = gr.Textbox(lines=2, placeholder="請輸入文章主題", label="文章主題")
120
  segments_input = gr.Slider(minimum=1, maximum=10, step=1, value=5, label="段落數")
121
  output_text = gr.Textbox(label="生成文章 + API 剩餘次數")
122
  output_file = gr.File(label="下載 DOCX")
 
 
 
123
 
124
+ btn = gr.Button("生成文章")
125
+ btn.click(generate_article_with_rate, [query_input, segments_input], [output_text, output_file])
126
+
127
+ # -------------------------------
128
+ # 8. 啟動 Gradio(Hugging Face Space 適用)
129
+ # -------------------------------
130
  if __name__ == "__main__":
131
+ demo.launch(share=True) # share=True 自動生成公開 URL