Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
from groq import Groq | |
# Set up the Groq API client | |
apikey = os.getenv("apikey") # Fetch the API key from secrets | |
if not apikey: | |
raise ValueError("API Key is not set. Add it in the Secrets tab.") | |
client = Groq(api_key=apikey) | |
# Function to interact with the LLM | |
def chatbot(user_input, history): | |
try: | |
# Format conversation history | |
messages = [{"role": "user", "content": user} for user, _ in history] | |
messages.append({"role": "user", "content": user_input}) | |
# Send to Groq's API | |
chat_completion = client.chat.completions.create( | |
messages=messages, model="llama3-8b-8192" | |
) | |
response = chat_completion.choices[0].message.content | |
history.append((user_input, response)) | |
return history, history | |
except Exception as e: | |
return history, history + [(user_input, f"An error occurred: {str(e)}")] | |
# Enhanced Gradio interface | |
with gr.Blocks(title="Groq LLM Chatbot") as demo: | |
gr.Markdown( | |
""" | |
# 🧠 **Groq LLM Chatbot** | |
Interact with a powerful LLM in real-time using Groq's | |