File size: 1,578 Bytes
6bd0088
73b3250
b67aee1
a1c1286
e867839
ced4a2f
 
e867839
ced4a2f
a1c1286
ced4a2f
 
 
 
 
73b3250
 
 
e867839
a1c1286
73b3250
ced4a2f
a1c1286
e867839
ced4a2f
 
73b3250
 
ced4a2f
73b3250
ced4a2f
73b3250
 
 
 
 
 
 
 
ced4a2f
73b3250
a1c1286
73b3250
6bd0088
 
86eac15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()