CHUNYU0505 commited on
Commit
255d19f
·
verified ·
1 Parent(s): 132ef2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -13
app.py CHANGED
@@ -68,23 +68,36 @@ qa_chain = RetrievalQA.from_chain_type(
68
  )
69
 
70
  # -------------------------------
71
- # 5. 查詢 API 剩餘額度
72
  # -------------------------------
73
- def get_hf_rate_limit():
 
74
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
75
  try:
76
- r = requests.get("https://huggingface.co/api/whoami", headers=headers)
77
  r.raise_for_status()
78
  data = r.json()
79
- remaining = data.get("rate_limit", {}).get("remaining", "未知")
80
- return f"本小時剩餘 API 次數:約 {remaining}"
 
81
  except Exception:
82
- return "無法取得 API 速率資訊"
 
 
 
 
 
83
 
84
  # -------------------------------
85
- # 6. 生成文章(可即時顯示進度)
86
  # -------------------------------
87
- def generate_article_progress(query, segments=5):
 
 
 
 
 
 
88
  docx_file = "/tmp/generated_article.docx"
89
  doc = DocxDocument()
90
  doc.add_heading(query, level=1)
@@ -105,16 +118,17 @@ def generate_article_progress(query, segments=5):
105
  doc.add_paragraph(paragraph)
106
  prompt = f"請接續上一段生成下一段:\n{paragraph}\n\n下一段:"
107
 
108
- # yield 即時更新 Textbox
109
  yield "\n\n".join(all_text), None
 
110
 
111
  doc.save(docx_file)
112
  rate_info = get_hf_rate_limit()
113
- yield f"{rate_info}\n\n" + "\n\n".join(all_text), docx_file
 
114
 
115
 
116
  # -------------------------------
117
- # 7. Gradio 介面(支援進度更新)
118
  # -------------------------------
119
  with gr.Blocks() as demo:
120
  gr.Markdown("# 佛教經論 RAG 系統 (HF API)")
@@ -126,11 +140,17 @@ with gr.Blocks() as demo:
126
  output_file = gr.File(label="下載 DOCX")
127
 
128
  btn = gr.Button("生成文章")
129
- btn.click(generate_article_progress, [query_input, segments_input], [output_text, output_file])
130
 
 
 
 
 
 
 
131
 
132
  # -------------------------------
133
- # 8. 啟動 Gradio(Hugging Face Space 適用)
134
  # -------------------------------
135
  if __name__ == "__main__":
136
  demo.launch()
 
 
68
  )
69
 
70
  # -------------------------------
71
+ # 5. 檢查 Hugging Face Token 權限
72
  # -------------------------------
73
+ def check_hf_token_permissions():
74
+ """確認 Token 是否可呼叫 Inference Endpoint"""
75
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
76
  try:
77
+ r = requests.get("https://huggingface.co/api/whoami-v2", headers=headers)
78
  r.raise_for_status()
79
  data = r.json()
80
+ if "allow_inference" in data and data["allow_inference"]:
81
+ return True
82
+ return False
83
  except Exception:
84
+ return False
85
+
86
+ token_valid = check_hf_token_permissions()
87
+ if not token_valid:
88
+ print("⚠ 警告:Hugging Face API Token 權限不足,無法呼叫模型。")
89
+
90
 
91
  # -------------------------------
92
+ # 6. 生成文章(修正版,支援進度顯示)
93
  # -------------------------------
94
+ def generate_article_with_progress(query, segments=5):
95
+ if not token_valid:
96
+ # Token 權限不足,直接返回訊息
97
+ yield "⚠ API Token 權限不足,請檢查 Token 是否允許呼叫 Inference Endpoint。", None
98
+ return
99
+
100
+ import time
101
  docx_file = "/tmp/generated_article.docx"
102
  doc = DocxDocument()
103
  doc.add_heading(query, level=1)
 
118
  doc.add_paragraph(paragraph)
119
  prompt = f"請接續上一段生成下一段:\n{paragraph}\n\n下一段:"
120
 
 
121
  yield "\n\n".join(all_text), None
122
+ time.sleep(0.1)
123
 
124
  doc.save(docx_file)
125
  rate_info = get_hf_rate_limit()
126
+ final_text = f"{rate_info}\n\n" + "\n\n".join(all_text)
127
+ yield final_text, docx_file
128
 
129
 
130
  # -------------------------------
131
+ # 7. Gradio 介面(修正版)
132
  # -------------------------------
133
  with gr.Blocks() as demo:
134
  gr.Markdown("# 佛教經論 RAG 系統 (HF API)")
 
140
  output_file = gr.File(label="下載 DOCX")
141
 
142
  btn = gr.Button("生成文章")
 
143
 
144
+ # 使用 .click() 搭配 generator
145
+ btn.click(
146
+ fn=generate_article_with_progress,
147
+ inputs=[query_input, segments_input],
148
+ outputs=[output_text, output_file]
149
+ )
150
 
151
  # -------------------------------
152
+ # 8. 啟動 Gradio(HF Space 適用)
153
  # -------------------------------
154
  if __name__ == "__main__":
155
  demo.launch()
156
+