Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
# 使用 Hugging Face Inference API 调用云端模型(无需本地加载)
|
5 |
+
client = InferenceClient(token="hf_your_token")
|
6 |
+
|
7 |
+
def medical_chat(user_input, history):
|
8 |
+
# 构建医学对话 prompt
|
9 |
+
prompt = f"患者:{user_input}\n医生:"
|
10 |
+
|
11 |
+
# 调用云端模型(示例使用微软BioGPT)
|
12 |
+
response = client.text_generation(
|
13 |
+
prompt=prompt,
|
14 |
+
model="microsoft/BioGPT-Large",
|
15 |
+
max_new_tokens=150,
|
16 |
+
temperature=0.7,
|
17 |
+
repetition_penalty=1.2
|
18 |
+
)
|
19 |
+
|
20 |
+
# 提取医生回复部分
|
21 |
+
doctor_response = response.split("医生:")[-1].strip()
|
22 |
+
|
23 |
+
# 添加安全过滤
|
24 |
+
if "死亡" in doctor_response or "癌症" in doctor_response:
|
25 |
+
return "⚠️ 请及时联系线下医疗机构进行专业诊断!"
|
26 |
+
|
27 |
+
return doctor_response
|
28 |
+
|
29 |
+
# 构建 Gradio 界面
|
30 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
31 |
+
gr.Markdown("# 🏥 在线医疗问诊系统 (AI版)")
|
32 |
+
gr.ChatInterface(
|
33 |
+
fn=medical_chat,
|
34 |
+
examples=["头痛三天了怎么办?", "接种疫苗后发烧正常吗?"],
|
35 |
+
title="症状咨询示例"
|
36 |
+
)
|
37 |
+
|
38 |
+
# 启动应用(Hugging Face Spaces 会自动处理)
|
39 |
+
demo.launch(debug=False)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|