"""Talk to spaces VM via subprocess.check_output.""" # import httpx import subprocess as sp from shlex import split # from textwrap import dedent from inspect import cleandoc import gradio as gr from logzero import logger from gradiobee.seg_text import seg_text # def greet(command): def process(command): """Probe vm.""" # single line: command # logger.info("input:\n\t %s", command) # print("print input:\n\t %s" % command) # print("print input*:\n\t %s" % "*".join(command.splitlines())) is_command = True is_command = command.strip().splitlines().__len__() < 2 and len(comman.strip()) < 100 if is_command: proc = sp.Popen( split(command), encoding="utf8", stdout=-1, stderr=-1 ) # sp.PIPE: -1 try: # out = sp.check_output(split(command), encoding="utf8", stderr=STDOUT) out, err = proc.communicate() except Exception as e: out, err = "", str(e) out = "\n".join( ( out, err, ) ).strip() if not out: out = "No output, that's all we know." return out # not is_command: text, do seg_text _ = "\n".join(seg_text(command.strip())) _ = seg_text(command.strip()) return cleandoc( f"""seg_text output (segmented sents): {_} """ ).strip() iface = gr.Interface( # fn=greet, # inputs="text", fn=process, # inputs="text", inputs=gr.inputs.Textbox( lines=5, placeholder="Type or paste input here then click 'Submit'", default="python -m site", label="command or multiline text", ), outputs="text", examples=[ "cat /proc/version", "free # show free memory", "uname -m", "df -h .", "cat /proc/cpuinfo", """python -c "from psutil import virtual_memory; print(virtual_memory())" """, ], title="probe the system", description="Talk to the system via subprocess.check_output ", ) # iface.launch(share=True, debug=True) iface.launch(debug=True)