amkj84's picture
Update app.py
9dea61e verified
raw
history blame
2.39 kB
import os
import gradio as gr
from groq import Groq
# Set up Groq API client (ensure GROQ_API_KEY is set in your environment or as a Hugging Face secret for deployment)
apikey = os.getenv("apikey")
print(f"API Key: {apikey}") # Debugging line, remove it before pushing to Hugging Face
client = Groq(api_key=apikey)
# Function to interact with the LLM using Groq's API
def chatbot(messages):
try:
if not messages:
messages = []
user_input = messages[-1][0] if messages else ""
# Sending request to Groq API
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": user_input}],
model="llama3-8b-8192", # Replace with the correct model name
)
response = chat_completion.choices[0].message.content
messages.append((user_input, response)) # Append user input and bot response as a tuple
return messages
except Exception as e:
# Capture the specific error message for debugging
print(f"Error Details: {str(e)}") # Print error details in the console (logs)
messages.append((None, f"An error occurred: {str(e)}")) # Show the error in chat as a tuple
return messages
# Function to reset the chat
def reset_chat():
return []
# Gradio interface with a chatbot component
with gr.Blocks(theme=gr.themes.Soft()) as iface:
gr.Markdown(
"""
# Real-Time Text-to-Text Chatbot
**by ATIF MEHMOOD**
Chat with an advanced language model using Groq's API. Enjoy real-time interactions!
"""
)
with gr.Row():
chatbot_ui = gr.Chatbot(label="Chat Interface", height=400)
with gr.Row():
user_input = gr.Textbox(
label="Type Your Message",
placeholder="Ask me something...",
lines=1,
interactive=True,
)
send_button = gr.Button("Send", variant="primary")
clear_button = gr.Button("Clear Chat")
with gr.Row():
gr.Examples(
examples=["Hello!", "Tell me a joke.", "What's the weather like?"],
inputs=user_input,
)
# Link components to functions
send_button.click(chatbot, inputs=[chatbot_ui], outputs=chatbot_ui)
clear_button.click(reset_chat, inputs=[], outputs=chatbot_ui)
# Launch the Gradio app
iface.launch()