EmbeddedAndrew commited on
Commit
97e1dbe
·
1 Parent(s): e2f8627
Files changed (3) hide show
  1. app.py +42 -11
  2. app_pword.py +64 -0
  3. lc_streaming_nopassword.py +33 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  # LC_Streaming.py
2
 
3
 
 
4
  from langchain.callbacks.base import BaseCallbackHandler
5
  from langchain.chat_models import ChatOpenAI
6
  from langchain.schema import HumanMessage
@@ -20,17 +21,47 @@ class StreamHandler(BaseCallbackHandler):
20
  else:
21
  raise ValueError(f"Invalid display_method: {self.display_method}")
22
 
23
- query = st.text_input("input your query", value="Tell me a joke")
24
- ask_button = st.button("ask")
25
 
26
- st.markdown("### GPT-3.5 response")
27
- chat_box = st.empty()
28
- stream_handler = StreamHandler(chat_box, display_method='write')
29
- chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
30
 
31
- #st.markdown("### together box")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- if query and ask_button:
34
- response = chat([HumanMessage(content=query)])
35
- # llm_response = response.content
36
- # st.markdown(llm_response)
 
1
  # LC_Streaming.py
2
 
3
 
4
+
5
  from langchain.callbacks.base import BaseCallbackHandler
6
  from langchain.chat_models import ChatOpenAI
7
  from langchain.schema import HumanMessage
 
21
  else:
22
  raise ValueError(f"Invalid display_method: {self.display_method}")
23
 
 
 
24
 
 
 
 
 
25
 
26
+ def check_password():
27
+ """Returns `True` if the user had the correct password."""
28
+
29
+ def password_entered():
30
+ """Checks whether a password entered by the user is correct."""
31
+ if st.session_state["password"] == os.environ["USER_PWORD"]:
32
+ st.session_state["password_correct"] = True
33
+ del st.session_state["password"] # don't store password
34
+ else:
35
+ st.session_state["password_correct"] = False
36
+
37
+ if "password_correct" not in st.session_state:
38
+ # First run, show input for password.
39
+ st.text_input(
40
+ "Password", type="password", on_change=password_entered, key="password"
41
+ )
42
+ return False
43
+ elif not st.session_state["password_correct"]:
44
+ # Password not correct, show input + error.
45
+ st.text_input(
46
+ "Password", type="password", on_change=password_entered, key="password"
47
+ )
48
+ st.error("😕 Password incorrect")
49
+ return False
50
+ else:
51
+ return True
52
+
53
+
54
+
55
+ if check_password():
56
+ query = st.text_input("Input your research question", value="How do biases in AI student evaluations compare to documented biases in human evaluations?")
57
+ ask_button = st.button("ask")
58
+
59
+ st.markdown("### GPT-3.5 response")
60
+ chat_box = st.empty()
61
+ stream_handler = StreamHandler(chat_box, display_method='write')
62
+ chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
63
+
64
+ #st.markdown("### together box")
65
 
66
+ if query and ask_button:
67
+ response = chat([HumanMessage(content=query)])
 
 
app_pword.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from langchain.callbacks.base import BaseCallbackHandler
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.schema import HumanMessage
5
+ import streamlit as st
6
+
7
+ class StreamHandler(BaseCallbackHandler):
8
+ def __init__(self, container, initial_text="", display_method='markdown'):
9
+ self.container = container
10
+ self.text = initial_text
11
+ self.display_method = display_method
12
+
13
+ def on_llm_new_token(self, token: str, **kwargs) -> None:
14
+ self.text += token # + "/"
15
+ display_function = getattr(self.container, self.display_method, None)
16
+ if display_function is not None:
17
+ display_function(self.text)
18
+ else:
19
+ raise ValueError(f"Invalid display_method: {self.display_method}")
20
+
21
+
22
+ import streamlit as st
23
+
24
+ def check_password():
25
+ """Returns `True` if the user had the correct password."""
26
+
27
+ def password_entered():
28
+ """Checks whether a password entered by the user is correct."""
29
+ if st.session_state["password"] == os.environ["USER_PWORD"]:
30
+ st.session_state["password_correct"] = True
31
+ del st.session_state["password"] # don't store password
32
+ else:
33
+ st.session_state["password_correct"] = False
34
+
35
+ if "password_correct" not in st.session_state:
36
+ # First run, show input for password.
37
+ st.text_input(
38
+ "Password", type="password", on_change=password_entered, key="password"
39
+ )
40
+ return False
41
+ elif not st.session_state["password_correct"]:
42
+ # Password not correct, show input + error.
43
+ st.text_input(
44
+ "Password", type="password", on_change=password_entered, key="password"
45
+ )
46
+ st.error("😕 Password incorrect")
47
+ return False
48
+ else:
49
+ # Password correct.
50
+ return True
51
+
52
+ if check_password():
53
+ query = st.text_input("Input your research question", value="How do biases in AI student evaluations compare to documented biases in human evaluations?")
54
+ ask_button = st.button("ask")
55
+
56
+ st.markdown("### GPT-3.5 response")
57
+ chat_box = st.empty()
58
+ stream_handler = StreamHandler(chat_box, display_method='write')
59
+ chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
60
+
61
+ #st.markdown("### together box")
62
+
63
+ if query and ask_button:
64
+ response = chat([HumanMessage(content=query)])
lc_streaming_nopassword.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.callbacks.base import BaseCallbackHandler
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.schema import HumanMessage
4
+ import streamlit as st
5
+
6
+ class StreamHandler(BaseCallbackHandler):
7
+ def __init__(self, container, initial_text="", display_method='markdown'):
8
+ self.container = container
9
+ self.text = initial_text
10
+ self.display_method = display_method
11
+
12
+ def on_llm_new_token(self, token: str, **kwargs) -> None:
13
+ self.text += token # + "/"
14
+ display_function = getattr(self.container, self.display_method, None)
15
+ if display_function is not None:
16
+ display_function(self.text)
17
+ else:
18
+ raise ValueError(f"Invalid display_method: {self.display_method}")
19
+
20
+ query = st.text_input("Input your research question", value="How do biases in AI student evaluations compare to documented biases in human evaluations?")
21
+ ask_button = st.button("ask")
22
+
23
+ st.markdown("### GPT-3.5 response")
24
+ chat_box = st.empty()
25
+ stream_handler = StreamHandler(chat_box, display_method='write')
26
+ chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
27
+
28
+ #st.markdown("### together box")
29
+
30
+ if query and ask_button:
31
+ response = chat([HumanMessage(content=query)])
32
+ # llm_response = response.content
33
+ # st.markdown(llm_response)