daviddwlee84 commited on
Commit
11f97a3
·
1 Parent(s): 1d9658a

Add chat_input version Streamlit

Browse files
Files changed (3) hide show
  1. Gradio_UI.py +2 -2
  2. README.md +4 -1
  3. streamlit_app_chat.py +36 -0
Gradio_UI.py CHANGED
@@ -239,7 +239,7 @@ class GradioUI:
239
  if not os.path.exists(file_upload_folder):
240
  os.mkdir(file_upload_folder)
241
 
242
- def interact_with_agent(self, prompt, messages):
243
  import gradio as gr
244
 
245
  messages.append(gr.ChatMessage(role="user", content=prompt))
@@ -301,7 +301,7 @@ class GradioUI:
301
  f"File uploaded: {file_path}", visible=True
302
  ), file_uploads_log + [file_path]
303
 
304
- def log_user_message(self, text_input, file_uploads_log):
305
  return (
306
  text_input
307
  + (
 
239
  if not os.path.exists(file_upload_folder):
240
  os.mkdir(file_upload_folder)
241
 
242
+ def interact_with_agent(self, prompt: str, messages: list):
243
  import gradio as gr
244
 
245
  messages.append(gr.ChatMessage(role="user", content=prompt))
 
301
  f"File uploaded: {file_path}", visible=True
302
  ), file_uploads_log + [file_path]
303
 
304
+ def log_user_message(self, text_input: str, file_uploads_log):
305
  return (
306
  text_input
307
  + (
README.md CHANGED
@@ -22,10 +22,13 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
22
  - [First Agent Template - a Hugging Face Space by daviddwlee84](https://huggingface.co/spaces/daviddwlee84/First_Agent)
23
 
24
  ```bash
 
25
  # Gradio (TODO: somehow buggy)
26
  python app.py
27
- # Streamlit (TODO: use `chat_input` and able to clear message history)
28
  streamlit run streamlit_app.py
 
 
29
  # Chainlit (TODO: move the ReAct process from agent message to "step")
30
  chainlit run chainlit_app.py
31
  ```
 
22
  - [First Agent Template - a Hugging Face Space by daviddwlee84](https://huggingface.co/spaces/daviddwlee84/First_Agent)
23
 
24
  ```bash
25
+ # https://www.gradio.app/guides/quickstart#building-your-first-demo
26
  # Gradio (TODO: somehow buggy)
27
  python app.py
28
+ # Streamlit (TODO: able to clear message history)
29
  streamlit run streamlit_app.py
30
+ # Streamlit chat_input (TODO: move ReAct process to `st.expander`)
31
+ streamlit run streamlit_app_chat.py
32
  # Chainlit (TODO: move the ReAct process from agent message to "step")
33
  chainlit run chainlit_app.py
34
  ```
streamlit_app_chat.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from app import agent # your CodeAgent instance
4
+ from Gradio_UI import stream_to_gradio # generator yielding gr.ChatMessage objects
5
+
6
+ st.set_page_config(page_title="CodeAgent Chat", layout="wide")
7
+ st.title("CodeAgent Chat (Streamlit)")
8
+
9
+ # Initialize session state for chat history.
10
+ if "chat_history" not in st.session_state:
11
+ st.session_state.chat_history = []
12
+
13
+ # Reset Chat button.
14
+ if st.button("Reset Chat"):
15
+ st.session_state.chat_history = []
16
+ st.rerun() # Rerun the script to update the UI immediately.
17
+
18
+ # Display chat history using Streamlit's chat message container.
19
+ for chat in st.session_state.chat_history:
20
+ st.chat_message(chat["role"]).write(chat["content"])
21
+
22
+ # Use st.chat_input for user message entry.
23
+ user_input = st.chat_input("Type your message here")
24
+ if user_input:
25
+ # Append and display the user's message.
26
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
27
+ st.chat_message("user").write(user_input)
28
+
29
+ # Stream the agent responses.
30
+ # The generator yields gr.ChatMessage objects that have 'role' and 'content' attributes.
31
+ for msg in stream_to_gradio(agent, user_input, reset_agent_memory=False):
32
+ role = msg.role
33
+ # Convert non-string content to a string as needed.
34
+ content = msg.content if isinstance(msg.content, str) else str(msg.content)
35
+ st.session_state.chat_history.append({"role": role, "content": content})
36
+ st.chat_message(role).write(content)