Jesus Sanchez commited on
Commit
e9f9de0
·
1 Parent(s): e470cbb
Files changed (2) hide show
  1. __init__.py +0 -0
  2. chat.py +43 -0
__init__.py ADDED
File without changes
chat.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from typing import Callable
3
+
4
+
5
+
6
+ RESPONSE_LABEL = 'chat_response'
7
+ PROMPT_LABEL = 'chat_user_input'
8
+
9
+ class Chat:
10
+ def __init__(self):
11
+ if RESPONSE_LABEL not in st.session_state:
12
+ st.session_state[RESPONSE_LABEL] = []
13
+
14
+ if PROMPT_LABEL not in st.session_state:
15
+ st.session_state[PROMPT_LABEL] = []
16
+
17
+ def get_input(self, process_prompt: Callable, *args):
18
+ """
19
+ process_prompt(promt: str, *args) -> tuple(Any, Callable)
20
+ callback to process the chat promt, it takes the promt for input
21
+ and returns a tuple with the response and a render callback
22
+ """
23
+
24
+ # Render history
25
+ messages = zip(st.session_state[PROMPT_LABEL], st.session_state[RESPONSE_LABEL])
26
+ for prompt, (response, on_render) in messages:
27
+ with st.chat_message("user"):
28
+ st.markdown(prompt)
29
+ with st.chat_message("assistant"):
30
+ on_render(response)
31
+
32
+ # Compute prompt
33
+ if prompt:= st.chat_input(placeholder="Ask Me Anything", key='chat_text_input'):
34
+ st.session_state[PROMPT_LABEL].append(prompt)
35
+ (response, on_render) = process_prompt(prompt, *args)
36
+ st.session_state[RESPONSE_LABEL].append((response, on_render))
37
+
38
+ with st.chat_message("user"):
39
+ st.markdown(prompt)
40
+
41
+ with st.chat_message("assistant"):
42
+ on_render(response)
43
+