Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 8 |
-
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"π₯
|
| 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"π€
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
-
|
| 29 |
-
|
| 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 |
-
#
|
| 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
|
| 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 |
)
|