Skip to main content

A custom chat input widget for Streamlit

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 --U 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
    

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 os
import streamlit as st
from streamlit_chat_widget import chat_input_widget

def app():
    st.title("Doctor AI Assistant")
    
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = [
            "Hello! I am your Doctor AI Assistant. How can I help you?"
        ]

    # Display 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()

    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__":
    app()

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-0.2.0.tar.gz (783.2 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-0.2.0-py3-none-any.whl (2.3 MB view details)

Uploaded Python 3

File details

Details for the file streamlit_chat_widget-0.2.0.tar.gz.

File metadata

  • Download URL: streamlit_chat_widget-0.2.0.tar.gz
  • Upload date:
  • Size: 783.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for streamlit_chat_widget-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7733cbf996cbf0df746f287100b50c3f9149024ace6f8ff6a8cb54d1f1baf56f
MD5 06c41f90545ec68b4ec2e0ab50bd2525
BLAKE2b-256 df82816086d1702b2e12a1d9f579cfb087c05de7275dd90f05083130d296820d

See more details on using hashes here.

File details

Details for the file streamlit_chat_widget-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_chat_widget-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2908ce2c9c63370268dc989784dbecef721f8c33ab992f163bfe0d5ff017c7b
MD5 a0af9ed6f63ace50cf8a2eb399cdd363
BLAKE2b-256 84ef3ee3dcf00b080b7d3a3ad0dd6ccd3f95994f37ca319129af7d95ed60474d

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