Chat interface

Gradio is an excellent tool for prototyping chatbots because it provides a simple and intuitive way to create interactive web-based interfaces for machine learning models. With Gradio, you can quickly set up a chat interface to test and demonstrate your chatbot's capabilities. It offers real-time interaction, easy integration with models, and minimal setup, making it ideal for rapid development and testing.

from openai import OpenAI
import gradio as gr

client = OpenAI(api_key='<API_KEY>', base_url='<MODEL_URL>')

if __name__ == '__main__':
    def predict(message, history):
        history_openai_format = []
        for human, assistant in history:
            history_openai_format.append({"role": "user", "content": human})
            history_openai_format.append({"role": "assistant", "content": assistant})
        history_openai_format.append({"role": "user", "content": message})

        response = client.chat.completions.create(model='meta-llama/Meta-Llama-3.1-8B-Instruct',
                                                  messages=history_openai_format,
                                                  stream=True)
        partial_message = ""
        for chunk in response:
            if chunk.choices[0].delta.content is not None:
                partial_message = partial_message + chunk.choices[0].delta.content
                yield partial_message


    gr.ChatInterface(predict).launch()

Run the script and visit http://127.0.0.1:7860 in your browser to interact with the chat interface.

Last updated