import os from groq import Groq import gradio as gr import logging os.environ["GROQ_API_KEY"] = "c5417d24599246a4845e0aefba9a596f" client = Groq(api_key=os.environ["GROQ_API_KEY"]) # Set up logging configuration logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) # Initialize the Groq client with API key from environment variables api_key = os.environ.get("GROQ_API_KEY") if not api_key: logger.error("GROQ_API_KEY environment variable is not set.") raise ValueError("GROQ_API_KEY environment variable is required.") client = Groq(api_key=api_key) # Default model name, can be overridden by environment variable MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192") def get_completion(user_input): """ Generate a chat completion response using the Groq client. Args: user_input (str): The user's input query. Returns: str: The generated response or an error message. """ try: completion = client.chat.completions.create( model=MODEL_NAME, messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": user_input} ], temperature=1, max_tokens=1024, top_p=1, stream=False, # Gradio does not support stream=True ) # Extract the response response = completion.choices[0].message.content.strip() return response except Exception as e: logger.error(f"Error during completion: {e}") return "Sorry, I encountered an error while processing your request." def launch_interface(): """ Launch the Gradio interface for the chatbot. """ demo = gr.Interface( fn=get_completion, inputs=gr.Textbox( label="Enter your query:", placeholder="Ask me anything...", lines=2, max_lines=5 ), outputs=gr.Textbox( label="Response:", lines=6, max_lines=10 ), title="Mr AI", description="""
Welcome to Mr AI
Ask anything and get a helpful response.
""", allow_flagging="never", live=True ) logger.info("Starting Gradio interface") demo.launch(share=True) if __name__ == "__main__": launch_interface()