Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -203,7 +203,7 @@ def get_tax_law(tax_type):
|
|
203 |
}
|
204 |
return tax_law_dict.get(tax_type, "無稅法")
|
205 |
|
206 |
-
def fetch_law_summary(tax_law, keywords):
|
207 |
url = "https://ttc.mof.gov.tw/Api/GetData"
|
208 |
headers = {
|
209 |
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
@@ -274,12 +274,65 @@ def fetch_law_summary(tax_law, keywords):
|
|
274 |
if tax_sn not in unique_results:
|
275 |
unique_results[tax_sn] = result
|
276 |
for index, result in enumerate(list(unique_results.values())[:20]): # 限制為前20個唯一結果
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
return summary
|
284 |
else:
|
285 |
return "<p>未檢索到相關法令彙編函釋。</p>"
|
@@ -305,7 +358,7 @@ def llm_openai_api(query, answer):
|
|
305 |
{"role": "user", "content": user_prompt}
|
306 |
],
|
307 |
temperature=0.7,
|
308 |
-
top_p=
|
309 |
)
|
310 |
return response.choices[0].message.content.strip()
|
311 |
except Exception as e:
|
@@ -323,8 +376,8 @@ def handle_interaction(query, api_key, state):
|
|
323 |
params: resend.Emails.SendParams = {
|
324 |
"from": "Tax_KM <[email protected]>",
|
325 |
"to": ["[email protected]"],
|
326 |
-
"subject": "
|
327 |
-
"html": f"<strong
|
328 |
}
|
329 |
try:
|
330 |
email_response = resend.Emails.send(params)
|
@@ -349,7 +402,7 @@ def handle_interaction(query, api_key, state):
|
|
349 |
print(f"Tax Law: {tax_law}")
|
350 |
print(f"Keywords: {keywords}")
|
351 |
tax_law = get_tax_law(tax_name)
|
352 |
-
law_summary_content = fetch_law_summary(tax_law, keywords)
|
353 |
else:
|
354 |
law_summary_content = ""
|
355 |
gr.Info(f"多個問題不會提供法令彙編函釋檢索結果。")
|
@@ -455,8 +508,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as iface:
|
|
455 |
> ### **※ RAG-based KM 以地方稅極少知識資料作示範,僅供參考,準確資訊請依地方稅稽徵機關回覆為準。系統部署:江信宗,LLM:Llama-3.1-70B。**
|
456 |
""", elem_classes="center-text")
|
457 |
with gr.Row():
|
458 |
-
query_input = gr.Textbox(label="輸入您的問題,系統將基於學習到的知識資料提供相關答案。", placeholder="
|
459 |
-
api_key_input = gr.Textbox(label="
|
460 |
answer_output = gr.Textbox(label="知識庫答案", interactive=False, max_lines=40, elem_classes="answer-box")
|
461 |
with gr.Row():
|
462 |
insight_q1 = gr.Button("洞察問題 1", visible=False, elem_classes=["insight-btn"])
|
|
|
203 |
}
|
204 |
return tax_law_dict.get(tax_type, "無稅法")
|
205 |
|
206 |
+
def fetch_law_summary(query, tax_law, keywords):
|
207 |
url = "https://ttc.mof.gov.tw/Api/GetData"
|
208 |
headers = {
|
209 |
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
|
274 |
if tax_sn not in unique_results:
|
275 |
unique_results[tax_sn] = result
|
276 |
for index, result in enumerate(list(unique_results.values())[:20]): # 限制為前20個唯一結果
|
277 |
+
client = OpenAI(
|
278 |
+
api_key=os.environ.get("YOUR_API_TOKEN"),
|
279 |
+
base_url="https://api.sambanova.ai/v1",
|
280 |
+
)
|
281 |
+
system_prompt = f"""
|
282 |
+
請判斷以下函釋內容與user提問的內容。
|
283 |
+
請給出一個0到100之間的相關性百分比,不要任何說明或理由。
|
284 |
+
回答格式為:
|
285 |
+
相關性:XX%
|
286 |
+
|
287 |
+
函釋內容:```{result['Content']}```
|
288 |
"""
|
289 |
+
prompt = f"""```{query}```"""
|
290 |
+
max_retries = 2
|
291 |
+
retry_delay = 4
|
292 |
+
for attempt in range(max_retries):
|
293 |
+
try:
|
294 |
+
response = client.chat.completions.create(
|
295 |
+
model="Meta-Llama-3.1-405B-Instruct",
|
296 |
+
messages=[
|
297 |
+
{"role": "system", "content": system_prompt},
|
298 |
+
{"role": "user", "content": prompt}
|
299 |
+
],
|
300 |
+
temperature=0.7
|
301 |
+
)
|
302 |
+
relevance_percentage = response.choices[0].message.content.strip()
|
303 |
+
break
|
304 |
+
except Exception as e:
|
305 |
+
if (attempt == 0) or (attempt == max_retries - 1):
|
306 |
+
try:
|
307 |
+
response = client.chat.completions.create(
|
308 |
+
model="Meta-Llama-3.1-70B-Instruct",
|
309 |
+
messages=[
|
310 |
+
{"role": "system", "content": system_prompt},
|
311 |
+
{"role": "user", "content": prompt}
|
312 |
+
],
|
313 |
+
temperature=0.7
|
314 |
+
)
|
315 |
+
relevance_percentage = response.choices[0].message.content.strip()
|
316 |
+
break
|
317 |
+
except Exception as e2:
|
318 |
+
relevance_percentage = "相關性:0%"
|
319 |
+
break
|
320 |
+
else:
|
321 |
+
print(f"Retrying in {retry_delay} seconds...")
|
322 |
+
time.sleep(retry_delay)
|
323 |
+
retry_delay *= 2
|
324 |
+
try:
|
325 |
+
percentage = int(relevance_percentage.split(":")[1].strip().rstrip('%'))
|
326 |
+
except ValueError:
|
327 |
+
print(f"Warning: Could not parse relevance percentage from '{relevance_percentage}'")
|
328 |
+
percentage = 0
|
329 |
+
if percentage > 0:
|
330 |
+
summary += f"""
|
331 |
+
<details>
|
332 |
+
<summary style="cursor: pointer; color: #0066cc;">{result['Title']} (相關性:{percentage} %)</summary>
|
333 |
+
<p>{result['Content']}</p>
|
334 |
+
</details>
|
335 |
+
"""
|
336 |
return summary
|
337 |
else:
|
338 |
return "<p>未檢索到相關法令彙編函釋。</p>"
|
|
|
358 |
{"role": "user", "content": user_prompt}
|
359 |
],
|
360 |
temperature=0.7,
|
361 |
+
top_p=0.9
|
362 |
)
|
363 |
return response.choices[0].message.content.strip()
|
364 |
except Exception as e:
|
|
|
376 |
params: resend.Emails.SendParams = {
|
377 |
"from": "Tax_KM <[email protected]>",
|
378 |
"to": ["[email protected]"],
|
379 |
+
"subject": "地方稅知識庫 API KEY",
|
380 |
+
"html": f"<strong>檢索內容:<br>{query}</strong>",
|
381 |
}
|
382 |
try:
|
383 |
email_response = resend.Emails.send(params)
|
|
|
402 |
print(f"Tax Law: {tax_law}")
|
403 |
print(f"Keywords: {keywords}")
|
404 |
tax_law = get_tax_law(tax_name)
|
405 |
+
law_summary_content = fetch_law_summary(query, tax_law, keywords)
|
406 |
else:
|
407 |
law_summary_content = ""
|
408 |
gr.Info(f"多個問題不會提供法令彙編函釋檢索結果。")
|
|
|
508 |
> ### **※ RAG-based KM 以地方稅極少知識資料作示範,僅供參考,準確資訊請依地方稅稽徵機關回覆為準。系統部署:江信宗,LLM:Llama-3.1-70B。**
|
509 |
""", elem_classes="center-text")
|
510 |
with gr.Row():
|
511 |
+
query_input = gr.Textbox(label="輸入您的問題,系統將基於學習到的知識資料提供相關答案。", placeholder="支援同時輸入多個問題,例如:問題1?問題2?", autofocus=True, scale=3, max_lines=5, elem_classes="query-input")
|
512 |
+
api_key_input = gr.Textbox(label="輸入您的 API Key", type="password", placeholder="API authentication key", scale=1, elem_classes="api-key-input")
|
513 |
answer_output = gr.Textbox(label="知識庫答案", interactive=False, max_lines=40, elem_classes="answer-box")
|
514 |
with gr.Row():
|
515 |
insight_q1 = gr.Button("洞察問題 1", visible=False, elem_classes=["insight-btn"])
|