Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from groq import Groq | |
| import traceback | |
| import sys | |
| # Chat function accepts API key as user input | |
| def chat_inference(message, history, api_key, example_prompt): | |
| try: | |
| print(f"π₯ Message: {message}", file=sys.stderr) | |
| # Strip whitespace/newlines | |
| api_key = api_key.strip() | |
| client = Groq(api_key=api_key) | |
| # Call Groq API | |
| response = client.chat.completions.create( | |
| messages=[{"role": "user", "content": message}], | |
| model="compound-beta" | |
| ) | |
| reply = response.choices[0].message.content | |
| print(f"π€ Reply: {reply}", file=sys.stderr) | |
| except Exception as e: | |
| print(traceback.format_exc(), file=sys.stderr) | |
| reply = "β οΈ Error: Connection error or invalid API key. See logs in Spaces tab." | |
| return reply | |
| # Inputs: API key + optional example prompt | |
| additional_inputs = [ | |
| gr.Textbox(label="π Groq API Key", placeholder="Paste your API key here", type="password"), | |
| 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 questions using Groqβs `compound-beta` model with real-time web access.", | |
| theme="default", | |
| type="messages", | |
| ) | |
| if __name__ == "__main__": | |
| chat_interface.launch() | |