Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,39 +2,42 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
import traceback
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Check if API key is loaded
|
| 11 |
-
api_key = os.getenv("GROQ_API_KEY")
|
| 12 |
if not api_key:
|
| 13 |
-
|
| 14 |
|
| 15 |
client = Groq(api_key=api_key)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
def chat_inference(message, history, example_prompt):
|
| 19 |
try:
|
| 20 |
-
|
|
|
|
| 21 |
response = client.chat.completions.create(
|
| 22 |
messages=[{"role": "user", "content": message}],
|
| 23 |
model="compound-beta"
|
| 24 |
)
|
|
|
|
| 25 |
reply = response.choices[0].message.content
|
| 26 |
-
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
return reply
|
| 32 |
|
| 33 |
-
# Optional input
|
| 34 |
additional_inputs = [
|
| 35 |
gr.Textbox(label="🔍 Example Prompt", value="What were the main highlights from the latest Apple keynote?")
|
| 36 |
]
|
| 37 |
|
|
|
|
| 38 |
chat_interface = gr.ChatInterface(
|
| 39 |
fn=chat_inference,
|
| 40 |
additional_inputs=additional_inputs,
|
|
@@ -42,7 +45,7 @@ chat_interface = gr.ChatInterface(
|
|
| 42 |
title="🔍 AI-Powered Real-Time Search with Groq",
|
| 43 |
description="Ask anything that requires real-time info — powered by Groq’s `compound-beta` model.",
|
| 44 |
theme="default",
|
| 45 |
-
type="messages",
|
| 46 |
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
|
|
|
| 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,
|
|
|
|
| 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 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|