import os import gradio as gr from groq import Groq import traceback import logging # Setup logging logging.basicConfig(filename="error.log", level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s") # Check if API key is loaded api_key = os.getenv("GROQ_API_KEY") if not api_key: logging.error("Missing GROQ_API_KEY environment variable.") client = Groq(api_key=api_key) # Function with error logging def chat_inference(message, history, example_prompt): try: logging.debug(f"User message: {message}") response = client.chat.completions.create( messages=[{"role": "user", "content": message}], model="compound-beta" ) reply = response.choices[0].message.content logging.debug(f"Groq reply: {reply}") except Exception as e: error_details = traceback.format_exc() logging.error(f"Error during Groq API call:\n{error_details}") reply = f"⚠️ Error: Connection error or API issue.\n\nSee logs in `error.log` for more info." return reply # Optional input additional_inputs = [ gr.Textbox(label="🔍 Example Prompt", value="What were the main highlights from the latest Apple keynote?") ] chat_interface = gr.ChatInterface( fn=chat_inference, additional_inputs=additional_inputs, additional_inputs_accordion=gr.Accordion("⚙️ Configuration & Advanced Parameters", open=True), title="🔍 AI-Powered Real-Time Search with Groq", description="Ask anything that requires real-time info — powered by Groq’s `compound-beta` model.", theme="default", type="messages", # Correct format ) if __name__ == "__main__": chat_interface.launch()