Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,7 @@ from langchain_community.vectorstores import FAISS
|
|
| 17 |
TXT_FOLDER = "./out_texts"
|
| 18 |
DB_PATH = "./faiss_db"
|
| 19 |
os.makedirs(DB_PATH, exist_ok=True)
|
|
|
|
| 20 |
|
| 21 |
HF_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 22 |
if not HF_TOKEN:
|
|
@@ -39,9 +40,7 @@ else:
|
|
| 39 |
docs = []
|
| 40 |
for filepath in txt_files:
|
| 41 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 42 |
-
docs.append(
|
| 43 |
-
Document(page_content=f.read(), metadata={"source": os.path.basename(filepath)})
|
| 44 |
-
)
|
| 45 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 46 |
split_docs = splitter.split_documents(docs)
|
| 47 |
db = FAISS.from_documents(split_docs, embeddings_model)
|
|
@@ -54,7 +53,7 @@ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
|
| 54 |
# -------------------------------
|
| 55 |
llm = HuggingFaceEndpoint(
|
| 56 |
repo_id="google/flan-t5-large",
|
| 57 |
-
task="text2text-generation",
|
| 58 |
huggingfacehub_api_token=HF_TOKEN,
|
| 59 |
model_kwargs={"temperature": 0.7, "max_new_tokens": 512},
|
| 60 |
)
|
|
@@ -74,10 +73,9 @@ def get_hf_rate_limit():
|
|
| 74 |
r = requests.get("https://huggingface.co/api/whoami", headers=headers)
|
| 75 |
r.raise_for_status()
|
| 76 |
data = r.json()
|
| 77 |
-
|
| 78 |
-
remaining = 300 - used if used is not None else "未知"
|
| 79 |
return f"本小時剩餘 API 次數:約 {remaining}"
|
| 80 |
-
except:
|
| 81 |
return "無法取得 API 速率資訊"
|
| 82 |
|
| 83 |
# -------------------------------
|
|
@@ -94,7 +92,7 @@ def generate_article_with_rate(query, segments=5):
|
|
| 94 |
for i in range(int(segments)):
|
| 95 |
try:
|
| 96 |
result = qa_chain({"query": prompt})
|
| 97 |
-
paragraph = result
|
| 98 |
if not paragraph:
|
| 99 |
paragraph = "(本段生成失敗,請嘗試減少段落或改用較小模型。)"
|
| 100 |
except Exception as e:
|
|
@@ -105,26 +103,23 @@ def generate_article_with_rate(query, segments=5):
|
|
| 105 |
|
| 106 |
doc.save(docx_file)
|
| 107 |
full_text = "\n\n".join(all_text)
|
| 108 |
-
|
| 109 |
rate_info = get_hf_rate_limit()
|
| 110 |
return f"{rate_info}\n\n{full_text}", docx_file
|
| 111 |
|
| 112 |
# -------------------------------
|
| 113 |
# 7. Gradio 介面
|
| 114 |
# -------------------------------
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
],
|
| 125 |
-
|
| 126 |
-
description="使用 Hugging Face Endpoint LLM + FAISS RAG,生成文章並提示 API 剩餘額度。"
|
| 127 |
-
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
-
|
|
|
|
| 17 |
TXT_FOLDER = "./out_texts"
|
| 18 |
DB_PATH = "./faiss_db"
|
| 19 |
os.makedirs(DB_PATH, exist_ok=True)
|
| 20 |
+
os.makedirs(TXT_FOLDER, exist_ok=True) # 避免沒有 txt 檔時錯誤
|
| 21 |
|
| 22 |
HF_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 23 |
if not HF_TOKEN:
|
|
|
|
| 40 |
docs = []
|
| 41 |
for filepath in txt_files:
|
| 42 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 43 |
+
docs.append(Document(page_content=f.read(), metadata={"source": os.path.basename(filepath)}))
|
|
|
|
|
|
|
| 44 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 45 |
split_docs = splitter.split_documents(docs)
|
| 46 |
db = FAISS.from_documents(split_docs, embeddings_model)
|
|
|
|
| 53 |
# -------------------------------
|
| 54 |
llm = HuggingFaceEndpoint(
|
| 55 |
repo_id="google/flan-t5-large",
|
| 56 |
+
task="text2text-generation",
|
| 57 |
huggingfacehub_api_token=HF_TOKEN,
|
| 58 |
model_kwargs={"temperature": 0.7, "max_new_tokens": 512},
|
| 59 |
)
|
|
|
|
| 73 |
r = requests.get("https://huggingface.co/api/whoami", headers=headers)
|
| 74 |
r.raise_for_status()
|
| 75 |
data = r.json()
|
| 76 |
+
remaining = data.get("rate_limit", {}).get("remaining", "未知")
|
|
|
|
| 77 |
return f"本小時剩餘 API 次數:約 {remaining}"
|
| 78 |
+
except Exception:
|
| 79 |
return "無法取得 API 速率資訊"
|
| 80 |
|
| 81 |
# -------------------------------
|
|
|
|
| 92 |
for i in range(int(segments)):
|
| 93 |
try:
|
| 94 |
result = qa_chain({"query": prompt})
|
| 95 |
+
paragraph = result.get("result", "").strip()
|
| 96 |
if not paragraph:
|
| 97 |
paragraph = "(本段生成失敗,請嘗試減少段落或改用較小模型。)"
|
| 98 |
except Exception as e:
|
|
|
|
| 103 |
|
| 104 |
doc.save(docx_file)
|
| 105 |
full_text = "\n\n".join(all_text)
|
|
|
|
| 106 |
rate_info = get_hf_rate_limit()
|
| 107 |
return f"{rate_info}\n\n{full_text}", docx_file
|
| 108 |
|
| 109 |
# -------------------------------
|
| 110 |
# 7. Gradio 介面
|
| 111 |
# -------------------------------
|
| 112 |
+
with gr.Blocks() as demo:
|
| 113 |
+
gr.Markdown("# 佛教經論 RAG 系統 (HF API)")
|
| 114 |
+
gr.Markdown("使用 Hugging Face Endpoint LLM + FAISS RAG,生成文章並提示 API 剩餘額度。")
|
| 115 |
+
|
| 116 |
+
query_input = gr.Textbox(lines=2, placeholder="請輸入文章主題", label="文章主題")
|
| 117 |
+
segments_input = gr.Slider(minimum=1, maximum=10, step=1, value=5, label="段落數")
|
| 118 |
+
output_text = gr.Textbox(label="生成文章 + API 剩餘次數")
|
| 119 |
+
output_file = gr.File(label="下載 DOCX")
|
| 120 |
+
|
| 121 |
+
query_input.submit(generate_article_with_rate, [query_input, segments_input], [output_text, output_file])
|
| 122 |
+
segments_input.change(generate_article_with_rate, [query_input, segments_input], [output_text, output_file])
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
+
demo.launch()
|