File size: 1,178 Bytes
c92006c
 
 
 
27f176a
c92006c
 
 
 
 
d87d8b2
 
c92006c
d87d8b2
 
 
 
 
c92006c
 
 
b7199a7
c92006c
 
b7199a7
c92006c
 
 
 
 
d87d8b2
 
 
 
 
c92006c
d87d8b2
c92006c
 
 
27f176a
 
d87d8b2
312d64f
c92006c
27f176a
d87d8b2
c92006c
 
 
 
 
27f176a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import gradio as gr
from groq import Groq

client = Groq(api_key=os.environ["Groq_api"])

def chatbot(user_input, history):
    if history is None:
        history = []

    # ✅ history is already in correct format now
    messages = history.copy()

    # add current user message
    messages.append({
        "role": "user",
        "content": user_input
    })

    try:
        response = client.chat.completions.create(
            model="llama3-8b-8192",
            messages=messages
        )

        bot_reply = response.choices[0].message.content

    except Exception as e:
        bot_reply = f"Error: {str(e)}"

    # add assistant reply
    messages.append({
        "role": "assistant",
        "content": bot_reply
    })

    return messages, messages


with gr.Blocks() as demo:
    gr.Markdown("## 🤖 Simple Groq Chatbot")

    # ✅ IMPORTANT CHANGE HERE
chatbot_ui = gr.Chatbot()
    msg = gr.Textbox(placeholder="Type your message...")
    state = gr.State([])

    clear = gr.Button("Clear")

    msg.submit(chatbot, [msg, state], [chatbot_ui, state])
    clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)

demo.launch()