Update app.py
Browse files
app.py
CHANGED
@@ -273,9 +273,9 @@ def handle_test_example(example_text, history):
|
|
273 |
# Initialize default API key if available
|
274 |
default_initialized, default_status = initialize_default_api_if_available()
|
275 |
|
276 |
-
#
|
277 |
with gr.Blocks(
|
278 |
-
title="
|
279 |
css="""
|
280 |
.gradio-container {
|
281 |
max-width: 100% !important;
|
@@ -326,6 +326,26 @@ with gr.Blocks(
|
|
326 |
margin: 5px 0;
|
327 |
background: rgba(0,0,0,0.1);
|
328 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
"""
|
330 |
) as demo:
|
331 |
gr.Markdown("# π Enhanced MCP Agent Client")
|
@@ -338,11 +358,9 @@ with gr.Blocks(
|
|
338 |
|
339 |
# API Key Section - conditional visibility and messaging
|
340 |
if default_initialized:
|
341 |
-
# Default key available - show optional upgrade section
|
342 |
-
with gr.
|
343 |
gr.Markdown("""
|
344 |
-
## π Upgrade to Personal API Key (Optional)
|
345 |
-
|
346 |
**You're currently using a limited default API key. For unlimited access:**
|
347 |
|
348 |
1. π Go to [Google AI Studio](https://aistudio.google.com/app/apikey)
|
@@ -351,7 +369,7 @@ with gr.Blocks(
|
|
351 |
4. π Paste it below and click "Upgrade to Personal Key"
|
352 |
|
353 |
*Your personal API key will give you unlimited access and faster responses.*
|
354 |
-
"""
|
355 |
|
356 |
with gr.Row():
|
357 |
api_key_input = gr.Textbox(
|
@@ -362,12 +380,12 @@ with gr.Blocks(
|
|
362 |
scale=4
|
363 |
)
|
364 |
api_key_submit = gr.Button("π Upgrade to Personal Key", variant="primary", scale=1)
|
|
|
|
|
365 |
else:
|
366 |
-
# No default key - show required section
|
367 |
-
with gr.
|
368 |
gr.Markdown("""
|
369 |
-
## π Setup Required: Gemini API Key
|
370 |
-
|
371 |
**To use this application, you need a free Gemini API key:**
|
372 |
|
373 |
1. π Go to [Google AI Studio](https://aistudio.google.com/app/apikey)
|
@@ -376,7 +394,7 @@ with gr.Blocks(
|
|
376 |
4. π Paste it below and click "Validate & Start"
|
377 |
|
378 |
Your API key is only stored locally in this session and is not saved anywhere.
|
379 |
-
"""
|
380 |
|
381 |
with gr.Row():
|
382 |
api_key_input = gr.Textbox(
|
@@ -387,8 +405,8 @@ with gr.Blocks(
|
|
387 |
scale=4
|
388 |
)
|
389 |
api_key_submit = gr.Button("π Validate & Start", variant="primary", scale=1)
|
390 |
-
|
391 |
-
|
392 |
|
393 |
# Main Chat Interface - visible if default key works, hidden otherwise
|
394 |
with gr.Group(visible=default_initialized) as chat_interface:
|
@@ -526,18 +544,39 @@ with gr.Blocks(
|
|
526 |
error_msg = [{"role": "assistant", "content": f"Test failed: {str(e)}"}]
|
527 |
return history + error_msg, None, {"error": str(e)}, "", get_api_key_status_info()
|
528 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
529 |
# API Key submission
|
530 |
api_key_submit.click(
|
531 |
fn=handle_api_key_submit,
|
532 |
inputs=[api_key_input],
|
533 |
-
outputs=[api_key_status,
|
534 |
)
|
535 |
|
536 |
# Allow Enter key in API key input
|
537 |
api_key_input.submit(
|
538 |
fn=handle_api_key_submit,
|
539 |
inputs=[api_key_input],
|
540 |
-
outputs=[api_key_status,
|
541 |
)
|
542 |
|
543 |
# Main chat handlers (only work when API key is set)
|
|
|
273 |
# Initialize default API key if available
|
274 |
default_initialized, default_status = initialize_default_api_if_available()
|
275 |
|
276 |
+
# Gradio interface with API key input
|
277 |
with gr.Blocks(
|
278 |
+
title="MCP Agent Client",
|
279 |
css="""
|
280 |
.gradio-container {
|
281 |
max-width: 100% !important;
|
|
|
326 |
margin: 5px 0;
|
327 |
background: rgba(0,0,0,0.1);
|
328 |
}
|
329 |
+
.accordion-header {
|
330 |
+
cursor: pointer;
|
331 |
+
padding: 15px;
|
332 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
333 |
+
border-radius: 10px;
|
334 |
+
margin: 10px 0;
|
335 |
+
color: white;
|
336 |
+
display: flex;
|
337 |
+
justify-content: space-between;
|
338 |
+
align-items: center;
|
339 |
+
}
|
340 |
+
.accordion-header-optional {
|
341 |
+
background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
|
342 |
+
}
|
343 |
+
.accordion-content {
|
344 |
+
background: rgba(255,255,255,0.05);
|
345 |
+
border-radius: 0 0 10px 10px;
|
346 |
+
padding: 20px;
|
347 |
+
margin-top: -10px;
|
348 |
+
}
|
349 |
"""
|
350 |
) as demo:
|
351 |
gr.Markdown("# π Enhanced MCP Agent Client")
|
|
|
358 |
|
359 |
# API Key Section - conditional visibility and messaging
|
360 |
if default_initialized:
|
361 |
+
# Default key available - show optional collapsible upgrade section
|
362 |
+
with gr.Accordion("π Upgrade to Personal API Key (Optional)", open=False) as api_accordion:
|
363 |
gr.Markdown("""
|
|
|
|
|
364 |
**You're currently using a limited default API key. For unlimited access:**
|
365 |
|
366 |
1. π Go to [Google AI Studio](https://aistudio.google.com/app/apikey)
|
|
|
369 |
4. π Paste it below and click "Upgrade to Personal Key"
|
370 |
|
371 |
*Your personal API key will give you unlimited access and faster responses.*
|
372 |
+
""")
|
373 |
|
374 |
with gr.Row():
|
375 |
api_key_input = gr.Textbox(
|
|
|
380 |
scale=4
|
381 |
)
|
382 |
api_key_submit = gr.Button("π Upgrade to Personal Key", variant="primary", scale=1)
|
383 |
+
|
384 |
+
api_key_status = gr.Markdown("", visible=True)
|
385 |
else:
|
386 |
+
# No default key - show required collapsible section (open by default)
|
387 |
+
with gr.Accordion("π Setup Required: Gemini API Key", open=True) as api_accordion:
|
388 |
gr.Markdown("""
|
|
|
|
|
389 |
**To use this application, you need a free Gemini API key:**
|
390 |
|
391 |
1. π Go to [Google AI Studio](https://aistudio.google.com/app/apikey)
|
|
|
394 |
4. π Paste it below and click "Validate & Start"
|
395 |
|
396 |
Your API key is only stored locally in this session and is not saved anywhere.
|
397 |
+
""")
|
398 |
|
399 |
with gr.Row():
|
400 |
api_key_input = gr.Textbox(
|
|
|
405 |
scale=4
|
406 |
)
|
407 |
api_key_submit = gr.Button("π Validate & Start", variant="primary", scale=1)
|
408 |
+
|
409 |
+
api_key_status = gr.Markdown("", visible=True)
|
410 |
|
411 |
# Main Chat Interface - visible if default key works, hidden otherwise
|
412 |
with gr.Group(visible=default_initialized) as chat_interface:
|
|
|
544 |
error_msg = [{"role": "assistant", "content": f"Test failed: {str(e)}"}]
|
545 |
return history + error_msg, None, {"error": str(e)}, "", get_api_key_status_info()
|
546 |
|
547 |
+
def handle_api_key_submit(api_key):
|
548 |
+
"""Handle API key submission."""
|
549 |
+
status_msg, is_valid = validate_and_set_api_key(api_key, is_user_provided=True)
|
550 |
+
|
551 |
+
if is_valid:
|
552 |
+
return (
|
553 |
+
status_msg,
|
554 |
+
gr.update(open=False), # Close accordion after successful setup
|
555 |
+
gr.update(visible=True), # Show chat interface
|
556 |
+
get_api_key_status_info(), # Update status
|
557 |
+
"" # Clear API key input for security
|
558 |
+
)
|
559 |
+
else:
|
560 |
+
return (
|
561 |
+
status_msg,
|
562 |
+
gr.update(open=True), # Keep accordion open on error
|
563 |
+
gr.update(visible=_api_key_set), # Show chat if default key works
|
564 |
+
get_api_key_status_info(), # Update status
|
565 |
+
api_key # Keep the input value for correction
|
566 |
+
)
|
567 |
+
|
568 |
# API Key submission
|
569 |
api_key_submit.click(
|
570 |
fn=handle_api_key_submit,
|
571 |
inputs=[api_key_input],
|
572 |
+
outputs=[api_key_status, api_accordion, chat_interface, api_status_display, api_key_input]
|
573 |
)
|
574 |
|
575 |
# Allow Enter key in API key input
|
576 |
api_key_input.submit(
|
577 |
fn=handle_api_key_submit,
|
578 |
inputs=[api_key_input],
|
579 |
+
outputs=[api_key_status, api_accordion, chat_interface, api_status_display, api_key_input]
|
580 |
)
|
581 |
|
582 |
# Main chat handlers (only work when API key is set)
|