File size: 2,096 Bytes
1d3ac34
1a2a2ec
1d3ac34
 
2cc4eba
 
223d9e3
2cc4eba
 
 
 
 
1d3ac34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cc4eba
1d3ac34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a2a2ec
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import gradio as gr
from groq import Groq

# ========== Option 1: Directly set your API key (quick testing) ==========
GROQ_API_KEY = "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"

# ========== Option 2: Read from environment variable ==========
# Uncomment below two lines if you prefer reading from environment
# GROQ_API_KEY = os.getenv("GROQ_API_KEY")
# if not GROQ_API_KEY:
#     raise ValueError("API key is missing. Please set the GROQ_API_KEY environment variable.")

# Initialize the Groq client
client = Groq(api_key=GROQ_API_KEY)

# Define the chatbot function
def chat_with_groq(message, history):
    try:
        # Prepare the conversation history
        messages = [{"role": "system", "content": "You are a helpful assistant."}]
        
        for user, bot in history:
            messages.append({"role": "user", "content": user})
            messages.append({"role": "assistant", "content": bot})
        
        # Add the current user message
        messages.append({"role": "user", "content": message})

        # Call Groq API
        response = client.chat.completions.create(
            model="llama3-70b-8192",  # ✅ Active supported model
            messages=messages,
            temperature=0.7,
            max_tokens=500,
        )

        # Get assistant's reply
        bot_reply = response.choices[0].message.content

        # Update chat history
        history.append((message, bot_reply))
        return history, history

    except Exception as e:
        error_message = f"Error: {str(e)}"
        history.append((message, error_message))
        return history, history

# Build Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("<h1><center>🤖 Chatbot Powered by Groq API</center></h1>")
    chatbot = gr.Chatbot()
    message = gr.Textbox(placeholder="Type your message...", label="Your Message")
    send_button = gr.Button("Send")
    state = gr.State([])

    send_button.click(
        chat_with_groq,
        inputs=[message, state],
        outputs=[chatbot, state],
    )

# Launch the Gradio app
demo.launch()