ProfessionalMario commited on
Commit
3eb2f44
Β·
1 Parent(s): 39f65bf

command fix

Browse files
Files changed (1) hide show
  1. app.py +82 -72
app.py CHANGED
@@ -1,36 +1,92 @@
1
  import os
 
2
  import sys
 
3
  from rich.console import Console
 
 
4
  from cli_app.command_handler import handle_command
 
5
  from utils.logger import logger
6
  from vector_store.instruction_embedder import embed_analyze_instructions
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
- import io
12
 
 
13
  console = Console()
14
 
15
- def run_web():
16
- """The 'Space Way': A Gradio interface that acts like your CLI."""
17
- import gradio as gr
 
 
18
 
19
- def chat_interface(command):
20
- # Calls your existing logic exactly like the CLI
21
- return handle_command(command)
22
-
23
- demo = gr.Interface(
24
- fn=chat_interface,
25
- inputs=gr.Textbox(label="EDA Command", placeholder="Type your command here..."),
26
- outputs=gr.Code(label="Terminal Output", language="markdown"),
27
- title="EDA Explorer",
28
- description="Web terminal for EDA Explorer. Type 'help' or your analysis commands."
29
  )
30
- # HF Spaces uses port 7860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  demo.launch(server_name="0.0.0.0", server_port=7860)
32
 
33
  def run_cli():
 
34
  embed_analyze_instructions()
35
  console.print("\n[bold cyan]EDA Explorer[/bold cyan]")
36
  console.print("Type 'exit' to quit\n")
@@ -48,58 +104,12 @@ def run_cli():
48
  break
49
  except Exception as e:
50
  logger.error(f"CLI error | {e}")
51
- console.print(f"Error: {e}")
52
-
53
- def run_space_interface():
54
- embed_analyze_instructions()
55
-
56
- def terminal_logic(command):
57
- output_buffer = io.StringIO()
58
- # width=80 is safer for mobile/small screens
59
- # color_system=None is CRITICAL to stop the  junk
60
- temp_console = Console(
61
- file=output_buffer,
62
- force_terminal=True,
63
- width=80,
64
- color_system=None,
65
- legacy_windows=False
66
- )
67
-
68
- import cli_app.command_handler as handler
69
- old_console = getattr(handler, 'console', None)
70
- handler.console = temp_console
71
-
72
- try:
73
- result = handler.handle_command(command)
74
- captured = output_buffer.getvalue()
75
- # If your handler already prints, we just return the captured text
76
- return captured if not result else f"{captured}\n{result}"
77
- except Exception as e:
78
- return f"Error: {str(e)}"
79
- finally:
80
- if old_console:
81
- handler.console = old_console
82
-
83
- with gr.Blocks(title="EDA Explorer Terminal", theme="monochrome") as demo:
84
- gr.Markdown("# πŸ“Š EDA Explorer Terminal")
85
- gr.Markdown("Type `help` to see commands or `list` to see datasets.")
86
-
87
- input_box = gr.Textbox(label="Command", placeholder="Type here...", autofocus=True)
88
- # gr.Code forces the Monospace font so the table lines (┏━┓) align!
89
- output_box = gr.Code(label="Console Output", language="text", lines=20)
90
-
91
- input_box.submit(fn=terminal_logic, inputs=input_box, outputs=output_box)
92
-
93
- demo.launch(server_name="0.0.0.0", server_port=7860)
94
 
95
  if __name__ == "__main__":
96
- # Force initialize the instructions/embeddings before anything else
97
- print("Initializing EDA System...")
98
- try:
99
- embed_analyze_instructions()
100
- print("Embeddings loaded successfully.")
101
- except Exception as e:
102
- print(f"Embedding Error: {e}")
103
-
104
- # Start the interface
105
- run_space_interface()
 
1
  import os
2
+ import io
3
  import sys
4
+ import gradio as gr
5
  from rich.console import Console
6
+
7
+ # Import your custom modules
8
  from cli_app.command_handler import handle_command
9
+ import cli_app.command_handler as handler
10
  from utils.logger import logger
11
  from vector_store.instruction_embedder import embed_analyze_instructions
 
 
 
 
 
12
 
13
+ # Global console for local CLI use
14
  console = Console()
15
 
16
+ def terminal_logic(command):
17
+ """
18
+ Captures Rich console output and returns it as a string for Gradio.
19
+ """
20
+ output_buffer = io.StringIO()
21
 
22
+ # color_system=None removes the 'gibberish' ANSI escape codes
23
+ temp_console = Console(
24
+ file=output_buffer,
25
+ force_terminal=True,
26
+ width=100,
27
+ color_system=None,
28
+ legacy_windows=False
 
 
 
29
  )
30
+
31
+ # Safely swap the console in the command_handler module
32
+ old_console = getattr(handler, 'console', None)
33
+ handler.console = temp_console
34
+
35
+ try:
36
+ if command.lower() == "exit":
37
+ return "Session ended. Please refresh the page to restart."
38
+
39
+ # Execute the actual logic
40
+ result = handle_command(command)
41
+
42
+ # Get what was printed to our 'fake' terminal (tables, etc.)
43
+ captured = output_buffer.getvalue()
44
+
45
+ # Merge printed output and returned string
46
+ if result and result not in captured:
47
+ return f"{captured}\n{result}".strip()
48
+ return captured.strip() or "Command executed."
49
+
50
+ except Exception as e:
51
+ logger.error(f"Space Terminal Error: {e}")
52
+ return f"Error: {str(e)}"
53
+ finally:
54
+ # ALWAYS restore the original console
55
+ if old_console:
56
+ handler.console = old_console
57
+
58
+ def run_space_interface():
59
+ """
60
+ Hugging Face Space Layout using a Monospace Code block for the terminal feel.
61
+ """
62
+ embed_analyze_instructions()
63
+
64
+ with gr.Blocks(title="EDA Explorer Terminal", theme="monochrome") as demo:
65
+ gr.Markdown("# πŸ“Š EDA Explorer Terminal")
66
+ gr.Markdown("Direct CLI access for dataset analysis. Type `help` to see available commands.")
67
+
68
+ with gr.Row():
69
+ output_box = gr.Code(
70
+ label="Console Output",
71
+ language="markdown", # markdown supports the box-drawing characters safely
72
+ lines=25,
73
+ interactive=False
74
+ )
75
+
76
+ with gr.Row():
77
+ input_box = gr.Textbox(
78
+ label="Command Line",
79
+ placeholder="e.g., load data.csv, list, analyze dataset...",
80
+ autofocus=True
81
+ )
82
+
83
+ # Submit button logic
84
+ input_box.submit(fn=terminal_logic, inputs=input_box, outputs=output_box)
85
+
86
  demo.launch(server_name="0.0.0.0", server_port=7860)
87
 
88
  def run_cli():
89
+ """Standard local terminal loop."""
90
  embed_analyze_instructions()
91
  console.print("\n[bold cyan]EDA Explorer[/bold cyan]")
92
  console.print("Type 'exit' to quit\n")
 
104
  break
105
  except Exception as e:
106
  logger.error(f"CLI error | {e}")
107
+ console.print(f"[red]Error:[/red] {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  if __name__ == "__main__":
110
+ # Check if running on Hugging Face or locally
111
+ if "SPACE_ID" in os.environ:
112
+ print("Starting Web Interface...")
113
+ run_space_interface()
114
+ else:
115
+ run_cli()