File size: 1,614 Bytes
b9e68ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

```python
import streamlit as st
import os
import runpy

# 从环境变量获取 token
token = os.getenv('token')
weather_token = os.getenv('weather_token')

st.set_page_config(layout="wide", page_title="My Multi-Page App")

def set_env_variable(key, value):
    os.environ[key] = value

def home_page():
    st.header("欢迎来到首页")
    # 检查环境变量是否已存在
    global token, weather_token
    if not token:
        token = st.text_input("请输入浦语 token:", type="password", key="token")
    if not weather_token:
        weather_token = st.text_input("请输入和风天气 token:", type="password", key="weather_token")
    
    if st.button("保存并体验 agent"):
        if token and weather_token:
            set_env_variable("token", token)  # 设置环境变量
            set_env_variable("weather_token", weather_token)
            st.session_state.token_entered = True
            st.rerun()
        else:
            st.error("请输入所有 token")

# 检查会话状态
if 'token_entered' not in st.session_state:
    # 如果 token 存在于环境变量中,直接跳过输入
    st.session_state.token_entered = bool(token and weather_token)

if not st.session_state.token_entered:
    home_page()
else:
    # 动态加载子页面
    page = st.sidebar.radio("选择页面", ["天气查询助手", "博客写作助手"])
    if page == "天气查询助手":
        runpy.run_path("examples/agent_api_web_demo.py", run_name="__main__")
    elif page == "博客写作助手":
        runpy.run_path("examples/multi_agents_api_web_demo.py", run_name="__main__")