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="The Ultimate Chat Companion") as demo: | |
gr.Markdown( | |
""" | |
# 🧠 **Groq LLM Chatbot** | |
Meet the Future - Interact with a powerful LLM in real-time. | |
""" | |
) | |
with gr.Row(): | |
# Use a local image file for the logo | |
gr.Image(value="chat-companion-high-resolution-logo.png", label="Logo", interactive=False) | |
gr.Markdown( | |
""" | |
**Created by ATIF MEHMOOD** | |
_Experience the next generation of AI-powered conversations._ | |
""" | |
) | |
chatbot_ui = gr.Chatbot(label="Chat Interface") | |
user_input = gr.Textbox( | |
placeholder="Type your message here...", | |
label="Your Message", | |
lines=2, | |
) | |
submit_btn = gr.Button(value="Send") | |
# Bind functions | |
history_state = gr.State([]) | |
submit_btn.click(chatbot, [user_input, history_state], [chatbot_ui, history_state]) | |
user_input.submit(chatbot, [user_input, history_state], [chatbot_ui, history_state]) | |
demo.launch() | |