Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +19 -15
src/streamlit_app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
-
|
5 |
|
6 |
# =======================================================================
|
7 |
# 1. 导入您的类和数据
|
@@ -10,25 +10,29 @@ from datasets import load_dataset # 导入Hugging Face datasets库
|
|
10 |
# 请将 'your_agent_file' 替换为包含 MsPatient 类的实际文件名
|
11 |
from ms_patient import MsPatient
|
12 |
|
13 |
-
#
|
14 |
-
def
|
15 |
"""
|
16 |
-
|
17 |
-
|
18 |
"""
|
19 |
try:
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
except Exception as e:
|
26 |
-
st.error(f"
|
27 |
return []
|
28 |
|
29 |
# 加载数据
|
30 |
-
#
|
31 |
-
ALL_PATIENTS =
|
32 |
|
33 |
|
34 |
# =======================================================================
|
@@ -38,7 +42,7 @@ ALL_PATIENTS = load_hf_dataset()
|
|
38 |
# --- 页面配置 ---
|
39 |
st.set_page_config(
|
40 |
page_title="与Anna对话",
|
41 |
-
page_icon="
|
42 |
layout="wide"
|
43 |
)
|
44 |
|
@@ -115,7 +119,7 @@ with st.sidebar:
|
|
115 |
|
116 |
# 病人选择
|
117 |
if not ALL_PATIENTS:
|
118 |
-
st.error("
|
119 |
else:
|
120 |
patient_options = {p["id"]: f"{p['portrait']['gender']},{p['portrait']['age']}岁 - {p['portrait']['symptom']}" for p in ALL_PATIENTS}
|
121 |
selected_id = st.selectbox(
|
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
+
import json # 导入json库
|
5 |
|
6 |
# =======================================================================
|
7 |
# 1. 导入您的类和数据
|
|
|
10 |
# 请将 'your_agent_file' 替换为包含 MsPatient 类的实际文件名
|
11 |
from ms_patient import MsPatient
|
12 |
|
13 |
+
# 从本地JSON文件加载数据集的函数
|
14 |
+
def load_data_from_json(filepath="Anna-CPsyCounD.json"):
|
15 |
"""
|
16 |
+
从本地的JSON文件加载数据集。
|
17 |
+
请确保您已将数据集文件上传到与此应用相同的目录中。
|
18 |
"""
|
19 |
try:
|
20 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
21 |
+
# 假设JSON文件的根是一个包含病人记录的列表
|
22 |
+
return json.load(f)
|
23 |
+
except FileNotFoundError:
|
24 |
+
st.error(f"错误:找不到数据文件 '{filepath}'。请确保您已将该文件上传到Hugging Face Space。")
|
25 |
+
return []
|
26 |
+
except json.JSONDecodeError:
|
27 |
+
st.error(f"错误:无法解析 '{filepath}'。请检查文件是否为有效的JSON格式。")
|
28 |
+
return []
|
29 |
except Exception as e:
|
30 |
+
st.error(f"加载数据时发生未知错误: {e}")
|
31 |
return []
|
32 |
|
33 |
# 加载数据
|
34 |
+
# 注意:现在每次脚本重新运行时都会从本地JSON文件加载
|
35 |
+
ALL_PATIENTS = load_data_from_json()
|
36 |
|
37 |
|
38 |
# =======================================================================
|
|
|
42 |
# --- 页面配置 ---
|
43 |
st.set_page_config(
|
44 |
page_title="与Anna对话",
|
45 |
+
page_icon="�",
|
46 |
layout="wide"
|
47 |
)
|
48 |
|
|
|
119 |
|
120 |
# 病人选择
|
121 |
if not ALL_PATIENTS:
|
122 |
+
st.error("无法加载病人数据。请检查JSON文件是否已上传且格式正确。")
|
123 |
else:
|
124 |
patient_options = {p["id"]: f"{p['portrait']['gender']},{p['portrait']['age']}岁 - {p['portrait']['symptom']}" for p in ALL_PATIENTS}
|
125 |
selected_id = st.selectbox(
|