-
Notifications
You must be signed in to change notification settings - Fork 124
/
streamlitui.py
72 lines (54 loc) · 2.76 KB
/
streamlitui.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
import os
import tempfile
import streamlit as st
from streamlit_chat import message
from webquery import WebQuery
st.set_page_config(page_title="Website to Chatbot")
def display_messages():
st.subheader("Chat")
for i, (msg, is_user) in enumerate(st.session_state["messages"]):
message(msg, is_user=is_user, key=str(i))
st.session_state["thinking_spinner"] = st.empty()
def process_input():
if st.session_state["user_input"] and len(st.session_state["user_input"].strip()) > 0:
user_text = st.session_state["user_input"].strip()
with st.session_state["thinking_spinner"], st.spinner(f"Thinking"):
query_text = st.session_state["webquery"].ask(user_text)
st.session_state["messages"].append((user_text, True))
st.session_state["messages"].append((query_text, False))
def ingest_input():
if st.session_state["input_url"] and len(st.session_state["input_url"].strip()) > 0:
url = st.session_state["input_url"].strip()
with st.session_state["thinking_spinner"], st.spinner(f"Thinking"):
ingest_text = st.session_state["webquery"].ingest(url)
def is_openai_api_key_set() -> bool:
return len(st.session_state["OPENAI_API_KEY"]) > 0
def main():
if len(st.session_state) == 0:
st.session_state["messages"] = []
st.session_state["url"] = ""
st.session_state["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "")
if is_openai_api_key_set():
st.session_state["webquery"] = WebQuery(st.session_state["OPENAI_API_KEY"])
else:
st.session_state["webquery"] = None
st.header("Website to Chatbot")
if st.text_input("OpenAI API Key", value=st.session_state["OPENAI_API_KEY"], key="input_OPENAI_API_KEY", type="password"):
if (
len(st.session_state["input_OPENAI_API_KEY"]) > 0
and st.session_state["input_OPENAI_API_KEY"] != st.session_state["OPENAI_API_KEY"]
):
st.session_state["OPENAI_API_KEY"] = st.session_state["input_OPENAI_API_KEY"]
st.session_state["messages"] = []
st.session_state["user_input"] = ""
st.session_state["input_url"] = ""
st.session_state["webquery"] = WebQuery(st.session_state["OPENAI_API_KEY"])
st.subheader("Add a url")
st.text_input("Input url", value=st.session_state["url"], key="input_url", disabled=not is_openai_api_key_set(), on_change=ingest_input)
st.session_state["ingestion_spinner"] = st.empty()
display_messages()
st.text_input("Message", key="user_input", disabled=not is_openai_api_key_set(), on_change=process_input)
st.divider()
st.markdown("Source code: [Github](https://github.com/Anil-matcha/Website-to-Chatbot)")
if __name__ == "__main__":
main()