Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,128 +0,0 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
# -------------------------------
|
| 3 |
-
# 1. 套件載入
|
| 4 |
-
# -------------------------------
|
| 5 |
-
import os, glob, requests
|
| 6 |
-
from langchain.docstore.document import Document
|
| 7 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
-
from langchain.chains import RetrievalQA
|
| 9 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 10 |
-
from docx import Document as DocxDocument
|
| 11 |
-
import gradio as gr
|
| 12 |
-
from langchain_community.vectorstores import FAISS
|
| 13 |
-
from langchain_community.llms import HuggingFaceHub
|
| 14 |
-
|
| 15 |
-
# -------------------------------
|
| 16 |
-
# 2. 環境變數與資料路徑
|
| 17 |
-
# -------------------------------
|
| 18 |
-
TXT_FOLDER = "./out_texts"
|
| 19 |
-
DB_PATH = "./faiss_db"
|
| 20 |
-
os.makedirs(DB_PATH, exist_ok=True)
|
| 21 |
-
|
| 22 |
-
HF_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 23 |
-
if not HF_TOKEN:
|
| 24 |
-
raise ValueError("請在 Hugging Face Space 的 Settings → Repository secrets 設定 HUGGINGFACEHUB_API_TOKEN")
|
| 25 |
-
|
| 26 |
-
# -------------------------------
|
| 27 |
-
# 3. 建立或載入向量資料庫
|
| 28 |
-
# -------------------------------
|
| 29 |
-
EMBEDDINGS_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
|
| 30 |
-
embeddings_model = HuggingFaceEmbeddings(model_name=EMBEDDINGS_MODEL_NAME)
|
| 31 |
-
|
| 32 |
-
if os.path.exists(os.path.join(DB_PATH, "index.faiss")):
|
| 33 |
-
print("載入現有向量資料庫...")
|
| 34 |
-
db = FAISS.load_local(DB_PATH, embeddings_model, allow_dangerous_deserialization=True)
|
| 35 |
-
else:
|
| 36 |
-
print("沒有資料庫,開始建立新向量資料庫...")
|
| 37 |
-
txt_files = glob.glob(f"{TXT_FOLDER}/*.txt")
|
| 38 |
-
docs = []
|
| 39 |
-
for filepath in txt_files:
|
| 40 |
-
with open(filepath, "r", encoding="utf-8") as f:
|
| 41 |
-
docs.append(Document(page_content=f.read(), metadata={"source": os.path.basename(filepath)}))
|
| 42 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 43 |
-
split_docs = splitter.split_documents(docs)
|
| 44 |
-
db = FAISS.from_documents(split_docs, embeddings_model)
|
| 45 |
-
db.save_local(DB_PATH)
|
| 46 |
-
|
| 47 |
-
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
| 48 |
-
|
| 49 |
-
# -------------------------------
|
| 50 |
-
# 4. LLM 設定(Hugging Face Hub)
|
| 51 |
-
# -------------------------------
|
| 52 |
-
llm = HuggingFaceHub(
|
| 53 |
-
repo_id="google/flan-t5-large",
|
| 54 |
-
model_kwargs={"temperature": 0.7, "max_new_tokens": 512},
|
| 55 |
-
huggingfacehub_api_token=HF_TOKEN
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 59 |
-
llm=llm,
|
| 60 |
-
retriever=retriever,
|
| 61 |
-
return_source_documents=True
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
# -------------------------------
|
| 65 |
-
# 5. 查詢 API 剩餘額度
|
| 66 |
-
# -------------------------------
|
| 67 |
-
def get_hf_rate_limit():
|
| 68 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 69 |
-
try:
|
| 70 |
-
r = requests.get("https://huggingface.co/api/whoami", headers=headers)
|
| 71 |
-
r.raise_for_status()
|
| 72 |
-
data = r.json()
|
| 73 |
-
used = data.get("rate_limit", {}).get("used", 0)
|
| 74 |
-
remaining = 300 - used if used is not None else "未知"
|
| 75 |
-
return f"本小時剩餘 API 次數:約 {remaining}"
|
| 76 |
-
except:
|
| 77 |
-
return "無法取得 API 速率資訊"
|
| 78 |
-
|
| 79 |
-
# -------------------------------
|
| 80 |
-
# 6. 生成文章
|
| 81 |
-
# -------------------------------
|
| 82 |
-
def generate_article_with_rate(query, segments=5):
|
| 83 |
-
docx_file = "/tmp/generated_article.docx"
|
| 84 |
-
doc = DocxDocument()
|
| 85 |
-
doc.add_heading(query, level=1)
|
| 86 |
-
|
| 87 |
-
all_text = []
|
| 88 |
-
prompt = f"請依據下列主題生成段落:{query}\n\n每段約150-200字。"
|
| 89 |
-
|
| 90 |
-
for i in range(int(segments)):
|
| 91 |
-
try:
|
| 92 |
-
result = qa_chain({"query": prompt})
|
| 93 |
-
paragraph = result["result"].strip()
|
| 94 |
-
if not paragraph:
|
| 95 |
-
paragraph = "(本段生成失敗,請嘗試減少段落或改用較小模型。)"
|
| 96 |
-
except Exception as e:
|
| 97 |
-
paragraph = f"(本段生成失敗:{e})"
|
| 98 |
-
all_text.append(paragraph)
|
| 99 |
-
doc.add_paragraph(paragraph)
|
| 100 |
-
prompt = f"請接續上一段生成下一段:\n{paragraph}\n\n下一段:"
|
| 101 |
-
|
| 102 |
-
doc.save(docx_file)
|
| 103 |
-
full_text = "\n\n".join(all_text)
|
| 104 |
-
|
| 105 |
-
# 取得 API 剩餘次數
|
| 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 |
-
iface = gr.Interface(
|
| 113 |
-
fn=generate_article_with_rate,
|
| 114 |
-
inputs=[
|
| 115 |
-
gr.Textbox(lines=2, placeholder="請輸入文章主題", label="文章主題"),
|
| 116 |
-
gr.Slider(minimum=1, maximum=10, step=1, value=5, label="段落數")
|
| 117 |
-
],
|
| 118 |
-
outputs=[
|
| 119 |
-
gr.Textbox(label="生成文章 + API 剩餘次數"),
|
| 120 |
-
gr.File(label="下載 DOCX")
|
| 121 |
-
],
|
| 122 |
-
title="佛教經論 RAG 系統 (HF API)",
|
| 123 |
-
description="使用 Hugging Face Hub LLM + FAISS RAG,生成文章並提示 API 剩餘額度。"
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
if __name__ == "__main__":
|
| 127 |
-
iface.launch()
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|