Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from src.chimera_core import Chimera | |
| import asyncio | |
| import sys | |
| import warnings | |
| # Suppress the specific asyncio warning for Python 3.13 | |
| if sys.version_info >= (3, 13): | |
| warnings.filterwarnings('ignore', category=RuntimeWarning, module='asyncio') | |
| # Initialize | |
| try: | |
| chimera = Chimera() | |
| except Exception as e: | |
| chimera = None | |
| print(f"Startup Error: {e}") | |
| def chat_logic(message, image_file, history, mode): | |
| # Ensure history is a list | |
| if history is None: | |
| history = [] | |
| # 1. Handle Empty Input | |
| if not message and not image_file: | |
| return history, "", None | |
| if not chimera: | |
| user_msg = message or "[Image uploaded]" | |
| history.append({"role": "user", "content": user_msg}) | |
| history.append({"role": "assistant", "content": "β Error: API Keys missing."}) | |
| return history, "", None | |
| # 2. Convert history to simple format for chimera | |
| simple_history = [] | |
| for msg in history: | |
| if isinstance(msg, dict): | |
| simple_history.append([msg.get("content", "")]) | |
| else: | |
| simple_history.append(msg) | |
| # 3. Process Request | |
| try: | |
| response_text, active_module = chimera.process_request( | |
| message or "", | |
| simple_history, | |
| mode, | |
| image_file | |
| ) | |
| except Exception as e: | |
| response_text = f"Processing Error: {str(e)}" | |
| active_module = "ERR" | |
| # 4. Format Output | |
| final_response = f"**[{active_module} Active]**\n\n{response_text}" | |
| # 5. Create user message | |
| if image_file: | |
| user_msg = f"[Uploaded Image] {message or 'Analyze this image'}" | |
| else: | |
| user_msg = message | |
| # 6. Append to History (Dictionary Format for Gradio 6.x) | |
| history.append({"role": "user", "content": user_msg}) | |
| history.append({"role": "assistant", "content": final_response}) | |
| return history, "", None | |
| # --- UI Layout --- | |
| custom_css = """ | |
| body { | |
| background-color: #0b0f19; | |
| color: #c9d1d9; | |
| } | |
| .gradio-container { | |
| font-family: 'IBM Plex Mono', monospace; | |
| } | |
| """ | |
| with gr.Blocks(title="Axon God Mode") as demo: | |
| gr.Markdown("# β‘ AXON: GOD MODE") | |
| gr.Markdown("*> Modules: VIM (Vision) | NET (Web) | IGM (Art) | ASM (Code)*") | |
| with gr.Row(): | |
| chatbot = gr.Chatbot(height=500, elem_id="chatbot") | |
| with gr.Row(): | |
| with gr.Column(scale=4): | |
| msg = gr.Textbox(placeholder="Ask anything, or upload an image...", show_label=False) | |
| btn_upload = gr.Image(type="filepath", label="Upload for Vision (VIM)", height=70) | |
| with gr.Column(scale=1): | |
| mode = gr.Dropdown( | |
| choices=["Auto", "ASM (Code)", "IGM (Generate Image)", "NET (Search)", "VIM (Vision)"], | |
| value="Auto", | |
| show_label=False | |
| ) | |
| submit = gr.Button("π EXECUTE", variant="primary") | |
| # Event Handlers | |
| submit.click( | |
| chat_logic, | |
| inputs=[msg, btn_upload, chatbot, mode], | |
| outputs=[chatbot, msg, btn_upload] | |
| ) | |
| msg.submit( | |
| chat_logic, | |
| inputs=[msg, btn_upload, chatbot, mode], | |
| outputs=[chatbot, msg, btn_upload] | |
| ) | |
| if __name__ == "__main__": | |
| try: | |
| demo.launch(ssr_mode=False, css=custom_css) | |
| finally: | |
| # Clean up event loop on exit | |
| try: | |
| loop = asyncio.get_event_loop() | |
| if loop.is_running(): | |
| loop.stop() | |
| if not loop.is_closed(): | |
| loop.close() | |
| except Exception: | |
| pass |