Shreyas94 commited on
Commit
ced4a2f
Β·
verified Β·
1 Parent(s): a1c1286

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -20
app.py CHANGED
@@ -1,49 +1,43 @@
1
- import os
2
  import gradio as gr
3
  from groq import Groq
4
  import traceback
5
  import sys
6
 
7
- # Get GROQ API Key from environment
8
- api_key = os.environ.get("GROQ_API_KEY")
9
- if not api_key:
10
- print("❌ GROQ_API_KEY is missing.", file=sys.stderr)
11
-
12
- client = Groq(api_key=api_key)
13
-
14
- # Chat function with logging to stderr (for Spaces)
15
- def chat_inference(message, history, example_prompt):
16
  try:
17
- print(f"πŸ“₯ User message: {message}", file=sys.stderr)
18
 
 
 
 
 
 
19
  response = client.chat.completions.create(
20
  messages=[{"role": "user", "content": message}],
21
  model="compound-beta"
22
  )
23
 
24
  reply = response.choices[0].message.content
25
- print(f"πŸ“€ Groq reply: {reply}", file=sys.stderr)
26
 
27
  except Exception as e:
28
- error_trace = traceback.format_exc()
29
- print("❌ Exception occurred:", file=sys.stderr)
30
- print(error_trace, file=sys.stderr)
31
-
32
- reply = "⚠️ Error: Connection error or API issue.\n\nCheck the Logs tab in Hugging Face Spaces for more details."
33
  return reply
34
 
35
- # Optional textbox for additional input
36
  additional_inputs = [
 
37
  gr.Textbox(label="πŸ” Example Prompt", value="What were the main highlights from the latest Apple keynote?")
38
  ]
39
 
40
- # Gradio interface
41
  chat_interface = gr.ChatInterface(
42
  fn=chat_inference,
43
  additional_inputs=additional_inputs,
44
  additional_inputs_accordion=gr.Accordion("βš™οΈ Configuration & Advanced Parameters", open=True),
45
  title="πŸ” AI-Powered Real-Time Search with Groq",
46
- description="Ask anything that requires real-time info β€” powered by Groq’s `compound-beta` model.",
47
  theme="default",
48
  type="messages",
49
  )
 
 
1
  import gradio as gr
2
  from groq import Groq
3
  import traceback
4
  import sys
5
 
6
+ # Chat function accepts API key as user input
7
+ def chat_inference(message, history, api_key, example_prompt):
 
 
 
 
 
 
 
8
  try:
9
+ print(f"πŸ“₯ Message: {message}", file=sys.stderr)
10
 
11
+ # Strip whitespace/newlines
12
+ api_key = api_key.strip()
13
+ client = Groq(api_key=api_key)
14
+
15
+ # Call Groq API
16
  response = client.chat.completions.create(
17
  messages=[{"role": "user", "content": message}],
18
  model="compound-beta"
19
  )
20
 
21
  reply = response.choices[0].message.content
22
+ print(f"πŸ“€ Reply: {reply}", file=sys.stderr)
23
 
24
  except Exception as e:
25
+ print(traceback.format_exc(), file=sys.stderr)
26
+ reply = "⚠️ Error: Connection error or invalid API key. See logs in Spaces tab."
 
 
 
27
  return reply
28
 
29
+ # Inputs: API key + optional example prompt
30
  additional_inputs = [
31
+ gr.Textbox(label="πŸ” Groq API Key", placeholder="Paste your API key here", type="password"),
32
  gr.Textbox(label="πŸ” Example Prompt", value="What were the main highlights from the latest Apple keynote?")
33
  ]
34
 
 
35
  chat_interface = gr.ChatInterface(
36
  fn=chat_inference,
37
  additional_inputs=additional_inputs,
38
  additional_inputs_accordion=gr.Accordion("βš™οΈ Configuration & Advanced Parameters", open=True),
39
  title="πŸ” AI-Powered Real-Time Search with Groq",
40
+ description="Ask questions using Groq’s `compound-beta` model with real-time web access.",
41
  theme="default",
42
  type="messages",
43
  )