Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,45 @@
|
|
| 1 |
import os
|
| 2 |
-
from groq import Groq
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Debug: show environment variable
|
| 6 |
key = os.getenv("GROQ_API_KEY")
|
| 7 |
-
print("DEBUG → Loaded
|
| 8 |
|
| 9 |
client = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
if key and key.strip():
|
| 12 |
try:
|
|
|
|
| 13 |
client = Groq(api_key=key.strip())
|
| 14 |
-
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
-
|
|
|
|
| 17 |
else:
|
| 18 |
status = "GROQ_API_KEY not found!"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def chat(message, history):
|
| 21 |
if client is None:
|
| 22 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
response = client.chat.completions.create(
|
| 25 |
model="llama-3.1-70b-versatile",
|
|
@@ -28,12 +48,19 @@ def chat(message, history):
|
|
| 28 |
max_tokens=500
|
| 29 |
)
|
| 30 |
return response.choices[0].message.content
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
with gr.Blocks(title="Groq API Test") as demo:
|
| 35 |
-
gr.Markdown("# Groq API Key
|
| 36 |
-
gr.Markdown(status)
|
| 37 |
gr.ChatInterface(chat)
|
| 38 |
|
| 39 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# ============================================================
|
| 6 |
+
# DEBUG: Check Groq API key
|
| 7 |
+
# ============================================================
|
| 8 |
|
|
|
|
| 9 |
key = os.getenv("GROQ_API_KEY")
|
| 10 |
+
print("DEBUG → Loaded GROQ_API_KEY:", repr(key))
|
| 11 |
|
| 12 |
client = None
|
| 13 |
+
status = ""
|
| 14 |
+
|
| 15 |
+
# ============================================================
|
| 16 |
+
# Initialize Groq Client
|
| 17 |
+
# ============================================================
|
| 18 |
|
| 19 |
if key and key.strip():
|
| 20 |
try:
|
| 21 |
+
print("DEBUG → Initializing Groq client...")
|
| 22 |
client = Groq(api_key=key.strip())
|
| 23 |
+
print("DEBUG → Groq client initialized successfully!")
|
| 24 |
+
status = "Groq API key detected and client initialized!"
|
| 25 |
except Exception as e:
|
| 26 |
+
print("DEBUG → Groq initialization ERROR:", str(e))
|
| 27 |
+
status = f"Groq key found but invalid or Groq initialization failed: {e}"
|
| 28 |
else:
|
| 29 |
status = "GROQ_API_KEY not found!"
|
| 30 |
|
| 31 |
+
|
| 32 |
+
# ============================================================
|
| 33 |
+
# Chat Function
|
| 34 |
+
# ============================================================
|
| 35 |
+
|
| 36 |
def chat(message, history):
|
| 37 |
if client is None:
|
| 38 |
+
return (
|
| 39 |
+
"Fix your GROQ_API_KEY in Secrets first!\n\n"
|
| 40 |
+
"DEBUG INFO: Groq client is None (initialization failed)."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
try:
|
| 44 |
response = client.chat.completions.create(
|
| 45 |
model="llama-3.1-70b-versatile",
|
|
|
|
| 48 |
max_tokens=500
|
| 49 |
)
|
| 50 |
return response.choices[0].message.content
|
| 51 |
+
|
| 52 |
except Exception as e:
|
| 53 |
+
print("DEBUG → Chat API error:", str(e))
|
| 54 |
+
return f"Groq API Error: {e}"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# ============================================================
|
| 58 |
+
# Gradio Interface
|
| 59 |
+
# ============================================================
|
| 60 |
|
| 61 |
with gr.Blocks(title="Groq API Test") as demo:
|
| 62 |
+
gr.Markdown("# 🔑 Groq API Key Debug Tester")
|
| 63 |
+
gr.Markdown(f"**Status:** {status}")
|
| 64 |
gr.ChatInterface(chat)
|
| 65 |
|
| 66 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|