Spaces:
Sleeping
Sleeping
# 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}") |