File size: 1,268 Bytes
8bf7251
6d1de12
 
8bf7251
6d1de12
 
8bf7251
6d1de12
 
 
8bf7251
 
6d1de12
8bf7251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d1de12
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()