Spaces:
Running
Running
milwright
commited on
Commit
·
df4cac5
1
Parent(s):
6ccbfde
refactor: update preview to match space_template.py visual hierarchy
Browse files
app.py
CHANGED
|
@@ -426,121 +426,113 @@ class SpaceGenerator:
|
|
| 426 |
def render_preview(config):
|
| 427 |
if not config or not config.get('preview_ready'):
|
| 428 |
gr.Markdown(
|
|
|
|
| 429 |
"Configure your assistant in the Configuration tab and click 'Preview Configuration' to test it here."
|
| 430 |
)
|
| 431 |
return
|
| 432 |
|
| 433 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
preview_chatbot = gr.Chatbot(
|
| 435 |
-
|
| 436 |
height=400,
|
| 437 |
-
show_copy_button=True
|
| 438 |
-
type='messages'
|
| 439 |
)
|
| 440 |
|
| 441 |
-
#
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
btn = gr.Button(f"{example}", size="sm")
|
| 448 |
-
example_btns.append((btn, example))
|
| 449 |
|
| 450 |
-
#
|
| 451 |
with gr.Row():
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
placeholder="Type your message...",
|
| 455 |
-
lines=1,
|
| 456 |
-
scale=4
|
| 457 |
-
)
|
| 458 |
-
preview_send = gr.Button("Send", variant="primary", scale=1)
|
| 459 |
|
| 460 |
-
# Export functionality
|
| 461 |
with gr.Row():
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
"
|
| 465 |
-
size="sm"
|
| 466 |
-
visible=False
|
| 467 |
)
|
| 468 |
|
| 469 |
-
#
|
| 470 |
-
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
-
#
|
| 474 |
-
def
|
|
|
|
|
|
|
|
|
|
| 475 |
# Simulate response for preview
|
| 476 |
api_key = os.environ.get(config.get('api_key_var', 'API_KEY'))
|
| 477 |
|
| 478 |
if not api_key:
|
| 479 |
response = (
|
| 480 |
-
f"🔑 **API Key Required
|
| 481 |
-
f"
|
| 482 |
-
f"
|
| 483 |
-
f"
|
|
|
|
|
|
|
|
|
|
| 484 |
)
|
| 485 |
else:
|
| 486 |
-
#
|
| 487 |
response = (
|
| 488 |
f"[Preview Mode] This would call {config.get('model', 'Unknown')} "
|
| 489 |
f"with your message: '{message}'"
|
| 490 |
)
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
outputs=[preview_input, preview_chatbot]
|
| 500 |
-
)
|
| 501 |
-
|
| 502 |
-
preview_input.submit(
|
| 503 |
-
preview_respond,
|
| 504 |
-
inputs=[preview_input, preview_chatbot],
|
| 505 |
-
outputs=[preview_input, preview_chatbot]
|
| 506 |
-
)
|
| 507 |
-
|
| 508 |
-
preview_clear.click(
|
| 509 |
-
lambda: ("", []),
|
| 510 |
-
outputs=[preview_input, preview_chatbot]
|
| 511 |
-
)
|
| 512 |
|
| 513 |
-
#
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
lambda ex=example: ex,
|
| 518 |
-
outputs=[preview_input]
|
| 519 |
-
)
|
| 520 |
|
| 521 |
# Export handler
|
| 522 |
-
def prepare_export(
|
| 523 |
-
|
| 524 |
-
return gr.update(visible=False)
|
| 525 |
-
|
| 526 |
-
content = export_conversation_to_markdown(history, config)
|
| 527 |
-
filename = create_safe_filename(
|
| 528 |
-
config.get('name', 'assistant'),
|
| 529 |
-
prefix="preview"
|
| 530 |
-
)
|
| 531 |
-
|
| 532 |
-
temp_file = Path(f"/tmp/{filename}")
|
| 533 |
-
temp_file.write_text(content)
|
| 534 |
-
|
| 535 |
-
return gr.update(
|
| 536 |
-
visible=True,
|
| 537 |
-
value=str(temp_file)
|
| 538 |
-
)
|
| 539 |
|
| 540 |
-
|
| 541 |
prepare_export,
|
| 542 |
-
|
| 543 |
-
outputs=[preview_export]
|
| 544 |
)
|
| 545 |
|
| 546 |
def _create_documentation_tab(self):
|
|
|
|
| 426 |
def render_preview(config):
|
| 427 |
if not config or not config.get('preview_ready'):
|
| 428 |
gr.Markdown(
|
| 429 |
+
"### ⚠️ Preview Not Ready\n\n"
|
| 430 |
"Configure your assistant in the Configuration tab and click 'Preview Configuration' to test it here."
|
| 431 |
)
|
| 432 |
return
|
| 433 |
|
| 434 |
+
# Header
|
| 435 |
+
gr.Markdown(f"# {config.get('name', 'AI Assistant')}")
|
| 436 |
+
if config.get('tagline'):
|
| 437 |
+
gr.Markdown(f"*{config.get('tagline')}*")
|
| 438 |
+
|
| 439 |
+
# Chat interface
|
| 440 |
preview_chatbot = gr.Chatbot(
|
| 441 |
+
type="messages",
|
| 442 |
height=400,
|
| 443 |
+
show_copy_button=True
|
|
|
|
| 444 |
)
|
| 445 |
|
| 446 |
+
# Message input
|
| 447 |
+
msg = gr.Textbox(
|
| 448 |
+
label="Message",
|
| 449 |
+
placeholder="Type your message here...",
|
| 450 |
+
lines=2
|
| 451 |
+
)
|
|
|
|
|
|
|
| 452 |
|
| 453 |
+
# Buttons
|
| 454 |
with gr.Row():
|
| 455 |
+
submit_btn = gr.Button("Send", variant="primary")
|
| 456 |
+
clear_btn = gr.Button("Clear")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
+
# Export functionality
|
| 459 |
with gr.Row():
|
| 460 |
+
export_btn = gr.DownloadButton(
|
| 461 |
+
"📥 Export Conversation",
|
| 462 |
+
variant="secondary",
|
| 463 |
+
size="sm"
|
|
|
|
| 464 |
)
|
| 465 |
|
| 466 |
+
# Examples section
|
| 467 |
+
examples = config.get('examples_list', [])
|
| 468 |
+
if examples:
|
| 469 |
+
gr.Examples(examples=examples, inputs=msg)
|
| 470 |
+
|
| 471 |
+
# File upload accordion (if enabled)
|
| 472 |
+
if config.get('enable_file_upload'):
|
| 473 |
+
with gr.Accordion("📎 Upload Files", open=False):
|
| 474 |
+
file_upload = gr.File(
|
| 475 |
+
label="Upload Files",
|
| 476 |
+
file_types=None,
|
| 477 |
+
file_count="multiple",
|
| 478 |
+
visible=True,
|
| 479 |
+
interactive=True
|
| 480 |
+
)
|
| 481 |
+
uploaded_files_display = gr.Markdown("", visible=False)
|
| 482 |
+
|
| 483 |
+
# Configuration accordion
|
| 484 |
+
with gr.Accordion("ℹ️ Configuration", open=False):
|
| 485 |
+
gr.JSON(
|
| 486 |
+
value=config,
|
| 487 |
+
label="config.json",
|
| 488 |
+
show_label=True
|
| 489 |
+
)
|
| 490 |
|
| 491 |
+
# Chat functionality
|
| 492 |
+
def respond(message, chat_history):
|
| 493 |
+
if not message:
|
| 494 |
+
return chat_history, ""
|
| 495 |
+
|
| 496 |
# Simulate response for preview
|
| 497 |
api_key = os.environ.get(config.get('api_key_var', 'API_KEY'))
|
| 498 |
|
| 499 |
if not api_key:
|
| 500 |
response = (
|
| 501 |
+
f"🔑 **API Key Required**\n\n"
|
| 502 |
+
f"Please configure your OpenRouter API key:\n"
|
| 503 |
+
f"1. Go to Settings (⚙️) in your HuggingFace Space\n"
|
| 504 |
+
f"2. Click 'Variables and secrets'\n"
|
| 505 |
+
f"3. Add secret: **{config.get('api_key_var', 'API_KEY')}**\n"
|
| 506 |
+
f"4. Value: Your OpenRouter API key (starts with `sk-or-`)\n\n"
|
| 507 |
+
f"Get your API key at: https://openrouter.ai/keys"
|
| 508 |
)
|
| 509 |
else:
|
| 510 |
+
# Preview mode response
|
| 511 |
response = (
|
| 512 |
f"[Preview Mode] This would call {config.get('model', 'Unknown')} "
|
| 513 |
f"with your message: '{message}'"
|
| 514 |
)
|
| 515 |
|
| 516 |
+
# Update chat history
|
| 517 |
+
chat_history = chat_history + [
|
| 518 |
+
{"role": "user", "content": message},
|
| 519 |
+
{"role": "assistant", "content": response}
|
| 520 |
+
]
|
| 521 |
+
|
| 522 |
+
return chat_history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
|
| 524 |
+
# Wire up the interface
|
| 525 |
+
msg.submit(respond, [msg, preview_chatbot], [preview_chatbot, msg])
|
| 526 |
+
submit_btn.click(respond, [msg, preview_chatbot], [preview_chatbot, msg])
|
| 527 |
+
clear_btn.click(lambda: ([], ""), outputs=[preview_chatbot, msg])
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
# Export handler
|
| 530 |
+
def prepare_export():
|
| 531 |
+
return None # Simplified for preview
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
+
export_btn.click(
|
| 534 |
prepare_export,
|
| 535 |
+
outputs=[export_btn]
|
|
|
|
| 536 |
)
|
| 537 |
|
| 538 |
def _create_documentation_tab(self):
|