import os import io import gradio as gr from pathlib import Path from rich.console import Console # Import your modules from cli_app.command_handler import handle_command import cli_app.command_handler as handler from vector_store.instruction_embedder import embed_analyze_instructions def terminal_logic(command): if not command: return "", None output_buffer = io.StringIO() temp_console = Console(file=output_buffer, force_terminal=True, width=100, color_system=None) old_console = getattr(handler, 'console', None) handler.console = temp_console try: result = handle_command(command) captured = output_buffer.getvalue() combined = f"{captured}\n{str(result) if result else ''}".strip() image_path = None if ".png" in combined: for word in combined.split(): clean = word.strip(".,![]'\"") if clean.endswith(".png") and os.path.exists(clean): image_path = clean break return f"```\n{combined}\n```", image_path except Exception as e: return f"Error: {str(e)}", None finally: handler.console = old_console def run_space_interface(): embed_analyze_instructions() # Auto-hydrate data_dir = Path("data/datasets") if data_dir.exists(): for file in os.listdir(data_dir): if file.endswith((".parquet", ".pkl")) and "embeddings" not in file: try: handler.handle_command(f"load {data_dir / file}") except: continue with gr.Blocks(theme="monochrome", analytics_enabled=False) as demo: gr.Markdown("# 📊 EDA Explorer Terminal") with gr.Row(): # Markdown is significantly safer than gr.Code for preventing schema crashes output_box = gr.Markdown(value="```\nTerminal Ready\n```") plot_output = gr.Image(label="Visual Analysis", type="filepath") input_box = gr.Textbox(label="Command Line") gr.Examples( examples=[["list"], ["NL: show top 10 rows in titanic"], ["NL: average Age in titanic"], ["histogram age in titanic"]], inputs=input_box ) # api_name=False is the crucial fix for the "bool is not iterable" crawler error input_box.submit( fn=terminal_logic, inputs=input_box, outputs=[output_box, plot_output], api_name=False ) demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False) if __name__ == "__main__": run_space_interface()