Spaces:
Running
Running
milwright
commited on
Commit
·
6ccbfde
1
Parent(s):
9289e4a
fix: correct indentation in preview render function to display chat interface
Browse files
app.py
CHANGED
|
@@ -421,134 +421,127 @@ class SpaceGenerator:
|
|
| 421 |
def _create_preview_tab(self):
|
| 422 |
"""Create the preview tab with modern patterns"""
|
| 423 |
with gr.Column():
|
| 424 |
-
#
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
gr.Markdown("### Assistant Preview")
|
| 433 |
|
| 434 |
-
#
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
btn = gr.Button(f"{example}", size="sm")
|
| 455 |
-
example_btns.append((btn, example))
|
| 456 |
-
|
| 457 |
-
# Chat input
|
| 458 |
-
with gr.Row():
|
| 459 |
-
preview_input = gr.Textbox(
|
| 460 |
-
label="Message",
|
| 461 |
-
placeholder="Type your message...",
|
| 462 |
-
lines=1,
|
| 463 |
-
scale=4
|
| 464 |
)
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
preview_export = gr.DownloadButton(
|
| 471 |
-
"Export Conversation",
|
| 472 |
-
size="sm",
|
| 473 |
-
visible=False
|
| 474 |
)
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
preview_input.submit(
|
| 510 |
-
preview_respond,
|
| 511 |
-
inputs=[preview_input, preview_chatbot],
|
| 512 |
-
outputs=[preview_input, preview_chatbot]
|
| 513 |
-
)
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
|
|
|
| 518 |
)
|
| 519 |
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
for btn, example in example_btns:
|
| 523 |
-
btn.click(
|
| 524 |
-
lambda ex=example: ex,
|
| 525 |
-
outputs=[preview_input]
|
| 526 |
-
)
|
| 527 |
-
|
| 528 |
-
# Export handler
|
| 529 |
-
def prepare_export(history):
|
| 530 |
-
if not history:
|
| 531 |
-
return gr.update(visible=False)
|
| 532 |
-
|
| 533 |
-
content = export_conversation_to_markdown(history, config)
|
| 534 |
-
filename = create_safe_filename(
|
| 535 |
-
config.get('name', 'assistant'),
|
| 536 |
-
prefix="preview"
|
| 537 |
-
)
|
| 538 |
-
|
| 539 |
-
temp_file = Path(f"/tmp/{filename}")
|
| 540 |
-
temp_file.write_text(content)
|
| 541 |
-
|
| 542 |
-
return gr.update(
|
| 543 |
-
visible=True,
|
| 544 |
-
value=str(temp_file)
|
| 545 |
-
)
|
| 546 |
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
outputs=[preview_export]
|
| 551 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
|
| 553 |
def _create_documentation_tab(self):
|
| 554 |
"""Create the documentation tab"""
|
|
|
|
| 421 |
def _create_preview_tab(self):
|
| 422 |
"""Create the preview tab with modern patterns"""
|
| 423 |
with gr.Column():
|
| 424 |
+
# Use gr.render for dynamic preview based on config
|
| 425 |
+
@gr.render(inputs=[self.config_state])
|
| 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 |
+
# Preview chatbot
|
| 434 |
+
preview_chatbot = gr.Chatbot(
|
| 435 |
+
label=config.get('name', 'AI Assistant'),
|
| 436 |
+
height=400,
|
| 437 |
+
show_copy_button=True,
|
| 438 |
+
type='messages'
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
# Example buttons
|
| 442 |
+
examples = config.get('examples_list', [])
|
| 443 |
+
if examples:
|
| 444 |
+
gr.Markdown("**Try these examples:**")
|
| 445 |
+
example_btns = []
|
| 446 |
+
for i, example in enumerate(examples[:3]):
|
| 447 |
+
btn = gr.Button(f"{example}", size="sm")
|
| 448 |
+
example_btns.append((btn, example))
|
| 449 |
+
|
| 450 |
+
# Chat input
|
| 451 |
+
with gr.Row():
|
| 452 |
+
preview_input = gr.Textbox(
|
| 453 |
+
label="Message",
|
| 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 |
+
preview_clear = gr.Button("🗑️ Clear", size="sm")
|
| 463 |
+
preview_export = gr.DownloadButton(
|
| 464 |
+
"Export Conversation",
|
| 465 |
+
size="sm",
|
| 466 |
+
visible=False
|
| 467 |
+
)
|
| 468 |
+
|
| 469 |
+
# Configuration display
|
| 470 |
+
with gr.Accordion("Configuration Details", open=False):
|
| 471 |
+
config_display = gr.Markdown(self._format_config_display(config))
|
| 472 |
+
|
| 473 |
+
# Event handlers
|
| 474 |
+
def preview_respond(message, history):
|
| 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 for Preview**\n\n"
|
| 481 |
+
f"To test with real responses, set your `{config.get('api_key_var', 'API_KEY')}` "
|
| 482 |
+
f"environment variable.\n\n"
|
| 483 |
+
f"**Configured Model:** {config.get('model', 'Unknown')}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
)
|
| 485 |
+
else:
|
| 486 |
+
# Here you would make actual API call
|
| 487 |
+
response = (
|
| 488 |
+
f"[Preview Mode] This would call {config.get('model', 'Unknown')} "
|
| 489 |
+
f"with your message: '{message}'"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
)
|
| 491 |
|
| 492 |
+
history.append({"role": "user", "content": message})
|
| 493 |
+
history.append({"role": "assistant", "content": response})
|
| 494 |
+
return "", history
|
| 495 |
+
|
| 496 |
+
preview_send.click(
|
| 497 |
+
preview_respond,
|
| 498 |
+
inputs=[preview_input, preview_chatbot],
|
| 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 |
+
# Example button handlers
|
| 514 |
+
if examples:
|
| 515 |
+
for btn, example in example_btns:
|
| 516 |
+
btn.click(
|
| 517 |
+
lambda ex=example: ex,
|
| 518 |
+
outputs=[preview_input]
|
| 519 |
+
)
|
| 520 |
+
|
| 521 |
+
# Export handler
|
| 522 |
+
def prepare_export(history):
|
| 523 |
+
if not history:
|
| 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 |
+
preview_chatbot.change(
|
| 541 |
+
prepare_export,
|
| 542 |
+
inputs=[preview_chatbot],
|
| 543 |
+
outputs=[preview_export]
|
| 544 |
+
)
|
| 545 |
|
| 546 |
def _create_documentation_tab(self):
|
| 547 |
"""Create the documentation tab"""
|