Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from dotenv import load_dotenv | |
| from demo.ui_components import create_landing_page, create_chat_interface | |
| from demo.logic import init_chat, run_agent_step, validate_api_key | |
| from demo.html_templates import get_status_html | |
| load_dotenv() | |
| def start_chat(): | |
| return ( | |
| gr.update(visible=False), | |
| gr.update(visible=True), | |
| ) | |
| def handle_api_validation(api_key): | |
| is_valid, message = validate_api_key(api_key) | |
| status_html = get_status_html(is_valid, message) | |
| return ( | |
| gr.update(value=status_html, visible=True), | |
| gr.update(interactive=is_valid) | |
| ) | |
| def _gradio_respond(message, history, chat_state): | |
| if chat_state is None or len(chat_state) == 0: | |
| chat_state = init_chat() | |
| reply, new_chat_state = run_agent_step(message, chat_state) | |
| return reply, new_chat_state | |
| with gr.Blocks() as demo: | |
| # Створюємо лендінг | |
| landing_page, start_btn = create_landing_page() | |
| # Створюємо інтерфейс чату | |
| chat_interface, chat_state, chat_component, api_status, validate_btn, api_key_input = create_chat_interface( | |
| respond_fn=_gradio_respond, | |
| init_chat_fn=init_chat | |
| ) | |
| # Перехід від лендінгу до чату | |
| start_btn.click( | |
| fn=start_chat, | |
| outputs=[landing_page, chat_interface] | |
| ) | |
| # Валідація API ключа | |
| validate_btn.click( | |
| fn=handle_api_validation, | |
| inputs=[api_key_input], | |
| outputs=[api_status, chat_component.textbox] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |