Spaces:
Running
Running
Abigail99216
commited on
Upload 4 files
Browse files
.env
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# 智谱 API 访问密钥配置
|
2 |
+
ZHIPUAI_API_KEY = "c9bc35e8e7c1c076a8aaba862efb19af.DhiaibnU9Mys34de"
|
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import time
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
from langchain_openai import ChatOpenAI
|
8 |
+
from langchain_core.output_parsers import StrOutputParser
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
|
11 |
+
load_dotenv()
|
12 |
+
zhipuai_api_key = os.getenv("ZHIPUAI_API_KEY")
|
13 |
+
|
14 |
+
# 使用中文Whisper模型
|
15 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
16 |
+
|
17 |
+
# 初始化对话记录
|
18 |
+
conversation = []
|
19 |
+
current_speaker = "患者"
|
20 |
+
|
21 |
+
def transcribe(audio):
|
22 |
+
global current_speaker
|
23 |
+
if audio is None:
|
24 |
+
return ""
|
25 |
+
|
26 |
+
sr, y = audio
|
27 |
+
|
28 |
+
# 转换为单声道
|
29 |
+
if y.ndim > 1:
|
30 |
+
y = y.mean(axis=1)
|
31 |
+
|
32 |
+
y = y.astype(np.float32)
|
33 |
+
y /= np.max(np.abs(y))
|
34 |
+
|
35 |
+
# 使用中文进行转录
|
36 |
+
result = transcriber({"sampling_rate": sr, "raw": y}, generate_kwargs={"language": "chinese"})
|
37 |
+
text = result["text"].strip()
|
38 |
+
|
39 |
+
# 创建结构化数据
|
40 |
+
if text:
|
41 |
+
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
42 |
+
conversation.append({
|
43 |
+
"时间": current_time,
|
44 |
+
"角色": current_speaker,
|
45 |
+
"内容": text
|
46 |
+
})
|
47 |
+
|
48 |
+
# 切换说话者
|
49 |
+
current_speaker = "医生" if current_speaker == "患者" else "患者"
|
50 |
+
|
51 |
+
# 将对话记录转换为格式化的字符串
|
52 |
+
formatted_conversation = json.dumps(conversation, ensure_ascii=False, indent=2)
|
53 |
+
return formatted_conversation
|
54 |
+
|
55 |
+
def switch_speaker():
|
56 |
+
global current_speaker
|
57 |
+
current_speaker = "医生" if current_speaker == "患者" else "患者"
|
58 |
+
return f"当前说话者:{current_speaker}"
|
59 |
+
|
60 |
+
def generate_memo(conversation_json):
|
61 |
+
llm = ChatOpenAI(
|
62 |
+
model="glm-3-turbo",
|
63 |
+
temperature=0.7,
|
64 |
+
openai_api_key=zhipuai_api_key,
|
65 |
+
openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
|
66 |
+
)
|
67 |
+
|
68 |
+
prompt = f"""
|
69 |
+
请根据以下医生和患者的对话,生成一份结构化的备忘录。备忘录应包含以下字段:主诉、检查、诊断、治疗和备注。
|
70 |
+
如果某个字段在对话中没有明确提及,请填写"未提及"。
|
71 |
+
|
72 |
+
对话内容:
|
73 |
+
{conversation_json}
|
74 |
+
|
75 |
+
请以JSON格式输出备忘录,格式如下:
|
76 |
+
{{
|
77 |
+
"主诉": "患者的主要症状和不适",
|
78 |
+
"检查": "医生建议或已进行的检查",
|
79 |
+
"诊断": "医生对患者的诊断",
|
80 |
+
"治疗": "医生对患者的治疗建议",
|
81 |
+
"备注": "医生对患者的备注"
|
82 |
+
}}
|
83 |
+
"""
|
84 |
+
|
85 |
+
output = llm.invoke(prompt)
|
86 |
+
output_parser = StrOutputParser()
|
87 |
+
output = output_parser.invoke(output)
|
88 |
+
#st.info(output)
|
89 |
+
return output
|
90 |
+
|
91 |
+
|
92 |
+
# 创建Gradio界面
|
93 |
+
with gr.Blocks() as demo:
|
94 |
+
gr.Markdown("# 实时中文对话转录与备忘录生成")
|
95 |
+
gr.Markdown("点击麦克风图标开始录音,说话后会自动进行语音识别。支持中文识别。")
|
96 |
+
|
97 |
+
with gr.Row():
|
98 |
+
audio_input = gr.Audio(source="microphone", type="numpy", streaming=True)
|
99 |
+
speaker_button = gr.Button("切换说话者")
|
100 |
+
|
101 |
+
speaker_label = gr.Label("当前说话者:患者")
|
102 |
+
conversation_output = gr.JSON(label="对话记录")
|
103 |
+
memo_output = gr.JSON(label="备忘录")
|
104 |
+
|
105 |
+
generate_memo_button = gr.Button("生成备忘录")
|
106 |
+
|
107 |
+
audio_input.stream(transcribe, inputs=[audio_input], outputs=[conversation_output])
|
108 |
+
speaker_button.click(switch_speaker, outputs=[speaker_label])
|
109 |
+
generate_memo_button.click(generate_memo, inputs=[conversation_output], outputs=[memo_output])
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
demo.launch()
|
113 |
+
|
readme.md
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
1. 这是一个基于streamlit的web app,是一个临床助手
|
2 |
+
2. 首先,将医生和患者的对话转录成文本,输出成结构化文本,即{'时间', '角色', '内容'}
|
3 |
+
3. 然后,调用chatglm大模型,对结构化文本进行处理,输出一个结构化的memo,包含{'主诉', '检查', '诊断', '治疗', '备注'}
|
4 |
+
4. 最后,将memo返回给医生,医生可以对memo进行修改,然后下载
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers==4.28.1
|
2 |
+
gradio==3.28.1
|
3 |
+
numpy==1.22.4
|
4 |
+
torch==1.13.1
|
5 |
+
|