milwright commited on
Commit
6ccbfde
·
1 Parent(s): 9289e4a

fix: correct indentation in preview render function to display chat interface

Browse files
Files changed (1) hide show
  1. app.py +110 -117
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
- # Preview info
425
- preview_info = gr.Markdown(
426
- "Configure your assistant in the Configuration tab and click 'Preview Configuration' to test it here.",
427
- visible=True
428
- )
429
-
430
- # Preview interface (hidden initially)
431
- with gr.Column(visible=False) as preview_interface:
432
- gr.Markdown("### Assistant Preview")
433
 
434
- # Use gr.render for dynamic preview based on config
435
- @gr.render(inputs=[self.config_state])
436
- def render_preview(config):
437
- if not config or not config.get('preview_ready'):
438
- return
439
-
440
- # Preview chatbot
441
- preview_chatbot = gr.Chatbot(
442
- label=config.get('name', 'AI Assistant'),
443
- height=400,
444
- show_copy_button=True,
445
- type='messages'
 
 
 
 
 
 
 
 
 
 
 
 
446
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447
 
448
- # Example buttons
449
- examples = config.get('examples_list', [])
450
- if examples:
451
- gr.Markdown("**Try these examples:**")
452
- example_btns = []
453
- for i, example in enumerate(examples[:3]):
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
- preview_send = gr.Button("Send", variant="primary", scale=1)
466
-
467
- # Export functionality
468
- with gr.Row():
469
- preview_clear = gr.Button("🗑️ Clear", size="sm")
470
- preview_export = gr.DownloadButton(
471
- "Export Conversation",
472
- size="sm",
473
- visible=False
474
  )
475
 
476
- # Configuration display
477
- with gr.Accordion("Configuration Details", open=False):
478
- config_display = gr.Markdown(self._format_config_display(config))
479
-
480
- # Event handlers
481
- def preview_respond(message, history):
482
- # Simulate response for preview
483
- api_key = os.environ.get(config.get('api_key_var', 'API_KEY'))
484
-
485
- if not api_key:
486
- response = (
487
- f"🔑 **API Key Required for Preview**\n\n"
488
- f"To test with real responses, set your `{config.get('api_key_var', 'API_KEY')}` "
489
- f"environment variable.\n\n"
490
- f"**Configured Model:** {config.get('model', 'Unknown')}"
491
- )
492
- else:
493
- # Here you would make actual API call
494
- response = (
495
- f"[Preview Mode] This would call {config.get('model', 'Unknown')} "
496
- f"with your message: '{message}'"
497
- )
498
-
499
- history.append({"role": "user", "content": message})
500
- history.append({"role": "assistant", "content": response})
501
- return "", history
502
-
503
- preview_send.click(
504
- preview_respond,
505
- inputs=[preview_input, preview_chatbot],
506
- outputs=[preview_input, preview_chatbot]
507
- )
508
-
509
- preview_input.submit(
510
- preview_respond,
511
- inputs=[preview_input, preview_chatbot],
512
- outputs=[preview_input, preview_chatbot]
513
- )
514
 
515
- preview_clear.click(
516
- lambda: ("", []),
517
- outputs=[preview_input, preview_chatbot]
 
518
  )
519
 
520
- # Example button handlers
521
- if examples:
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
- preview_chatbot.change(
548
- prepare_export,
549
- inputs=[preview_chatbot],
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"""