File size: 2,862 Bytes
b2ec4c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import time
import base64
import streamlit as st
from agent.react_agent import ReactAgent
import os

current_dir = os.path.dirname(os.path.abspath(__file__))

st.title("智扫通机器人智能客服")
st.divider()

if "agent" not in st.session_state:
    st.session_state["agent"] = ReactAgent()

if "message" not in st.session_state:
    st.session_state["message"] = []

for message in st.session_state["message"]:
    st.chat_message(message["role"]).write(message["content"])

prompt = st.chat_input()


def set_background_with_opacity(image_path, opacity=0.5):
    """

    设置带有透明度(半透明)的背景图片

    :param image_path: 本地图片路径

    :param opacity: 图片透明度,0为完全透明,1为完全不透明

    """
    with open(image_path, "rb") as f:
        img_data = f.read()
    encoded = base64.b64encode(img_data).decode()
    bg_url = f"data:image/jpg;base64,{encoded}"

    st.markdown(
        f"""

        <style>

        /* 主容器定位为相对位置,以便伪元素绝对定位 */

        .stApp {{

            position: relative;

            background: none;  /* 移除原有背景 */

        }}

        /* 伪元素作为背景层 */

        .stApp::before {{

            content: "";

            position: absolute;

            top: 0;

            left: 0;

            width: 100%;

            height: 100%;

            background: url({bg_url}) no-repeat center center fixed;

            background-size: cover;

            opacity: {opacity};      /* 关键:设置图片透明度 */

            z-index: -1;             /* 置于底层 */

            pointer-events: none;    /* 让背景层不干扰鼠标事件 */

        }}

        /* 确保主要内容在背景层之上且有足够对比度,可选 */

        .stApp > div {{

            position: relative;

            z-index: 1;

        }}

        </style>

        """,
        unsafe_allow_html=True
    )


set_background_with_opacity(os.path.join(current_dir, "iu.jpg"), opacity=0.5)  # 调整透明度值

if prompt:
    st.chat_message("user").write(prompt)
    st.session_state["message"].append({
        "role": "user", "content": prompt
    })
    response_messages = []
    with st.spinner("智能客服思考中..."):
        res_stream = st.session_state["agent"].execute_stream(prompt)


        def capture(generator, cache_list):

            for chunk in generator:
                cache_list.append(chunk)
                for char in chunk:
                    time.sleep(0.01)
                    yield char


        st.chat_message("assistant").write_stream(capture(res_stream, response_messages))
        st.session_state["message"].append({
            "role": "assistant", "content": response_messages[-1]
        })