make a minimal GUI for a chat application for LLM. Use streamlit for the interface. We want to be able to edit the history of the model, stored as one big string, or editable text field in the graphical application. The history should not be a json file anymore, just a raw text file. Below is the base code for the CLI application.
py
from gradio_client import Client
import json
try:
with open('history.json', 'r') as f:
history = json.load(f)
for h in history:
print(h)
except:
history = []
while True:
client = Client('yuntian-deng/o1mini')
prompt = input()
if prompt != '':
history.append(prompt)
result = client.predict(
inputs=history,
top_p=1,
temperature=1,
chat_counter=0,
chatbot=[],
api_name='/predict'
)
output = result[0][0][1]
history.append(output)
print(f"{output}")
with open('history.json', 'w') as f:
json.dump(history, f, indent=2)
Assistant
Certainly! Below is a minimal Streamlit-based GUI for a chat application using a Large Language Model (LLM). This GUI allows users to interact with the model, view and edit the chat history as a raw text file, and persist the history between sessions.
Features:
Editable Chat History: The chat history is displayed in a text area where users can edit it directly.
User Input: Users can input new messages which are sent to the LLM.
Model Response: The LLM's response is appended to the chat history.
Persistence: Chat history is saved and loaded from a history.txt file.
Implementation Details:
Dependencies:
streamlit: For creating the web-based GUI.
gradio_client: To interact with the LLM API.
State Management:
Session State: Utilized to manage the chat history within the Streamlit app.
File Handling:
Loading History: On startup, the app attempts to load history.txt. If it doesn't exist, it initializes an empty history.
Saving History: After each interaction, the updated history is saved back to history.txt.
User Interaction Flow:
User types a message and clicks "Send".
The message is appended to the history.
The app calls the LLM API with the current history.
The LLM's response is appended to the history.
The updated history is displayed and saved.
Complete Streamlit App Code:
python
import streamlit as st
from gradio_client import Client
import os
# Constants
HISTORY_FILE = 'history.txt'
MODEL_CLIENT_NAME = 'yuntian-deng/o1mini' # Replace with your actual model identifier
# Initialize session state for history
if 'history' not in st.session_state:
if os.path.exists(HISTORY_FILE):
with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
st.session_state.history = f.read()
else:
st.session_state.history = ""
# Streamlit App Layout
st.title("LLM Chat Application")
# Editable text area for history
history = st.text_area("Chat History", value=st.session_state.history, height=300)
# Input for new user message
user_input = st.text_input("Your Message:", "")
# Send button
if st.button("Send"):
if user_input.strip() != "":
# Append user message to history
st.session_state.history += f"User: {user_input}\n"
# Display the updated history in the text area
st.text_area("Chat History", value=st.session_state.history, height=300, key='history_display')
# Initialize the client
client = Client(MODEL_CLIENT_NAME)
# Prepare inputs - split history into list if required by the model
# Assuming the model expects the full history as a single string
prompt = st.session_state.history
# Call the model to get a response
try:
result = client.predict(
inputs=prompt,
top_p=1,
temperature=1,
chat_counter=0,
chatbot=[],
api_name='/predict'
)
# Assuming the result structure is similar to the CLI version
# Adjust based on actual API response
output = result[0][0][1]
# Append model response to history
st.session_state.history += f"LLM: {output}\n"
# Update the text area with the new history
st.text_area("Chat History", value=st.session_state.history, height=300, key='history_display')
# Save the updated history to the text file
with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
f.write(st.session_state.history)
except Exception as e:
st.error(f"Error communicating with the model: {e}")
# Optionally, allow users to manually save edited history
if st.button("Save History"):
with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
f.write(history)
st.success("History saved successfully!")
# Optionally, allow users to clear the history
if st.button("Clear History"):
st.session_state.history = ""
with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
f.write(st.session_state.history)
st.text_area("Chat History", value=st.session_state.history, height=300, key='history_display')
st.success("History cleared!")
Instructions to Run the App:
Install Dependencies:
Ensure you have Streamlit and Gradio Client installed. You can install them using pip:
bash
pip install streamlit gradio
Save the Code:
Save the above script to a file, for example, chat_app.py.
Run the Streamlit App:
Navigate to the directory containing chat_app.py and run:
bash
streamlit run chat_app.py
Interact with the App:
A new browser window/tab will open displaying the chat interface.
You can view and edit the chat history in the text area.
Type your message in the input box and click "Send" to interact with the LLM.
Use the "Save History" button to manually save any edits made to the history.
Use the "Clear History" button to reset the chat history.
Notes:
Model Client: Ensure that 'yuntian-deng/o1mini' is the correct identifier for your LLM model. If you're using a different model or a private endpoint, adjust MODEL_CLIENT_NAME accordingly.
Error Handling: The app includes basic error handling for communication with the model. Ensure that the model endpoint is accessible and that your API keys (if any) are correctly configured.
History Format: The history is maintained as a plain text file with each message prefixed by the speaker (User: or LLM:). You can customize the format as needed.
Enhancements: This is a minimal implementation. Depending on your requirements, you might want to add features like user authentication, better UI/UX, support for multiple conversations, etc.
Feel free to customize and expand upon this foundation to suit your specific needs!