mikeee commited on
Commit
937eac0
·
1 Parent(s): 6940a4a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -0
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Talk to spaces VM via subprocess.check_output."""
2
+ # pylint: disable=wrong-import-position
3
+ import sys
4
+ from pathlib import Path
5
+ if "." not in sys.path:
6
+ sys.path.insert(0, ".")
7
+
8
+ # import httpx
9
+ import subprocess as sp
10
+ from shlex import split
11
+ import pandas as pd
12
+
13
+ import matplotlib
14
+ import matplotlib.pyplot as plt
15
+ import seaborn as sns
16
+
17
+ # from textwrap import dedent
18
+ from inspect import cleandoc
19
+ import gradio as gr
20
+ import logzero
21
+ from logzero import logger
22
+
23
+ from gradiobee.seg_text import seg_text
24
+
25
+ matplotlib.use("Agg") # non-interactive for plt.savefig
26
+ sns.set()
27
+ sns.set_style("darkgrid")
28
+ logzero.loglevel() # default to 10
29
+
30
+
31
+ # def greet(command):
32
+ def process(command):
33
+ """Probe vm."""
34
+ # single line: command
35
+ # logger.info("input:\n\t %s", command)
36
+ # print("print input:\n\t %s" % command)
37
+ # print("print input*:\n\t %s" % "*".join(command.splitlines()))
38
+ is_command = True
39
+ is_command = command.strip().splitlines().__len__() < 2 and len(command.strip()) < 100
40
+
41
+ if is_command:
42
+ try:
43
+ # out = sp.check_output(split(command), encoding="utf8", stderr=STDOUT)
44
+ proc = sp.Popen(
45
+ split(command), encoding="utf8", stdout=-1, stderr=-1
46
+ ) # sp.PIPE: -1
47
+ out, err = proc.communicate()
48
+ success = True
49
+ except Exception as e:
50
+ out, err = "", str(e)
51
+ success = False
52
+
53
+ if success:
54
+ out = "\n".join(
55
+ (
56
+ out,
57
+ err,
58
+ )
59
+ ).strip()
60
+ if not out:
61
+ out = "No output, that's all we know."
62
+ return out, None
63
+
64
+ # quick test altair altair-save tooltip
65
+ # from PIL import Image
66
+
67
+ # import altair as alt
68
+ # from altair_saver import save
69
+ # alt.renderers.enable('altair_saver', fmts=['vega-lite', 'png'])
70
+
71
+ df_ = pd.DataFrame(data={'x': [1, 2], 'y': [3, 4], "cos": [0.1, 0.5]})
72
+ # chart_df = alt.Chart(df_).mark_circle(size=60).encode(
73
+ # x='x',
74
+ # y='y',
75
+ # color='cos',
76
+ # tooltip=['x', 'y', 'cos', ]
77
+ # )
78
+ # .interactive()
79
+
80
+ # save(chart_df, "chart_df.html")
81
+ # chart_df_html = Path("chart_df.html").read_text("utf")
82
+ # save(chart_df, "chart_df.png")
83
+ # chart_df_png = Path("chart_df.png").read_bytes()
84
+
85
+ # chart_df_png = Image.open("chart_df.png")
86
+ # chart_df_png = "chart_df.png"
87
+
88
+ # scatter_plot.save('simple_scatter_plot_with_altairchart.html')
89
+ # chart_df.save("chart_df.html") # does not work, constains js
90
+ # chart_df_html = Path("chart_df.html").read_text("utf")
91
+
92
+ # chart_df.save("chart_df.png") #
93
+ # chart_df_png = "chart_df.png"
94
+
95
+ # not is_command or not flag: text, do seg_text
96
+ _ = "\n\n".join(seg_text(command.strip()))
97
+
98
+ logger.debug(_)
99
+ # logger.debug(chart_df_html)
100
+ # print(_)
101
+ # print(chart_df_html)
102
+
103
+ # _ = seg_text(command.strip())
104
+ _ = cleandoc(
105
+ f"""seg_text output (segmented sents):
106
+ {_}
107
+ """
108
+ ).strip()
109
+
110
+ # return _, chart_df_html
111
+
112
+ plt.figure()
113
+ sns.scatterplot(data=df_, x='x', y='y')
114
+ Path("img").mkdir(exist_ok=True)
115
+ plt.savefig("img/df.png")
116
+ plt.close()
117
+ df_png = "img/df.png"
118
+
119
+ # return _, chart_df_png
120
+ return _, df_png
121
+
122
+
123
+ iface = gr.Interface(
124
+ # fn=greet,
125
+ # inputs="text",
126
+ fn=process,
127
+ # inputs="text",
128
+ inputs=gr.inputs.Textbox(
129
+ lines=5,
130
+ placeholder="Type or paste input here then click 'Submit'",
131
+ default="python -m site",
132
+ label="command or multiline text",
133
+ ),
134
+ # outputs="text",
135
+ # outputs=["text",],
136
+ # outputs=["text", "html"],
137
+ outputs=[
138
+ "text",
139
+ gr.outputs.Image("auto"),
140
+ ],
141
+ examples=[
142
+ "cat /proc/version",
143
+ "free # show free memory",
144
+ "uname -m",
145
+ "df -h .",
146
+ "cat /proc/cpuinfo",
147
+ """python -c "from psutil import virtual_memory; print(virtual_memory())" """,
148
+ ],
149
+ title="probe the system",
150
+ description="Talk to the system via subprocess.check_output ",
151
+ layout="vertical",
152
+ )
153
+
154
+ iface.launch(share=True, debug=True)
155
+ # iface.launch()