inoki-giskard commited on
Commit
4be4da9
1 Parent(s): aeabc6f

Attempt to display CPU and GPU usage

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py CHANGED
@@ -5,6 +5,7 @@ import logging
5
  import subprocess
6
  import threading
7
 
 
8
  import sys
9
  import os
10
 
@@ -100,6 +101,30 @@ def start_ml_worker(url, api_key, hf_token):
100
  thread.start()
101
  return f"ML worker running for {url}"
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  theme = gr.themes.Soft(
104
  primary_hue="green",
105
  )
@@ -130,6 +155,7 @@ with gr.Blocks(theme=theme) as iface:
130
 
131
  with gr.Column():
132
  output = gr.Textbox(label="Status")
 
133
  if READONLY:
134
  gr.Textbox("You are browsering a read-only 🐢 Giskard ML worker instance. ", container=False)
135
  gr.Textbox("Please duplicate this space to configure your own Giskard ML worker.", container=False)
 
5
  import subprocess
6
  import threading
7
 
8
+ import psutil
9
  import sys
10
  import os
11
 
 
101
  thread.start()
102
  return f"ML worker running for {url}"
103
 
104
+
105
+
106
+
107
+ def get_gpu_usage():
108
+ # Referring: https://stackoverflow.com/questions/67707828/how-to-get-every-seconds-gpu-usage-in-python
109
+ output_to_list = lambda x: x.decode('ascii').split('\n')[:-1]
110
+ COMMAND = "nvidia-smi --query-gpu=utilization.gpu --format=csv"
111
+ try:
112
+ gpu_use_info = output_to_list(subprocess.check_output(COMMAND.split(),stderr=subprocess.STDOUT))[1:]
113
+ except subprocess.CalledProcessError:
114
+ return "Unavailable"
115
+ gpu_use_values = [int(x.split()[0]) for i, x in enumerate(gpu_use_info)]
116
+ return gpu_use_values[0] if len(gpu_use_values) > 0 else "Unavailable"
117
+
118
+
119
+ def get_usage():
120
+ from shutil import which
121
+ gpu_usage_info = ""
122
+ if which("nvidia-smi") is not None:
123
+ gpu_usage_info = f" GPU: {get_gpu_usage()}"
124
+
125
+ return f"CPU: {psutil.cpu_percent()} %{gpu_usage_info}"
126
+
127
+
128
  theme = gr.themes.Soft(
129
  primary_hue="green",
130
  )
 
155
 
156
  with gr.Column():
157
  output = gr.Textbox(label="Status")
158
+ gr.Textbox(value=get_usage, label="Usage", every=1.0)
159
  if READONLY:
160
  gr.Textbox("You are browsering a read-only 🐢 Giskard ML worker instance. ", container=False)
161
  gr.Textbox("Please duplicate this space to configure your own Giskard ML worker.", container=False)