Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,26 +7,71 @@ from datetime import datetime
|
|
| 7 |
|
| 8 |
# Groq API Configuration
|
| 9 |
API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 10 |
-
API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# In-memory chat history storage
|
| 16 |
chat_history: List[Dict[str, str]] = []
|
| 17 |
|
| 18 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""Groq API call with chat history and optional summarization, returning chatbot format"""
|
| 20 |
|
| 21 |
-
if
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
if not message.strip():
|
| 25 |
return [[None, "β Empty message"]]
|
| 26 |
|
| 27 |
try:
|
| 28 |
headers = {
|
| 29 |
-
"Authorization": f"Bearer {
|
| 30 |
"Content-Type": "application/json"
|
| 31 |
}
|
| 32 |
|
|
@@ -45,7 +90,6 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 45 |
messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
|
| 46 |
|
| 47 |
if summarize:
|
| 48 |
-
# Create a prompt to summarize the conversation
|
| 49 |
summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
|
| 50 |
for msg in recent_history:
|
| 51 |
if msg["topic"] == topic:
|
|
@@ -60,32 +104,18 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 60 |
"temperature": 0.7
|
| 61 |
}
|
| 62 |
|
| 63 |
-
|
| 64 |
-
print(f"π Payload: {json.dumps(payload, indent=2)}")
|
| 65 |
-
|
| 66 |
-
response = requests.post(
|
| 67 |
-
API_URL,
|
| 68 |
-
headers=headers,
|
| 69 |
-
json=payload,
|
| 70 |
-
timeout=30
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
print(f"π Status Code: {response.status_code}")
|
| 74 |
-
print(f"π Response Headers: {dict(response.headers)}")
|
| 75 |
-
print(f"π Response Text (first 300 chars): {response.text[:300]}")
|
| 76 |
|
| 77 |
if response.status_code == 200:
|
| 78 |
result = response.json()
|
| 79 |
if "choices" in result and len(result["choices"]) > 0:
|
| 80 |
response_content = result["choices"][0]["message"]["content"]
|
| 81 |
-
# Add assistant's response to history
|
| 82 |
chat_history.append({
|
| 83 |
"role": "assistant",
|
| 84 |
"content": response_content,
|
| 85 |
"topic": topic,
|
| 86 |
"timestamp": datetime.now().isoformat()
|
| 87 |
})
|
| 88 |
-
# Return in chatbot format: [[user_message, assistant_response]]
|
| 89 |
return [[message.strip(), response_content]]
|
| 90 |
else:
|
| 91 |
error_msg = f"β No choices in response: {json.dumps(result, indent=2)}"
|
|
@@ -105,7 +135,16 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 105 |
|
| 106 |
def update_chatbot_display() -> List[List[str]]:
|
| 107 |
"""Update the chatbot display with the entire chat history"""
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
def clear_history():
|
| 111 |
"""Clear the chat history"""
|
|
@@ -119,17 +158,19 @@ def view_history(topic: str = None) -> str:
|
|
| 119 |
|
| 120 |
output = "π Chat History:\n\n"
|
| 121 |
for msg in chat_history:
|
| 122 |
-
if topic is None or msg
|
| 123 |
-
output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg
|
| 124 |
return output
|
| 125 |
|
| 126 |
-
def manual_curl_example():
|
| 127 |
"""Generate curl command for manual testing"""
|
| 128 |
-
if
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
|
| 131 |
curl_cmd = f'''curl -X POST "{API_URL}" \\
|
| 132 |
-
-H "Authorization: Bearer {
|
| 133 |
-H "Content-Type: application/json" \\
|
| 134 |
-d '{{
|
| 135 |
"model": "llama3-8b-8192",
|
|
@@ -141,17 +182,62 @@ def manual_curl_example():
|
|
| 141 |
'''
|
| 142 |
return curl_cmd
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# Create Gradio interface
|
| 145 |
-
with gr.Blocks(title="
|
| 146 |
.chatbot-container { max-height: 500px; overflow-y: auto; }
|
| 147 |
.input-container { position: sticky; bottom: 0; background: white; padding: 10px; }
|
| 148 |
-
.chatbot .message.user { text-align: left; background: #e6f3ff; padding: 10px; border-radius: 10px; margin: 5px; }
|
| 149 |
-
.chatbot .message.assistant { text-align: right; background: #f0f0f0; padding: 10px; border-radius: 10px; margin: 5px; }
|
| 150 |
""") as demo:
|
| 151 |
-
gr.Markdown("# π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
with gr.Tab("Chat with Memory"):
|
| 154 |
-
gr.Markdown("Chat with
|
| 155 |
|
| 156 |
chatbot = gr.Chatbot(
|
| 157 |
label="Conversation",
|
|
@@ -166,11 +252,12 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
|
|
| 166 |
placeholder="Enter your message",
|
| 167 |
value="Hello, how can you help me today?",
|
| 168 |
show_label=False,
|
| 169 |
-
container=False
|
|
|
|
| 170 |
)
|
| 171 |
topic_input = gr.Textbox(
|
| 172 |
label="Topic",
|
| 173 |
-
placeholder="
|
| 174 |
value="general",
|
| 175 |
show_label=False,
|
| 176 |
container=False,
|
|
@@ -178,17 +265,28 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
|
|
| 178 |
)
|
| 179 |
summarize_checkbox = gr.Checkbox(
|
| 180 |
label="Summarize",
|
| 181 |
-
value=False
|
|
|
|
| 182 |
)
|
| 183 |
-
chat_button = gr.Button("Send", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
chat_button.click(
|
| 186 |
-
|
| 187 |
-
inputs=[chat_input, topic_input, summarize_checkbox],
|
| 188 |
-
outputs=[chatbot]
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
with gr.Tab("View History"):
|
|
@@ -198,8 +296,10 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
|
|
| 198 |
label="Filter by Topic (optional)",
|
| 199 |
placeholder="Enter topic to filter (leave blank for all)"
|
| 200 |
)
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
| 203 |
history_output = gr.Textbox(
|
| 204 |
label="Chat History",
|
| 205 |
lines=10,
|
|
@@ -211,8 +311,13 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
|
|
| 211 |
inputs=[history_topic],
|
| 212 |
outputs=[history_output]
|
| 213 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
clear_button.click(
|
| 215 |
-
|
| 216 |
outputs=[chatbot, history_output]
|
| 217 |
)
|
| 218 |
|
|
@@ -222,37 +327,62 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
|
|
| 222 |
curl_button = gr.Button("Generate Curl Command")
|
| 223 |
curl_output = gr.Code(
|
| 224 |
label="Curl Command",
|
| 225 |
-
language="
|
| 226 |
)
|
| 227 |
|
| 228 |
curl_button.click(
|
| 229 |
manual_curl_example,
|
|
|
|
| 230 |
outputs=[curl_output]
|
| 231 |
)
|
| 232 |
|
| 233 |
-
with gr.Tab("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
gr.Markdown(f"""
|
| 235 |
-
**
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
-
|
| 251 |
-
-
|
| 252 |
-
|
| 253 |
-
**
|
| 254 |
-
-
|
| 255 |
-
-
|
|
|
|
|
|
|
|
|
|
| 256 |
""")
|
| 257 |
|
| 258 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
# Groq API Configuration
|
| 9 |
API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
|
|
|
| 10 |
|
| 11 |
+
# Try multiple ways to get API key
|
| 12 |
+
API_KEY = None
|
| 13 |
+
api_key_sources = []
|
| 14 |
+
|
| 15 |
+
# Method 1: Environment variable
|
| 16 |
+
env_key = os.getenv("GROQ_API_KEY")
|
| 17 |
+
if env_key:
|
| 18 |
+
API_KEY = env_key
|
| 19 |
+
api_key_sources.append("Environment variable GROQ_API_KEY")
|
| 20 |
+
|
| 21 |
+
# Method 2: Alternative environment variable names
|
| 22 |
+
alt_names = ["GROQ_KEY", "GROQ_API", "GROQ_TOKEN"]
|
| 23 |
+
for name in alt_names:
|
| 24 |
+
alt_key = os.getenv(name)
|
| 25 |
+
if alt_key and not API_KEY:
|
| 26 |
+
API_KEY = alt_key
|
| 27 |
+
api_key_sources.append(f"Environment variable {name}")
|
| 28 |
+
|
| 29 |
+
# Method 3: Check if running in HF Spaces
|
| 30 |
+
hf_token = os.getenv("HF_TOKEN") # HF Spaces sets this automatically
|
| 31 |
+
is_hf_spaces = hf_token is not None
|
| 32 |
|
| 33 |
# In-memory chat history storage
|
| 34 |
chat_history: List[Dict[str, str]] = []
|
| 35 |
|
| 36 |
+
def get_api_key_status():
|
| 37 |
+
"""Get detailed API key status for debugging"""
|
| 38 |
+
status = {
|
| 39 |
+
"found": API_KEY is not None,
|
| 40 |
+
"preview": API_KEY[:10] + "..." if API_KEY else "None",
|
| 41 |
+
"sources": api_key_sources,
|
| 42 |
+
"is_hf_spaces": is_hf_spaces,
|
| 43 |
+
"env_vars": {k: v[:10] + "..." if v and len(v) > 10 else v
|
| 44 |
+
for k, v in os.environ.items()
|
| 45 |
+
if 'GROQ' in k.upper() or 'API' in k.upper()}
|
| 46 |
+
}
|
| 47 |
+
return status
|
| 48 |
+
|
| 49 |
+
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10, user_api_key: str = "") -> List[List[str]]:
|
| 50 |
"""Groq API call with chat history and optional summarization, returning chatbot format"""
|
| 51 |
|
| 52 |
+
# Use user-provided API key if available
|
| 53 |
+
current_api_key = user_api_key.strip() if user_api_key.strip() else API_KEY
|
| 54 |
+
|
| 55 |
+
if not current_api_key:
|
| 56 |
+
status = get_api_key_status()
|
| 57 |
+
error_msg = f"""β No API Key found.
|
| 58 |
+
|
| 59 |
+
Debug Info:
|
| 60 |
+
- Environment check: {status}
|
| 61 |
+
- Running in HF Spaces: {is_hf_spaces}
|
| 62 |
+
|
| 63 |
+
Solutions:
|
| 64 |
+
1. If in HF Spaces: Go to Settings > Secrets and add GROQ_API_KEY
|
| 65 |
+
2. If local: Set environment variable or enter API key in the input field below
|
| 66 |
+
3. Get your key from: https://console.groq.com/keys"""
|
| 67 |
+
return [[message, error_msg]]
|
| 68 |
|
| 69 |
if not message.strip():
|
| 70 |
return [[None, "β Empty message"]]
|
| 71 |
|
| 72 |
try:
|
| 73 |
headers = {
|
| 74 |
+
"Authorization": f"Bearer {current_api_key}",
|
| 75 |
"Content-Type": "application/json"
|
| 76 |
}
|
| 77 |
|
|
|
|
| 90 |
messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
|
| 91 |
|
| 92 |
if summarize:
|
|
|
|
| 93 |
summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
|
| 94 |
for msg in recent_history:
|
| 95 |
if msg["topic"] == topic:
|
|
|
|
| 104 |
"temperature": 0.7
|
| 105 |
}
|
| 106 |
|
| 107 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
if response.status_code == 200:
|
| 110 |
result = response.json()
|
| 111 |
if "choices" in result and len(result["choices"]) > 0:
|
| 112 |
response_content = result["choices"][0]["message"]["content"]
|
|
|
|
| 113 |
chat_history.append({
|
| 114 |
"role": "assistant",
|
| 115 |
"content": response_content,
|
| 116 |
"topic": topic,
|
| 117 |
"timestamp": datetime.now().isoformat()
|
| 118 |
})
|
|
|
|
| 119 |
return [[message.strip(), response_content]]
|
| 120 |
else:
|
| 121 |
error_msg = f"β No choices in response: {json.dumps(result, indent=2)}"
|
|
|
|
| 135 |
|
| 136 |
def update_chatbot_display() -> List[List[str]]:
|
| 137 |
"""Update the chatbot display with the entire chat history"""
|
| 138 |
+
display_history = []
|
| 139 |
+
for msg in chat_history:
|
| 140 |
+
if msg["role"] == "user":
|
| 141 |
+
display_history.append([msg["content"], None])
|
| 142 |
+
else:
|
| 143 |
+
if display_history: # If there's a previous user message
|
| 144 |
+
display_history[-1][1] = msg["content"]
|
| 145 |
+
else: # If assistant message without user message
|
| 146 |
+
display_history.append([None, msg["content"]])
|
| 147 |
+
return display_history
|
| 148 |
|
| 149 |
def clear_history():
|
| 150 |
"""Clear the chat history"""
|
|
|
|
| 158 |
|
| 159 |
output = "π Chat History:\n\n"
|
| 160 |
for msg in chat_history:
|
| 161 |
+
if topic is None or msg.get("topic") == topic:
|
| 162 |
+
output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg.get('topic', 'N/A')}): {msg['content']}\n\n"
|
| 163 |
return output
|
| 164 |
|
| 165 |
+
def manual_curl_example(user_api_key: str = ""):
|
| 166 |
"""Generate curl command for manual testing"""
|
| 167 |
+
current_api_key = user_api_key.strip() if user_api_key.strip() else API_KEY
|
| 168 |
+
|
| 169 |
+
if not current_api_key:
|
| 170 |
+
return "β No API key to generate curl. Set GROQ_API_KEY in environment or enter it below."
|
| 171 |
|
| 172 |
curl_cmd = f'''curl -X POST "{API_URL}" \\
|
| 173 |
+
-H "Authorization: Bearer {current_api_key}" \\
|
| 174 |
-H "Content-Type: application/json" \\
|
| 175 |
-d '{{
|
| 176 |
"model": "llama3-8b-8192",
|
|
|
|
| 182 |
'''
|
| 183 |
return curl_cmd
|
| 184 |
|
| 185 |
+
def test_api_connection(user_api_key: str = ""):
|
| 186 |
+
"""Test API connection"""
|
| 187 |
+
current_api_key = user_api_key.strip() if user_api_key.strip() else API_KEY
|
| 188 |
+
|
| 189 |
+
if not current_api_key:
|
| 190 |
+
return "β No API key provided"
|
| 191 |
+
|
| 192 |
+
try:
|
| 193 |
+
headers = {
|
| 194 |
+
"Authorization": f"Bearer {current_api_key}",
|
| 195 |
+
"Content-Type": "application/json"
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
payload = {
|
| 199 |
+
"model": "llama3-8b-8192",
|
| 200 |
+
"messages": [{"role": "user", "content": "Test connection"}],
|
| 201 |
+
"max_tokens": 10
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
|
| 205 |
+
|
| 206 |
+
if response.status_code == 200:
|
| 207 |
+
return f"β
API connection successful! Status: {response.status_code}"
|
| 208 |
+
else:
|
| 209 |
+
return f"β API error: {response.status_code} - {response.text}"
|
| 210 |
+
|
| 211 |
+
except Exception as e:
|
| 212 |
+
return f"β Connection test failed: {str(e)}"
|
| 213 |
+
|
| 214 |
# Create Gradio interface
|
| 215 |
+
with gr.Blocks(title="Groq Debug with Memory", css="""
|
| 216 |
.chatbot-container { max-height: 500px; overflow-y: auto; }
|
| 217 |
.input-container { position: sticky; bottom: 0; background: white; padding: 10px; }
|
|
|
|
|
|
|
| 218 |
""") as demo:
|
| 219 |
+
gr.Markdown("# π Groq API Debug Tool with Memory")
|
| 220 |
+
|
| 221 |
+
# Global API key input
|
| 222 |
+
with gr.Row():
|
| 223 |
+
global_api_key = gr.Textbox(
|
| 224 |
+
label="API Key (Optional - if not set in environment)",
|
| 225 |
+
placeholder="Enter your Groq API key here...",
|
| 226 |
+
type="password",
|
| 227 |
+
scale=3
|
| 228 |
+
)
|
| 229 |
+
test_button = gr.Button("Test Connection", scale=1)
|
| 230 |
+
|
| 231 |
+
connection_status = gr.Textbox(label="Connection Status", interactive=False)
|
| 232 |
+
|
| 233 |
+
test_button.click(
|
| 234 |
+
test_api_connection,
|
| 235 |
+
inputs=[global_api_key],
|
| 236 |
+
outputs=[connection_status]
|
| 237 |
+
)
|
| 238 |
|
| 239 |
with gr.Tab("Chat with Memory"):
|
| 240 |
+
gr.Markdown("Chat with Groq, maintain history, and summarize by topic")
|
| 241 |
|
| 242 |
chatbot = gr.Chatbot(
|
| 243 |
label="Conversation",
|
|
|
|
| 252 |
placeholder="Enter your message",
|
| 253 |
value="Hello, how can you help me today?",
|
| 254 |
show_label=False,
|
| 255 |
+
container=False,
|
| 256 |
+
scale=3
|
| 257 |
)
|
| 258 |
topic_input = gr.Textbox(
|
| 259 |
label="Topic",
|
| 260 |
+
placeholder="Topic (e.g., 'coding')",
|
| 261 |
value="general",
|
| 262 |
show_label=False,
|
| 263 |
container=False,
|
|
|
|
| 265 |
)
|
| 266 |
summarize_checkbox = gr.Checkbox(
|
| 267 |
label="Summarize",
|
| 268 |
+
value=False,
|
| 269 |
+
scale=1
|
| 270 |
)
|
| 271 |
+
chat_button = gr.Button("Send", variant="primary", scale=1)
|
| 272 |
+
|
| 273 |
+
def chat_and_update(message, topic, summarize, api_key):
|
| 274 |
+
# Get the new chat entry
|
| 275 |
+
new_chat = groq_with_memory(message, topic, summarize, user_api_key=api_key)
|
| 276 |
+
# Return updated chatbot display and clear input
|
| 277 |
+
return update_chatbot_display(), ""
|
| 278 |
|
| 279 |
chat_button.click(
|
| 280 |
+
chat_and_update,
|
| 281 |
+
inputs=[chat_input, topic_input, summarize_checkbox, global_api_key],
|
| 282 |
+
outputs=[chatbot, chat_input]
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
# Enable Enter key
|
| 286 |
+
chat_input.submit(
|
| 287 |
+
chat_and_update,
|
| 288 |
+
inputs=[chat_input, topic_input, summarize_checkbox, global_api_key],
|
| 289 |
+
outputs=[chatbot, chat_input]
|
| 290 |
)
|
| 291 |
|
| 292 |
with gr.Tab("View History"):
|
|
|
|
| 296 |
label="Filter by Topic (optional)",
|
| 297 |
placeholder="Enter topic to filter (leave blank for all)"
|
| 298 |
)
|
| 299 |
+
with gr.Row():
|
| 300 |
+
history_button = gr.Button("View History")
|
| 301 |
+
clear_button = gr.Button("Clear History", variant="secondary")
|
| 302 |
+
|
| 303 |
history_output = gr.Textbox(
|
| 304 |
label="Chat History",
|
| 305 |
lines=10,
|
|
|
|
| 311 |
inputs=[history_topic],
|
| 312 |
outputs=[history_output]
|
| 313 |
)
|
| 314 |
+
|
| 315 |
+
def clear_and_update():
|
| 316 |
+
clear_history()
|
| 317 |
+
return [], "β
Chat history cleared"
|
| 318 |
+
|
| 319 |
clear_button.click(
|
| 320 |
+
clear_and_update,
|
| 321 |
outputs=[chatbot, history_output]
|
| 322 |
)
|
| 323 |
|
|
|
|
| 327 |
curl_button = gr.Button("Generate Curl Command")
|
| 328 |
curl_output = gr.Code(
|
| 329 |
label="Curl Command",
|
| 330 |
+
language="bash"
|
| 331 |
)
|
| 332 |
|
| 333 |
curl_button.click(
|
| 334 |
manual_curl_example,
|
| 335 |
+
inputs=[global_api_key],
|
| 336 |
outputs=[curl_output]
|
| 337 |
)
|
| 338 |
|
| 339 |
+
with gr.Tab("Debug Info"):
|
| 340 |
+
gr.Markdown("**System Information & Troubleshooting**")
|
| 341 |
+
|
| 342 |
+
debug_button = gr.Button("Show Debug Info")
|
| 343 |
+
debug_output = gr.JSON(label="Debug Information")
|
| 344 |
+
|
| 345 |
+
def show_debug_info():
|
| 346 |
+
status = get_api_key_status()
|
| 347 |
+
debug_info = {
|
| 348 |
+
"api_key_status": status,
|
| 349 |
+
"api_url": API_URL,
|
| 350 |
+
"available_env_vars": list(os.environ.keys()),
|
| 351 |
+
"python_version": os.sys.version,
|
| 352 |
+
"current_directory": os.getcwd()
|
| 353 |
+
}
|
| 354 |
+
return debug_info
|
| 355 |
+
|
| 356 |
+
debug_button.click(
|
| 357 |
+
show_debug_info,
|
| 358 |
+
outputs=[debug_output]
|
| 359 |
+
)
|
| 360 |
+
|
| 361 |
gr.Markdown(f"""
|
| 362 |
+
**Setup Instructions:**
|
| 363 |
+
|
| 364 |
+
**For Hugging Face Spaces:**
|
| 365 |
+
1. Go to your Space Settings
|
| 366 |
+
2. Click on "Secrets" tab
|
| 367 |
+
3. Add new secret: `GROQ_API_KEY` = your_api_key_here
|
| 368 |
+
4. Restart the space
|
| 369 |
+
|
| 370 |
+
**For Local Development:**
|
| 371 |
+
```bash
|
| 372 |
+
export GROQ_API_KEY="your_api_key_here"
|
| 373 |
+
python app.py
|
| 374 |
+
```
|
| 375 |
+
|
| 376 |
+
**Get API Key:**
|
| 377 |
+
- Visit: https://console.groq.com/keys
|
| 378 |
+
- Create account and generate API key
|
| 379 |
+
|
| 380 |
+
**Troubleshooting:**
|
| 381 |
+
- β
API Key Status: {'Found' if API_KEY else 'Missing'}
|
| 382 |
+
- β
Running in HF Spaces: {is_hf_spaces}
|
| 383 |
+
- β
API URL: `{API_URL}`
|
| 384 |
+
|
| 385 |
+
**Alternative:** Enter your API key in the field above for temporary testing.
|
| 386 |
""")
|
| 387 |
|
| 388 |
if __name__ == "__main__":
|