mychatbot / app.py
NayabShakeel's picture
Update app.py
6d1de12 verified
import gradio as gr
import os
from some_groq_client_library import Groq # Replace with the actual Groq client library
# Set up the API key from environment variables
api_key = os.getenv("GROQ_API_KEY")
# Ensure the API key is provided
if not api_key:
raise ValueError("GROQ_API_KEY environment variable is not set.")
# Initialize the client
client = Groq(api_key=api_key)
# Function to interact with the Groq API
def chat_with_groq(user_input):
try:
# Generate a chat completion
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": user_input}
],
model="llama3-8b-8192", # Replace with a valid model name if needed
)
# Return the response
return chat_completion.choices[0].message.content
except Exception as e:
# Handle errors
return f"An error occurred: {e}"
# Create a Gradio interface
interface = gr.Interface(
fn=chat_with_groq,
inputs=gr.Textbox(label="Enter your message"),
outputs=gr.Textbox(label="Server Response"),
title="Groq Chatbot",
description="Type a message to interact with the Groq API.",
)
# Launch the interface
if __name__ == "__main__":
interface.launch()