CHUNYU0505 commited on
Commit
7d4ca1a
·
verified ·
1 Parent(s): 223bb48

更新專案

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -1,33 +1,39 @@
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:
@@ -38,17 +44,15 @@ else:
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(
@@ -58,15 +62,14 @@ qa_chain = RetrievalQA.from_chain_type(
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}"
@@ -74,16 +77,16 @@ def get_hf_rate_limit():
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})
@@ -95,17 +98,16 @@ def generate_article_with_rate(query, segments=5):
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,
@@ -121,4 +123,6 @@ iface = gr.Interface(
121
  description="使用 Hugging Face Hub LLM + FAISS RAG,生成文章並提示 API 剩餘額度。"
122
  )
123
 
124
- iface.launch()
 
 
 
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.vectorstores import FAISS
9
  from langchain.chains import RetrievalQA
10
+ from langchain_huggingface import HuggingFaceHub # <-- 正確的 Import
11
+ from langchain.embeddings import HuggingFaceEmbeddings
12
  from docx import Document as DocxDocument
13
  import gradio as gr
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:
 
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(
 
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}"
 
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})
 
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,
 
123
  description="使用 Hugging Face Hub LLM + FAISS RAG,生成文章並提示 API 剩餘額度。"
124
  )
125
 
126
+ if __name__ == "__main__":
127
+ iface.launch()
128
+