Spaces:
Running
Running
| | |
| # ./app.py | |
| """ | |
| The Interface Skeleton - The code sets up the navigation panel and the multimodal chat interface | |
| """ | |
| import gradio as gr | |
| from core_logic import chat_function | |
| from storage import save_chat, load_history, get_chat_content | |
| with gr.Blocks() as demo: | |
| chat_id_state = gr.State("") | |
| with gr.Row(): | |
| with gr.Column(scale=1, variant="secondary"): | |
| gr.Markdown("### 🛠️ Silicon Architect") | |
| new_btn = gr.Button("➕ New Chat", variant="primary") | |
| history_list = gr.Dataset( | |
| components=[gr.Textbox(visible=False)], | |
| label="Recent Conversations", | |
| samples=load_history() | |
| ) | |
| with gr.Column(scale=4): | |
| chatbot = gr.Chatbot(show_label=False, height=700) | |
| chat_input = gr.MultimodalTextbox( | |
| interactive=True, | |
| placeholder="Discuss architecture or upload code...", | |
| show_label=False | |
| ) | |
| def bot_response(message, history): | |
| # 1. Add User Message | |
| user_content = message["text"] | |
| history.append({"role": "user", "content": user_content}) | |
| # 2. Add empty Assistant Message to be filled | |
| history.append({"role": "assistant", "content": ""}) | |
| # 3. Stream the response | |
| # history[:-1] sends everything EXCEPT the empty assistant slot we just made | |
| for partial_resp in chat_function(message, history[:-1]): | |
| history[-1]["content"] = partial_resp | |
| yield history | |
| # Event Handlers | |
| chat_input.submit(bot_response, [chat_input, chatbot], [chatbot]).then( | |
| lambda h: save_chat(None, h), [chatbot], None | |
| ) | |
| new_btn.click(lambda: ([], ""), None, [chatbot, chat_id_state]) | |
| demo.launch(theme=gr.themes.Soft(), css="styles.css") |