ProfessionalMario commited on
Commit
6fc688e
·
1 Parent(s): f4a7794

command fix

Browse files
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -8,6 +8,7 @@ import os
8
  import gradio as gr
9
  from cli_app.command_handler import handle_command
10
  from vector_store.instruction_embedder import embed_analyze_instructions
 
11
 
12
  console = Console()
13
 
@@ -50,27 +51,50 @@ def run_cli():
50
  console.print(f"Error: {e}")
51
 
52
  def run_space_interface():
53
- """The 'Space Way' using ChatInterface."""
54
-
55
- # Pre-embed instructions just like your run_cli() does
56
  embed_analyze_instructions()
57
 
58
  def chat_response(message, history):
59
- # 'message' is what the user typed in the box
60
- # We pass it to your existing handler
61
- result = handle_command(message)
 
62
 
63
- if result == "exit":
64
- return "Session ended. Refresh the page to restart."
65
 
66
- return str(result) if result else "Command executed with no output."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- # ChatInterface is the closest 'look and feel' to a CLI
69
  demo = gr.ChatInterface(
70
  fn=chat_response,
71
- title="EDA Explorer Terminal",
72
- description="Type your EDA commands below. Works just like the CLI version!",
73
- examples=["help", "analyze data.csv", "status"] # Optional: suggest commands
 
74
  )
75
 
76
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
8
  import gradio as gr
9
  from cli_app.command_handler import handle_command
10
  from vector_store.instruction_embedder import embed_analyze_instructions
11
+ import io
12
 
13
  console = Console()
14
 
 
51
  console.print(f"Error: {e}")
52
 
53
  def run_space_interface():
 
 
 
54
  embed_analyze_instructions()
55
 
56
  def chat_response(message, history):
57
+ # 1. Create a "Fake" terminal to catch the output
58
+ output_buffer = io.StringIO()
59
+ # We use legacy_windows=False to ensure clean formatting on Linux servers
60
+ temp_console = Console(file=output_buffer, force_terminal=True, width=100, color_system="truecolor")
61
 
62
+ # 2. Access the handler's console
63
+ import cli_app.command_handler as handler
64
 
65
+ # Save the original to restore it later
66
+ old_console = getattr(handler, 'console', None)
67
+ handler.console = temp_console
68
+
69
+ try:
70
+ # 3. Run the command
71
+ result = handler.handle_command(message)
72
+
73
+ # 4. Get the captured "Rich" text
74
+ captured_output = output_buffer.getvalue()
75
+
76
+ # 5. Clean logic for the UI
77
+ if result == "exit":
78
+ return "Session ended. Refresh to restart."
79
+
80
+ # If the command printed a table, result is likely ""
81
+ # so we return the captured buffer.
82
+ return captured_output if not result else f"{captured_output}\n{result}"
83
+
84
+ except Exception as e:
85
+ return f"Error: {str(e)}"
86
+ finally:
87
+ # 6. Restore original console
88
+ if old_console:
89
+ handler.console = old_console
90
 
91
+ # Using ChatInterface for that modern terminal look
92
  demo = gr.ChatInterface(
93
  fn=chat_response,
94
+ title="📊 EDA Explorer Terminal",
95
+ description="Interactive CLI-style data analysis. Start by typing 'help' or 'load <url>'.",
96
+ theme="soft", # Looks clean for recruiters
97
+ examples=["help", "list", "analyze"]
98
  )
99
 
100
  demo.launch(server_name="0.0.0.0", server_port=7860)