Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from groq import Groq | |
| import traceback | |
| import sys | |
| # Get GROQ API Key from environment | |
| api_key = os.environ.get("GROQ_API_KEY") | |
| if not api_key: | |
| print("β GROQ_API_KEY is missing.", file=sys.stderr) | |
| client = Groq(api_key=api_key) | |
| # Chat function with logging to stderr (for Spaces) | |
| def chat_inference(message, history, example_prompt): | |
| try: | |
| print(f"π₯ User message: {message}", file=sys.stderr) | |
| response = client.chat.completions.create( | |
| messages=[{"role": "user", "content": message}], | |
| model="compound-beta" | |
| ) | |
| reply = response.choices[0].message.content | |
| print(f"π€ Groq reply: {reply}", file=sys.stderr) | |
| except Exception as e: | |
| error_trace = traceback.format_exc() | |
| print("β Exception occurred:", file=sys.stderr) | |
| print(error_trace, file=sys.stderr) | |
| reply = "β οΈ Error: Connection error or API issue.\n\nCheck the Logs tab in Hugging Face Spaces for more details." | |
| return reply | |
| # Optional textbox for additional input | |
| additional_inputs = [ | |
| gr.Textbox(label="π Example Prompt", value="What were the main highlights from the latest Apple keynote?") | |
| ] | |
| # Gradio interface | |
| 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", | |
| ) | |
| if __name__ == "__main__": | |
| chat_interface.launch() | |