aka7774 commited on
Commit
a53c64d
1 Parent(s): d896ad0

Upload 6 files

Browse files
Files changed (6) hide show
  1. app.py +18 -0
  2. fn.py +52 -0
  3. install.bat +56 -0
  4. main.py +40 -0
  5. requirements.txt +4 -0
  6. venv.sh +7 -0
app.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import fn
3
+ import gradio as gr
4
+
5
+ def fn_run():
6
+ return json.dumps(fn.run())
7
+
8
+ with gr.Blocks() as demo:
9
+ info = gr.Markdown()
10
+ run_button = gr.Button(value='run')
11
+
12
+ run_button.click(
13
+ fn=fn_run,
14
+ outputs=[info],
15
+ )
16
+
17
+ if __name__ == '__main__':
18
+ demo.launch()
fn.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import csv
4
+ import subprocess
5
+ import psutil
6
+
7
+ def load_model():
8
+ pass
9
+
10
+ def run():
11
+ # WSL内で取得するとホストの値とは異なるので使い物にならない
12
+ try:
13
+ # load average per cpu count
14
+ la = round((psutil.getloadavg()[0] / psutil.cpu_count()) * 100)
15
+
16
+ # cpu
17
+ cpu = round(psutil.cpu_percent(percpu=False))
18
+
19
+ # ram
20
+ ram = psutil.virtual_memory()
21
+ rama = round(ram.available)
22
+ ramt = round(ram.total)
23
+ ramp = 100 - ram.percent
24
+ ramg = round(ram.available / 1073741824)
25
+
26
+ # vram
27
+ cmd = 'nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total --format=csv,noheader,nounits'
28
+ csv = subprocess.run(cmd, shell=True, capture_output=True, text=True).stdout
29
+ r = csv.split(',')
30
+ gpu = round(float(r[0]))
31
+ vrama = round(float(r[1]))
32
+ vramt = round(float(r[2]))
33
+ vramp = round(vrama / vramt * 100)
34
+ vramg = round(float(r[1]) / 1024)
35
+
36
+ # network
37
+ # 1秒ぶんだけ取りたかった
38
+ #net = psutil.net_io_counters()
39
+ #ni = round((float(net.bytes_recv) / 1048576) * 100)
40
+ #no = round((float(net.bytes_sent) / 1048576) * 100)
41
+
42
+ res = {
43
+ 'la': la,
44
+ 'cpu': cpu,
45
+ 'ram': ramg,
46
+ 'gpu': gpu,
47
+ 'vram': vramg,
48
+ }
49
+
50
+ return res
51
+ except Exception as e:
52
+ return {}
install.bat ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+
3
+ rem -------------------------------------------
4
+ rem NOT guaranteed to work on Windows
5
+
6
+ set APPDIR=perf
7
+ set REPOS=https://huggingface.co/spaces/aka7774/%APPDIR%
8
+ set VENV=venv
9
+
10
+ rem -------------------------------------------
11
+
12
+ set INSTALL_DIR=%~dp0
13
+ cd /d %INSTALL_DIR%
14
+
15
+ :git_clone
16
+ set DL_URL=%REPOS%
17
+ set DL_DST=%APPDIR%
18
+ git clone %DL_URL% %APPDIR%
19
+ if exist %DL_DST% goto install_python
20
+
21
+ set DL_URL=https://github.com/git-for-windows/git/releases/download/v2.41.0.windows.3/PortableGit-2.41.0.3-64-bit.7z.exe
22
+ set DL_DST=PortableGit-2.41.0.3-64-bit.7z.exe
23
+ curl -L -o %DL_DST% %DL_URL%
24
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
25
+ %DL_DST% -y
26
+ del %DL_DST%
27
+
28
+ set GIT=%INSTALL_DIR%PortableGit\bin\git
29
+ %GIT% clone %REPOS%
30
+
31
+ :install_python
32
+ set DL_URL=https://github.com/indygreg/python-build-standalone/releases/download/20240107/cpython-3.10.13+20240107-i686-pc-windows-msvc-shared-install_only.tar.gz
33
+ set DL_DST="%INSTALL_DIR%python.tar.gz"
34
+ curl -L -o %DL_DST% %DL_URL%
35
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
36
+ tar -xzf %DL_DST%
37
+
38
+ set PYTHON=%INSTALL_DIR%python\python.exe
39
+ set PATH=%PATH%;%INSTALL_DIR%python310\Scripts
40
+
41
+ :install_venv
42
+ cd %APPDIR%
43
+ %PYTHON% -m venv %VENV%
44
+ set PYTHON=%VENV%\Scripts\python.exe
45
+
46
+ :install_pip
47
+ set DL_URL=https://bootstrap.pypa.io/get-pip.py
48
+ set DL_DST=%INSTALL_DIR%get-pip.py
49
+ curl -o %DL_DST% %DL_URL%
50
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
51
+ %PYTHON% %DL_DST%
52
+
53
+ %PYTHON% -m pip install gradio
54
+ %PYTHON% -m pip install -r requirements.txt
55
+
56
+ pause
main.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import time
4
+ import signal
5
+ import psutil
6
+ import io
7
+
8
+ from fastapi import FastAPI, Request, status, Form, UploadFile
9
+ from fastapi.staticfiles import StaticFiles
10
+ from fastapi.middleware.cors import CORSMiddleware
11
+ from pydantic import BaseModel, Field
12
+ from fastapi.exceptions import RequestValidationError
13
+ from fastapi.responses import JSONResponse
14
+
15
+ import fn
16
+ import gradio as gr
17
+ from app import demo
18
+
19
+ app = FastAPI()
20
+
21
+ app.add_middleware(
22
+ CORSMiddleware,
23
+ allow_origins=['*'],
24
+ allow_credentials=True,
25
+ allow_methods=["*"],
26
+ allow_headers=["*"],
27
+ )
28
+
29
+ gr.mount_gradio_app(app, demo, path="/gradio")
30
+
31
+ fn.load_model()
32
+
33
+ @app.get("/json")
34
+ async def api_json():
35
+ try:
36
+ results = fn.run()
37
+
38
+ return results
39
+ except Exception as e:
40
+ return {"error": str(e)}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ python-multipart
4
+ psutil
venv.sh ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/bash
2
+
3
+ python3 -m venv venv
4
+ curl -kL https://bootstrap.pypa.io/get-pip.py | venv/bin/python
5
+
6
+ venv/bin/python -m pip install gradio
7
+ venv/bin/python -m pip install -r requirements.txt