Spaces:
Sleeping
Sleeping
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() | |