Skip to main content

A custom chat input widget for Streamlit based on Mohammed Bahageel's Chat Input Widget

Project description

Streamlit Chat Widget

Streamlit Chat Widget

The streamlit_chat_widget is a custom chat input component designed for Streamlit applications, allowing users to send text and audio messages seamlessly. This widget can be integrated into Streamlit apps, making it ideal for building conversational AI interfaces, voice assistants, or any chat-based applications the component was developed by Mohammed Bahageel artificial Intelligence developer.

Features

  • Text Input: Users can type text messages and send them.
  • Audio Recording: Built-in microphone functionality to capture audio messages.
  • Fixed Positioning: The widget remains fixed at the bottom of the Streamlit app, similar to Streamlit's st.chat_input.

Installation

Install the package directly from PyPI:

pip install streamlit-chat-widget

Setup

Step 1: Import and Initialize the Component

To use the component, import it into your Streamlit app. The widget will display at the bottom of the screen by default.

Step 2: Set Up the Frontend Component

If you want to modify or rebuild the React component, you can find the source code in the frontend directory.

  1. Navigate to the frontend folder:
    cd streamlit_chat_widget/frontend
    
  2. Install dependencies:
    npm install
    
  3. Build the frontend:
    npm run build
    
  4. Run the component locally (for development):
    npm start
    

Step 3: Publish the Package

With reference to: https://github.com/linancn/Chat_input_widget/

pip install twine setuptools
python setup.py sdist bdist_wheel
twine upload dist/*

Usage

Here’s a sample code to get you started with streamlit_chat_widget:

import streamlit as st
from streamlit_chat_widget import chat_input_widget

def main():
    st.title("My Custom Chat Application")
    
    # Initialize chat history in Streamlit session state
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = []

    # Display the chat history
    for message in st.session_state.chat_history:
        st.write(message)

    # Display the chat input widget at the bottom
    user_input = chat_input_widget()

    # Process the user's input from the widget
     if user_input:
        if "text" in user_input:
            user_text =user_input["text"]
            st.session_state.chat_history.append(f"You: {user_text}")
        elif "audioFile" in user_input:
            audio_bytes = bytes(user_input["audioFile"])
            st.audio(audio_bytes)

if __name__ == "__main__":
    main()

Example

To create a simple app with the chat widget:

import streamlit as st
from streamlit_chat_widget import chat_input_widget
from streamlit_extras.bottom_container import bottom #pip install streamlit_extras

def main():
    st.title("My Custom Chat Application")
    
    # Initialize chat history in Streamlit session state
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = []

    # Display the chat history
    for message in st.session_state.chat_history:
        st.write(message)

    # Display the chat input widget at the bottom
    with bottom():
        user_input = chat_input_widget()

    # Process the user's input from the widget
    if user_input:
        if "text" in user_input:
            user_text =user_input["text"]
            st.session_state.chat_history.append(f"You: {user_text}")
        elif "audioFile" in user_input:
            audio_bytes = bytes(user_input["audioFile"])
            st.audio(audio_bytes)

if __name__ == "__main__":
    main()

CSS Customization

To style the widget or customize its appearance, add custom CSS in your Streamlit app. Here’s a basic example:

st.markdown("""
<style>
.chat-container {
    position: fixed;
    bottom: 0;
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}
</style>
""", unsafe_allow_html=True)
 

Additional Customization:

To postion the chat input widget in a fixed postion at the bottom of streamlit application use streamlit floating containers

import streamlit as st
from streamlit_float import *
from streamlit_chat_widget import chat_input_widget

def app():  
    float_init()
    footer_container = st.container()
    with footer_container:
        user_input = chat_input_widget()
    footer_container.float(
        "display:flex; align-items:center;justify-content:center; overflow:hidden visible;flex-direction:column; position:fixed;bottom:15px;"
    )
    
if __name__ == "__main__":
    app()

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_chat_widget_franklu-0.6.0.tar.gz (795.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_chat_widget_franklu-0.6.0-py3-none-any.whl (2.3 MB view details)

Uploaded Python 3

File details

Details for the file streamlit_chat_widget_franklu-0.6.0.tar.gz.

File metadata

File hashes

Hashes for streamlit_chat_widget_franklu-0.6.0.tar.gz
Algorithm Hash digest
SHA256 674701d0b58677251f0e04af347a3c92ed928681f99680951bfdcb7e4d7968c2
MD5 aeae1cb2863ba267551af1bea9a27c07
BLAKE2b-256 5229b610bde4cb7aeb64fd9e03cee2e8f4200347088066ff81642b8e1102b6ca

See more details on using hashes here.

File details

Details for the file streamlit_chat_widget_franklu-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_chat_widget_franklu-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58b095a12ea5bc030aef2ef7cc6d778d14882f10ea7df656a899ed13e8ba1c7f
MD5 04735a61c5c5dce10be04a6214205156
BLAKE2b-256 a012e094dda3998ec3259a2bd9e7c05665f52cb53f27fd60bd1d9ed6d83dae6d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page