"""
If you are running this code, replace API_KEY in config.json with actual api key obtained from https://console.groq.com/keys
Also, please grab the public URL and then save it into config.json and keep the program runnig. 
"""

import gradio as gr
from groq import Groq

api_key = config['API_KEY']
if api_key == "your-api-key-here":
    print("Please replac API_KEY in config.json with actual api key obtained from https://console.groq.com/keys")
else:
    client = Groq(api_key=api_key)

    messages = [
        {"role": "system", "content": "Act as though you are Bart Simpson"}
    ]
    print("!!!!After the launch is done, please grab the public URL and then save it into config.json and keeo the program running")

    def respond(message, chat_history):
        messages.append({"role": "user", "content": message})
        chat_completion = client.chat.completions.create(
            messages=messages,
            model="llama3-8b-8192",
        )
        bot_message = chat_completion.choices[0].message.content
        messages.append({"role": "assistant", "content": bot_message})
        chat_history.append((message, bot_message))
        return "", chat_history

    with gr.Blocks() as demo:
        chatbot = gr.Chatbot()
        msg = gr.Textbox(placeholder="Type a message and press Enter")
        clear = gr.ClearButton([msg, chatbot])

        msg.submit(respond, [msg, chatbot], [msg, chatbot])

    if __name__ == "__main__":
        demo.launch()