Sentinel02 / app.py
Shreyas94's picture
Update app.py
ced4a2f verified
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()