CHUNYU0505 commited on
Commit
6328158
·
verified ·
1 Parent(s): 552e659

API 使用次數提示

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