fdbw commited on
Commit
89a84b5
·
verified ·
1 Parent(s): 970e5d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -4,6 +4,8 @@ import requests
4
  # 填你的 DeepSeek Key(免费领:https://platform.deepseek.com/api_keys)
5
  DEEPSEEK_API_KEY = "sk-33b1f***********************0801" # ←←← 粘贴你的 Key
6
 
 
 
7
  with open("genealogy.txt", "r", encoding="utf-8") as f:
8
  GENEALOGY_TEXT = f.read()
9
 
@@ -13,17 +15,23 @@ SYSTEM_PROMPT = f"""你现在是冯氏家谱机器人,只根据以下家谱回
13
 
14
  def chat(message, history):
15
  messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": message}]
16
- response = requests.post(
17
- "https://api.deepseek.com/chat/completions",
18
- headers={"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"},
19
- json={"model": "deepseek-chat", "messages": messages, "temperature": 0.01, "max_tokens": 500}
20
- )
21
- return response.json()["choices"][0]["message"]["content"] if response.status_code == 200 else "错误,请检查 Key"
 
 
 
 
 
 
22
 
23
- with gr.Blocks() as demo:
24
  gr.Markdown("# 冯氏家谱机器人")
25
- chatbot = gr.Chatbot()
26
- msg = gr.Textbox(label="问问题")
27
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
28
 
29
  demo.launch()
 
4
  # 填你的 DeepSeek Key(免费领:https://platform.deepseek.com/api_keys)
5
  DEEPSEEK_API_KEY = "sk-33b1f***********************0801" # ←←← 粘贴你的 Key
6
 
7
+
8
+ # 读取家谱
9
  with open("genealogy.txt", "r", encoding="utf-8") as f:
10
  GENEALOGY_TEXT = f.read()
11
 
 
15
 
16
  def chat(message, history):
17
  messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": message}]
18
+ try:
19
+ response = requests.post(
20
+ "https://api.deepseek.com/chat/completions",
21
+ headers={"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"},
22
+ json={"model": "deepseek-chat", "messages": messages, "temperature": 0.01, "max_tokens": 500}
23
+ )
24
+ if response.status_code == 200:
25
+ return response.json()["choices"][0]["message"]["content"].strip()
26
+ else:
27
+ return f"API 错误:{response.status_code}"
28
+ except Exception as e:
29
+ return f"异常:{str(e)}"
30
 
31
+ with gr.Blocks(title="冯氏家谱机器人") as demo:
32
  gr.Markdown("# 冯氏家谱机器人")
33
+ chatbot = gr.Chatbot(height=500)
34
+ msg = gr.Textbox(label="输入问题", placeholder="例:冯达尊生于何时何地?")
35
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
36
 
37
  demo.launch()