|
"""Talk to spaces VM via subprocess.check_output.""" |
|
|
|
|
|
from typing import Optional, Tuple |
|
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 |
|
|
|
import matplotlib |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
|
|
|
|
from inspect import cleandoc |
|
import gradio as gr |
|
import logzero |
|
from logzero import logger |
|
|
|
from gradiobee.seg_text import seg_text |
|
|
|
matplotlib.use("Agg") |
|
sns.set() |
|
sns.set_style("darkgrid") |
|
logzero.loglevel() |
|
|
|
|
|
|
|
def process(command) -> Tuple[str, Optional[str]]: |
|
"""Probe vm.""" |
|
|
|
|
|
|
|
|
|
|
|
logger.info("input: %s", command) |
|
|
|
try: |
|
command = str(command).strip() |
|
except Exception as e: |
|
_ = str(e) |
|
return _, None |
|
|
|
if not command: |
|
return "gimme something to start with...", None |
|
|
|
is_command = True |
|
is_command = command.strip().splitlines().__len__() < 2 and len(command.strip()) < 500 |
|
|
|
if is_command: |
|
try: |
|
|
|
proc = sp.Popen( |
|
split(command), encoding="utf8", stdout=-1, stderr=-1 |
|
) |
|
out, err = proc.communicate() |
|
|
|
return f"[{out}], [{err}]", None |
|
except Exception as e: |
|
out, err = "", str(e) |
|
|
|
|
|
return f"[{out}], [{err}]", None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
df_ = pd.DataFrame(data={'x': [1, 2], 'y': [3, 4], "cos": [0.1, 0.5]}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ = "\n\n".join(seg_text(command.strip())) |
|
|
|
logger.debug(_) |
|
|
|
|
|
|
|
|
|
|
|
_ = cleandoc( |
|
f"""seg_text output (segmented sents): |
|
{_} |
|
""" |
|
).strip() |
|
|
|
|
|
|
|
plt.figure() |
|
sns.scatterplot(data=df_, x='x', y='y') |
|
Path("img").mkdir(exist_ok=True) |
|
plt.savefig("img/df.png") |
|
plt.close() |
|
df_png = "img/df.png" |
|
|
|
|
|
return _, df_png |
|
|
|
|
|
iface = gr.Interface( |
|
|
|
|
|
fn=process, |
|
|
|
inputs=gr.inputs.Textbox( |
|
lines=5, |
|
placeholder="Type or paste input here then click 'Submit'", |
|
default="python -V # show version", |
|
label="command or multiline text", |
|
), |
|
|
|
|
|
|
|
outputs=[ |
|
"text", |
|
gr.outputs.Image("auto"), |
|
], |
|
examples=[ |
|
"python -m site", |
|
"""python -c "import gradio; print(gradio.__version__")""", |
|
"python -V ", |
|
"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=False, debug=True) |
|
|
|
|
|
|