forked from prcore/assistant
-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
75 lines (61 loc) · 2.38 KB
/
web.py
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
import os
from copy import deepcopy
import streamlit as st
from streamlit import runtime
from streamlit.delta_generator import DeltaGenerator
from streamlit.runtime.scriptrunner import get_script_run_ctx
import config
def set_web():
st.set_page_config(page_title=os.getenv("WEB_TITLE", "Docs Assistant"), layout="wide")
st.title(os.getenv("WEB_TITLE", "Docs Assistant"))
with open(config.INTRO_PATH, "r") as f:
intro_text = f.read()
st.markdown(intro_text, unsafe_allow_html=True)
with open(config.SIDEBAR_PATH, "r") as f:
sidebar_text = f.read()
with st.sidebar:
st.title(os.getenv("SIDEBAR_TITLE", "Links"))
st.markdown(sidebar_text, unsafe_allow_html=True)
if st.session_state.get("magic_mode", False):
if not st.session_state.get("celebrated", False):
st.balloons()
st.session_state["celebrated"] = True
with open(config.MAGIC_PATH, "r") as f:
magic_text = f.read()
st.markdown(magic_text, unsafe_allow_html=True)
hide_menu_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)
def display_conversation(container: DeltaGenerator):
with container.container():
message_containers = [st.empty() for _ in range(len(st.session_state["human"]) * 2)]
human_list = deepcopy(st.session_state["human"])
ai_list = deepcopy(st.session_state["ai"])
if len(human_list) > len(ai_list):
ai_list.append("...")
for i, (human_msg, ai_msg) in enumerate(zip(human_list, ai_list)):
message_containers[i * 2].info(human_msg, icon="💡")
message_containers[i * 2 + 1].success(ai_msg, icon="🤖")
def get_ip():
result = None
try:
ctx = get_script_run_ctx()
if ctx is None:
return None
session_info = runtime.get_instance().get_client(ctx.session_id)
if session_info is None:
return None
request = session_info.request # noqa
result = request.headers.get("X-Forwarded-For")
if not result:
return request.headers.get("Host")
if "," not in result:
return result
result = result.split(",")[0]
except Exception as e:
print(e)
return result