Skip to content

Commit cd734f0

Browse files
@FIR-748: Added endpoints for health, sysinfo, upload and restart (#15)
1 parent 6191598 commit cd734f0

File tree

2 files changed

+101
-14
lines changed

2 files changed

+101
-14
lines changed

tools/flaskIfc/flaskIfc.py

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22
import subprocess
33
import threading
44
import time
5+
from werkzeug.utils import secure_filename
6+
import os
57

68
job_status = {"running": False, "result": "", "thread": None}
79

810
app = Flask(__name__)
911

12+
port = '/dev/ttyUSB3'
13+
baudrate = '921600'
14+
1015
@app.route('/')
1116
def index():
1217
return render_template('index.html')
1318

1419
@app.route('/llama-cli', methods=['GET'])
15-
def serial_command():
16-
# Currently the port is hard coded to /dev/ttyUSB3 but can be parameterized
17-
port = '/dev/ttyUSB3'
18-
#port = request.args.get('port')
19-
20-
# Currently the baudrate is hard coded to 921600 but can be parameterized
21-
#baudrate = request.args.get('baudrate')
22-
baudrate = '921600'
23-
#./run_platform_test.sh "my cat's name" "10" "tinyllama-vo-5m-para.gguf" "none"
20+
def llama_cli_serial_command():
21+
22+
#./run_llama_cli.sh "my cat's name" "10" "tinyllama-vo-5m-para.gguf" "none"
2423
model = request.args.get('model')
2524
backend = request.args.get('backend')
2625
tokens = request.args.get('tokens')
@@ -59,7 +58,95 @@ def serial_command():
5958
except subprocess.CalledProcessError as e:
6059
return f"Error executing script: {e.stderr}", 500
6160

61+
UPLOAD_FOLDER = './' # Directory where uploaded files will be stored
62+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
63+
os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Create the upload folder if it doesn't exist
64+
65+
@app.route('/upload-gguf', methods=['POST', 'GET'])
66+
def upload_serial_command():
67+
if request.method == 'POST':
68+
# Check if a file was submitted
69+
if 'file' not in request.files:
70+
return "No file part"
71+
file = request.files['file']
72+
73+
# Check if the file is empty
74+
if file.filename == '':
75+
return "No file selected"
76+
77+
# Save the file if it exists
78+
if file:
79+
filename = secure_filename(file.filename)
80+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
81+
return "File uploaded successfully"
82+
return render_template('upload.html') # Display the upload form
83+
84+
# command = f"upload file"
85+
# try:
86+
# result = subprocess.run(['python3', 'serial_script.py', port, baudrate, command], capture_output=True, text=True, check=True)
87+
# return result.stdout, 200
88+
# except subprocess.CalledProcessError as e:
89+
# return f"Error executing script: {e.stderr}", 500
90+
91+
@app.route('/upload-file', methods=['GET', 'POST'])
92+
def upload_file():
93+
if request.method == 'POST':
94+
# Check if a file was submitted
95+
if 'file' not in request.files:
96+
return "No file part"
97+
file = request.files['file']
98+
99+
# Check if the file is empty
100+
if file.filename == '':
101+
return "No file selected"
102+
103+
# Save the file if it exists
104+
if file:
105+
filename = secure_filename(file.filename)
106+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
107+
return "File uploaded successfully"
108+
return render_template('upload.html') # Display the upload form
109+
110+
@app.route('/restart-txe', methods=['GET'])
111+
def restart_txe_serial_command():
112+
command = f"telnet localhost 8000; close all"
113+
114+
try:
115+
result = subprocess.run(['python3', 'serial_script.py', port, baudrate, command], capture_output=True, text=True, check=True)
116+
return result.stdout, 200
117+
except subprocess.CalledProcessError as e:
118+
return f"Error executing script: {e.stderr}", 500
119+
120+
@app.route('/health-check', methods=['GET'])
121+
def health_check_serial_command():
122+
command = f"free -h"
123+
124+
try:
125+
result = subprocess.run(['python3', 'serial_script.py', port, baudrate, command], capture_output=True, text=True, check=True)
126+
return result.stdout, 200
127+
except subprocess.CalledProcessError as e:
128+
return f"Error executing script: {e.stderr}", 500
62129

130+
@app.route('/test', methods=['GET'])
131+
def test_serial_command():
132+
command = f"test"
133+
134+
try:
135+
result = subprocess.run(['python3', 'serial_script.py', port, baudrate, command], capture_output=True, text=True, check=True)
136+
return result.stdout, 200
137+
except subprocess.CalledProcessError as e:
138+
return f"Error executing script: {e.stderr}", 500
139+
140+
@app.route('/system-info', methods=['GET'])
141+
def system_info_serial_command():
142+
143+
command = f"lscpu"
144+
145+
try:
146+
result = subprocess.run(['python3', 'serial_script.py', port, baudrate, command], capture_output=True, text=True, check=True)
147+
return result.stdout, 200
148+
except subprocess.CalledProcessError as e:
149+
return f"Error executing script: {e.stderr}", 500
63150

64151
@app.route('/submit', methods=['POST'])
65152
def submit():
@@ -68,7 +155,7 @@ def submit():
68155
if job_status["running"]:
69156
return "<h2>A model is already running. Please wait or abort.</h2>"
70157

71-
#./run_platform_test.sh "my cat's name" "10" "tinyllama-vo-5m-para.gguf" "none"
158+
#./run_llama_cli.sh "my cat's name" "10" "tinyllama-vo-5m-para.gguf" "none"
72159
model = request.form.get('model')
73160
backend = request.form.get('backend')
74161
tokens = request.form.get('tokens')
@@ -96,11 +183,7 @@ def submit():
96183
# "--top-k", "0",
97184
# "--top-p", "1"
98185
#]
99-
# Currently the port is hard coded to /dev/ttyUSB3 but can be parameterized
100-
port = '/dev/ttyUSB3'
101186

102-
# Currently the baudrate is hard coded to 921600 but can be parameterized
103-
baudrate = '921600'
104187
script_path = "/usr/bin/tsi/v0.1.1.tsv31_06_06_2025/bin/run_llama_cli.sh"
105188
command = f"{script_path} \"{prompt}\" {tokens} {model_path} {backend}"
106189

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<form method="post" enctype="multipart/form-data">
2+
<input type="file" name="file">
3+
<input type="submit" value="Upload">
4+
</form>

0 commit comments

Comments
 (0)