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

refactor: update preview to match space_template.py visual hierarchy

Browse files
Files changed (1) hide show
  1. app.py +75 -83
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
- # 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):
 
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):