User: With streamlit, how can I parse and manage Markdown text? Say I have an LLM responding to me and giving markdown, such as:
Title
bold text
bash
# This is some text
```User: How can I do that?
Streamlit provides built-in support for rendering Markdown, which makes it easy to parse and display Markdown text returned from a language model like an LLM. You can use the `st.markdown()` function to render the Markdown content directly on your Streamlit app. Here's a step-by-step guide on how you can manage and display Markdown text using Streamlit:
1. **Install Streamlit**: If you haven't already, you need to install Streamlit. You can do this using pip:
```bash
pip install streamlit
Create a Streamlit App: Create a new Python file, say app.py.
Write the Streamlit Code: Use the st.markdown() function to render the Markdown content returned by the LLM. For example:
python
import streamlit as st
# Sample Markdown text from an LLM
markdown_text = """
### Title
**bold text**
```bash
# This is some code in a bash script
"""
Display the Markdown text
st.markdown(markdown_text)
code
Run your Streamlit App: Open a terminal, navigate to the directory containing your app.py, and run:
bash
streamlit run app.py
This will start a local server and open your Streamlit app in a web browser.
Streamlit automatically parses and renders the Markdown content, including headings, bold text, and code blocks, in a visually appealing format. This makes it a great tool for displaying dynamic content generated by LLMs or other sources.
Additionally, you can use Markdown specific syntax within the function, such as tables, lists, images, and more to enhance the display of your content. If there are additional specific needs for your Markdown display, Streamlit offers other features and widgets to further customize the user interface.
User: And how can I prevent pressing return from sending the submit? to the message?
Assistant: When implementing a text input field in a Streamlit app, particularly when using a multi-line text input (like a chat interface with an LLM returning Markdown responses), it's common to want to prevent the 'Enter' or 'Return' key from automatically submitting the input. As of my last update, Streamlit doesn’t provide built-in support for intercepting keypress events like this directly in the simple text input widgets. However, you can manage this behavior using a workaround by implementing the following setup:
Assistant
st.markdown()
Using Session State
Install Streamlit if you haven't already done so:
bash
pip install streamlit
Create a new Streamlit app (app.py):
python
import streamlit as st
# Define or load previous user input or app state
if 'user_input' not in st.session_state:
st.session_state.user_input = ""
def handle_submit():
"""Handle the input when the form is submitted."""
# You can add logic here, like sending the input to an LLM
st.write("You submitted:", st.session_state.user_input)
# Create a form-like structure with a callback
with st.form(key='input_form'):
# Multi-line text input (textarea)
st.session_state.user_input = st.text_area("Enter Markdown text here:", value=st.session_state.user_input, height=100)
# Submit button with a callback
submitted = st.form_submit_button("Submit", on_click=handle_submit)
# Display the current content of the "user_input"
st.markdown("### You entered:")
st.markdown(st.session_state.user_input)
Run the Streamlit App:
bash
streamlit run app.py
Explanation:
Session State: We use st.session_state to hold the current state of the text input. This allows us to manage the text input state across interactions, as Streamlit re-runs the script from top to bottom each time the user interacts with the app.
Text Area: The text_area widget allows for multi-line input without auto-submission on pressing 'Enter'. This is crucial for allowing users to enter complex markdown without submitting each time they press return for a new line.
Form: By wrapping the input in a form (st.form()), you explicitly define a section of the app that behaves like a form, only submitting when the form_submit_button is pressed. This setup prevents Enter from submitting the form and instead only submits when the "Submit" button is clicked.
With this structure, you have control over when the input is considered complete and can be submitted, making the user experience more intuitive for submitting Markdown text especially when interacting with an LLM or similar applications.
User: Here is the code for my current application. Change it. Also, I would like it to scroll down to the history automatically. Code: py import streamlit as st from gradio_client import Client import os import json # Set page configuration st.set_page_config( page_title="LLM Chat Application", layout="wide", initial_sidebar_state="auto" ) # Constants HISTORY_FILE = 'history.txt' # This file stores the conversation history MODEL_CLIENT_NAME = 'yuntian-deng/ChatGPT4' # Replace with your actual model identifier CONFIG_FILE = 'config.json' # Config file to store top_p and temperature # Function to load history from the file def load_history(): if os.path.exists(HISTORY_FILE): with open(HISTORY_FILE, 'r', encoding='utf-8') as f: return f.read() return "" # Function to save history to the file def save_history(history): with open(HISTORY_FILE, 'w', encoding='utf-8') as f: f.write(history) # Function to load config from the file def load_config(): if os.path.exists(CONFIG_FILE): with open(CONFIG_FILE, 'r', encoding='utf-8') as f: return json.load(f) else: return {"top_p": 1.0, "temperature": 1.0} # Function to save config to the file def save_config(config): with open(CONFIG_FILE, 'w', encoding='utf-8') as f: json.dump(config, f) # Initialize session state for history if 'history' not in st.session_state: st.session_state.history = load_history() # Initialize session state for config if 'config' not in st.session_state: st.session_state.config = load_config() # We use a different key for the text area to avoid conflicts history_input = st.text_area( "Conversation", value=st.session_state.history, height=400, key='history_text_area', placeholder="Your conversation so far...", ) # Handle manual edits to the history if history_input != st.session_state.history: # Update the history with the user edits st.session_state.history = history_input save_history(st.session_state.history) # Create a form for new input with st.form("chat_form", clear_on_submit=True): user_input = st.text_input("Your Message", key='user_input', placeholder="Type your message here...") # Generate Response button generate = st.form_submit_button("Send Message") # Sliders for Temperature and Top-P sampling col1, col2 = st.columns(2) with col1: temperature = st.slider( "Temperature", min_value=0.0, max_value=5.0, value=st.session_state.config.get('temperature', 1.0), step=0.1, help="Controls the randomness of the model's output." ) with col2: top_p = st.slider( "Top-P Sampling", min_value=0.0, max_value=1.0, value=st.session_state.config.get('top_p', 1.0), step=0.05, help="Nucleus sampling." ) # Update the config in session_state and save to file when sliders change if temperature != st.session_state.config.get('temperature', 1.0) or top_p != st.session_state.config.get('top_p', 1.0): st.session_state.config['temperature'] = temperature st.session_state.config['top_p'] = top_p save_config(st.session_state.config) # Handle form submission if generate: if user_input.strip(): # Append user's message to the history st.session_state.history += f"User: {user_input.strip()}\n" save_history(st.session_state.history) try: # Initialize the client client = Client(MODEL_CLIENT_NAME) # Prepare the prompt prompt = st.session_state.history # Call the model to get a response result = client.predict( inputs=prompt, top_p=st.session_state.config['top_p'], temperature=st.session_state.config['temperature'], chat_counter=0, chatbot=[], api_name='/predict' ) # Adjust based on actual API response structure # Assuming 'result' is a list of lists where result[0][0][1] is the response output = result[0][0][1].strip() # Append model response to history st.session_state.history += f"Assistant: {output}\n" st.markdown(st.session_state.history) # Save updated history save_history(st.session_state.history) # Re-render the page to show updated history st.rerun() except Exception as e: st.error(f"Error communicating with the model: {e}") else: st.warning("Please enter a message.")
To modify your Streamlit application based on your requirements (preventing the Enter key from submitting and automatically scrolling to the history), follow these steps to adjust the provided code:
Change Text Input to Text Area: Use a text area for the new user input to allow multi-line input without submitting automatically with the Return key. We can use Streamlit's form submissions to handle Enter key better.
Auto-Scroll to Latest Messages: We will ensure the message history auto-scrolls by arranging how we proceed with displaying the conversation at the end of every submit.
Here is the modified version of your code:
python
import streamlit as st
from gradio_client import Client
import os
import json
# Set page configuration
st.set_page_config(
page_title="LLM Chat Application",
layout="wide",
initial_sidebar_state="auto"
)
# Constants
HISTORY_FILE = 'history.txt' # This file stores the conversation history
MODEL_CLIENT_NAME = 'yuntian-deng/ChatGPT4' # Replace with your actual model identifier
CONFIG_FILE = 'config.json' # Config file to store top_p and temperature
# Function to load history from the file
def load_history():
if os.path.exists(HISTORY_FILE):
with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
return f.read()
return ""
# Function to save history to the file
def save_history(history):
with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
f.write(history)
# Function to load config from the file
def load_config():
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
else:
return {"top_p": 1.0, "temperature": 1.0}
# Function to save config to the file
def save_config(config):
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(config, f)
# Initialize session state for history
if 'history' not in st.session_state:
st.session_state.history = load_history()
# Initialize session state for config
if 'config' not in st.session_state:
st.session_state.config = load_config()
# We use a different key for the text area to avoid conflicts
history_input = st.empty()
# Create a form for new input
with st.form("chat_form", clear_on_submit=True):
user_input = st.text_area("Your Message", key='user_input', placeholder="Type your message here...")
# Generate Response button
generate = st.form_submit_button("Send Message")
# Sliders for Temperature and Top-P sampling
col1, col2 = st.columns(2)
with col1:
temperature = st.slider(
"Temperature",
min_value=0.0,
max_value=5.0,
value=st.session_state.config.get('temperature', 1.0),
step=0.1,
help="Controls the randomness of the model's output."
)
with col2:
top_p = st.slider(
"Top-P Sampling",
min_value=0.0,
max_value=1.0,
value=st.session_state.config.get('top_p', 1.0),
step=0.05,
help="Nucleus sampling."
)
# Update the config in session_state and save to file when sliders change
if temperature != st.session_state.config.get('temperature', 1.0) or top_p != st.session_state.config.get('top_p', 1.0):
st.session_state.config['temperature'] = temperature
st.session_state.config['top_p'] = top_p
save_config(st.session_state.config)
# Handle form submission
if generate:
if user_input.strip():
# Append user's message to the history
st.session_state.history += f"User: {user_input.strip()}\n"
save_history(st.session_state.history)
try:
# Initialize the client
client = Client(MODEL_CLIENT_NAME)
# Prepare the prompt
prompt = st.session_state.history
# Call the model to get a response
result = client.predict(
inputs=prompt,
top_p=st.session_state.config['top_p'],
temperature=st.session_state.config['temperature'],
chat_counter=0,
chatbot=[],
api_name='/predict'
)
# Adjust based on actual API response structure
# Assuming 'result' is a list of lists where result[0][0][1] is the response
output = result[0][0][1].strip()
# Append model response to history
st.session_state.history += f"Assistant: {output}\n"
save_history(st.session_state.history)
except Exception as e:
st.error(f"Error communicating with the model: {e}")
else:
st.warning("Please enter a message.")
# Update the markdown display with new conversation history
history_input.markdown(st.session_state.history)
Key Changes
Text Area for Input: The st.text_area is used instead of st.text_input for user inputs, allowing multi-line inputs with no auto-submission on pressing 'Enter'.
Auto-Scroll Down: The rendering part history_input.markdown(st.session_state.history) is moved outside the form submission to ensure the updated history is rendered continuously regardless of user activity.
This setup ensures that the conversation history is preserved appropriately, user inputs are not prematurely submitted with the 'Enter' key, and the app always displays the most recent conversation.