File size: 1,351 Bytes
90fdbc9
 
0f10375
90fdbc9
95599b6
0f10375
90fdbc9
95599b6
0f10375
90fdbc9
 
b522828
90fdbc9
 
 
 
 
 
 
b522828
90fdbc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
# Defining the API connection
import os
import gradio as gr
from groq import Groq
#from dotenv import load_dotenv

# Load environment variables
#load_dotenv()

# Debug print
print("API Key:", os.environ.get("GROQ_API_KEY", "Not found"))

try:
    client = Groq(
        api_key=os.environ.get("GROQ_API_KEY"),
    )
except Exception as e:
    print(f"Error initializing Groq client: {e}")
    raise

def chat_with_groq(user_input, additional_info=None):
    try:
        chat_completion = client.chat.completions.create(
            messages=[
                {
                    "role": "user",
                    "content": user_input,
                }
            ],
            model="llama-3.1-8b-instant",
        )
        return chat_completion.choices[0].message.content
    except Exception as e:
        print(f"Error in chat completion: {e}")
        return f"An error occurred: {str(e)}"

# Create Gradio interface
try:
    iface = gr.Interface(
        fn=chat_with_groq,
        inputs=gr.Textbox(label="Your message"),
        outputs=gr.Textbox(label="Response"),
        title="Chat with Groq",
        description="Enter your message and get a response from the Llama model."
    )

    # Launch with specific port and host (optional)
    iface.launch()
except Exception as e:
    print(f"Error launching Gradio interface: {e}")