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