ljy266987 commited on
Commit
e713918
1 Parent(s): a9c8347

add gpu info

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +39 -5
  3. requirements.txt +5 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .idea
app.py CHANGED
@@ -1,15 +1,49 @@
1
- import os
2
- os.system('pip install transformers -U')
3
- os.system('pip install modelscope -U')
4
- os.system('pip install accelerate')
5
- os.system('pip install psutil')
6
 
 
 
7
  # 获取全部环境变量
8
  env_vars = os.environ
9
 
10
  # 遍历并打印环境变量
11
  for key, value in env_vars.items():
12
  print(f"{key}: {value}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  import spaces
15
  from threading import Thread
 
 
 
 
 
 
1
 
2
+ import os
3
+ ##
4
  # 获取全部环境变量
5
  env_vars = os.environ
6
 
7
  # 遍历并打印环境变量
8
  for key, value in env_vars.items():
9
  print(f"{key}: {value}")
10
+ ##
11
+ import subprocess
12
+ import json
13
+
14
+ # 运行nvidia-smi命令并以JSON格式获取输出
15
+ result = subprocess.run(
16
+ ['nvidia-smi', '--query-gpu=index,name,memory.total,memory.used,memory.free,utilization.gpu,temperature.gpu',
17
+ '--format=csv,noheader,nounits', '--json'],
18
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
19
+ )
20
+
21
+ # 解析输出
22
+ if result.stdout:
23
+ gpu_info_json = result.stdout
24
+
25
+ # 将JSON字符串转换成Python字典
26
+ gpu_info = json.loads(gpu_info_json)
27
+
28
+ # 打印全部GPU信息
29
+ print(json.dumps(gpu_info, indent=4))
30
+ else:
31
+ print("Error:", result.stderr)
32
+
33
+ ##
34
+ import GPUtil
35
+
36
+ # 获取所有可用的GPU列表
37
+ gpus = GPUtil.getGPUs()
38
+
39
+ # 遍历列表,打印每个GPU的详细信息
40
+ for gpu in gpus:
41
+ print(f"GPU ID: {gpu.id}, Name: {gpu.name}")
42
+ print(f" Total Memory: {gpu.memoryTotal}MB")
43
+ print(f" Used Memory: {gpu.memoryUsed}MB")
44
+ print(f" Free Memory: {gpu.memoryFree}MB")
45
+ print(f" GPU Utilization: {gpu.load*100}%")
46
+ print(f" GPU Temperature: {gpu.temperature}C\n")
47
 
48
  import spaces
49
  from threading import Thread
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ modelscope
3
+ accelerate
4
+ psutil
5
+ gputil