File size: 1,586 Bytes
a1f8e73
529d9fe
3d0613b
529d9fe
 
 
a1f8e73
529d9fe
 
 
 
 
 
 
 
 
9688ff7
529d9fe
 
9688ff7
529d9fe
a1f8e73
529d9fe
 
 
a1f8e73
529d9fe
9688ff7
529d9fe
 
9688ff7
529d9fe
 
 
 
 
 
 
 
 
 
 
 
a1f8e73
9688ff7
 
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
import gradio as gr
from groq.client import Client  # Adjust this based on Groq's official documentation

# Initialize Groq API client
api_key = "your_groq_api_key"  # Replace with your actual API key
client = Client(api_key=api_key)

# Chatbot logic using Groq API
def chatbot(message, history):
    history = history or []  # Initialize history if None

    # Build a prompt using the chat history
    prompt = "\n".join([f"User: {msg[0]}\nBot: {msg[1]}" for msg in history])
    prompt += f"\nUser: {message}\nBot:"

    # Query the Groq API
    response = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model="llama-3.3-70b-versatile",  # Adjust the model name if necessary
    )
    bot_reply = response.choices[0].message.content.strip()  # Extract the reply

    # Append the conversation to history
    history.append((message, bot_reply))
    return history, history

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("# Gradio Chatbot with Groq API")
    chatbot_ui = gr.Chatbot(label="Chatbot")  # Chatbot display
    with gr.Row():
        user_input = gr.Textbox(placeholder="Type your message here...")  # Input box
        submit_btn = gr.Button("Send")  # Send button

    # Clear history button
    clear_btn = gr.Button("Clear Chat")

    # State management for chat history
    state = gr.State()

    # Event bindings
    submit_btn.click(chatbot, inputs=[user_input, state], outputs=[chatbot_ui, state])
    clear_btn.click(lambda: ([], None), outputs=[chatbot_ui, state])

# Launch the app
demo.launch()