| | """Talk to spaces VM via subprocess.check_output.""" |
| | |
| | import sys |
| | from pathlib import Path |
| | if "." not in sys.path: |
| | sys.path.insert(0, ".") |
| |
|
| | |
| | import subprocess as sp |
| | from shlex import split |
| | import pandas as pd |
| |
|
| | |
| | from inspect import cleandoc |
| | import gradio as gr |
| | import logzero |
| | from logzero import logger |
| |
|
| | from gradiobee.seg_text import seg_text |
| |
|
| | logzero.loglevel() |
| |
|
| |
|
| | |
| | def process(command): |
| | """Probe vm.""" |
| | |
| | |
| | |
| | |
| | is_command = True |
| | is_command = command.strip().splitlines().__len__() < 2 and len(command.strip()) < 100 |
| |
|
| | if is_command: |
| | try: |
| | |
| | proc = sp.Popen( |
| | split(command), encoding="utf8", stdout=-1, stderr=-1 |
| | ) |
| | out, err = proc.communicate() |
| | success = True |
| | except Exception as e: |
| | out, err = "", str(e) |
| | success = False |
| |
|
| | if success: |
| | out = "\n".join( |
| | ( |
| | out, |
| | err, |
| | ) |
| | ).strip() |
| | if not out: |
| | out = "No output, that's all we know." |
| | return out, None |
| |
|
| | |
| | |
| | import altair as alt |
| | from altair_saver import save |
| | alt.renderers.enable('altair_saver', fmts=['vega-lite', 'png']) |
| |
|
| | df_ = pd.DataFrame(data={'x': [1, 2], 'y': [3, 4], "cos": [0.1, 0.5]}) |
| | chart_df = alt.Chart(df_).mark_circle(size=60).encode( |
| | x='x', |
| | y='y', |
| | color='cos', |
| | |
| | ) |
| | |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| | chart_df.save("chart_df.png") |
| | chart_df_png = "chart_df.png" |
| |
|
| | |
| | _ = "\n\n".join(seg_text(command.strip())) |
| |
|
| | logger.debug(_) |
| | |
| | |
| | |
| |
|
| | |
| | _ = cleandoc( |
| | f"""seg_text output (segmented sents): |
| | {_} |
| | """ |
| | ).strip() |
| |
|
| | |
| |
|
| | return _, chart_df_png |
| |
|
| |
|
| | iface = gr.Interface( |
| | |
| | |
| | fn=process, |
| | |
| | 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", |
| | gr.outputs.Image("auto"), |
| | ], |
| | 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 ", |
| | layout="vertical", |
| | ) |
| |
|
| | iface.launch(share=True, debug=True) |
| | |
| |
|