From 8a5ffff1fee344726e4e6da14016fd92e6ad14b6 Mon Sep 17 00:00:00 2001 From: Anoop Kapoor Date: Wed, 11 Jun 2025 21:46:16 -0700 Subject: [PATCH 1/2] @FIR-733 - Lllama.cpp: Webserver, add JOB status support for Model --- tools/flaskIfc/flaskIfc.py | 55 ++++++++++++++++++++---- tools/flaskIfc/templates/processing.html | 22 ++++++++++ 2 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 tools/flaskIfc/templates/processing.html diff --git a/tools/flaskIfc/flaskIfc.py b/tools/flaskIfc/flaskIfc.py index 4d65c9a7ffa0e..e2945f680dfc1 100644 --- a/tools/flaskIfc/flaskIfc.py +++ b/tools/flaskIfc/flaskIfc.py @@ -1,5 +1,9 @@ from flask import Flask, render_template, request import subprocess +import threading +import time + +job_status = {"running": False, "result": "", "thread": None} app = Flask(__name__) @@ -9,6 +13,11 @@ def index(): @app.route('/submit', methods=['POST']) def submit(): + global job_status + + if job_status["running"]: + return "

A model is already running. Please wait or abort.

" + #./run_platform_test.sh "my cat's name" "10" "tinyllama-vo-5m-para.gguf" "none" model = request.form.get('model') backend = request.form.get('backend') @@ -25,7 +34,6 @@ def submit(): if not model_path: return f"

Error: Model path not found for '{model}'

" - # Below is for reference i will remove later # Build llama-cli command #command = [ # "./llama-cli", @@ -43,18 +51,47 @@ def submit(): # Currently the baudrate is hard coded to 921600 but can be parameterized baudrate = '921600' - script_path = "/usr/bin/tsi/v0.1.1.tsv31_06_06_2025/bin/run_platform_test.sh" + #command = script_path prompt tokens model backend + #command = script_path command = f"{script_path} \"{prompt}\" {tokens} {model_path} {backend}" - try: - result = subprocess.run(['python3', 'serial_script.py', port, baudrate, command], capture_output=True, text=True, check=True) - output = result.stdout # This should have \n - except subprocess.CalledProcessError as e: - output = f"Error running model: {e.stderr}" + def run_script(): + try: + result = subprocess.run(['python3', 'serial_script.py', port, baudrate, command], capture_output=True, text=True, check=True) + job_status["result"] = result.stdout + except subprocess.CalledProcessError as e: + job_status["result"] = f"Error: {e.stderr}" + finally: + job_status["running"] = False + + thread = threading.Thread(target=run_script) + job_status = {"running": True, "result": "", "thread": thread} + thread.start() + + return render_template("processing.html") + +@app.route('/status') +def status(): + if job_status["running"]: + return "running" + else: + return "done" + +@app.route('/result') +def result(): + return render_template("result.html", output=job_status["result"]) - return render_template('result.html', output=output) +@app.route('/abort') +def abort(): + global job_status + if job_status["running"] and job_status["thread"].is_alive(): + # Use subprocess.Popen + pid handling instead for real process termination + job_status["running"] = False + job_status["result"] = "Aborted by user." + return "

Job aborted.

Home" + return "

No job running.

Home" if __name__ == '__main__': - app.run(debug=True, port=5000) + app.run(debug=True, port=5001) diff --git a/tools/flaskIfc/templates/processing.html b/tools/flaskIfc/templates/processing.html new file mode 100644 index 0000000000000..15f609bee1712 --- /dev/null +++ b/tools/flaskIfc/templates/processing.html @@ -0,0 +1,22 @@ + + + + Processing + + + +

Model is running...

+ + + From 52ae0e9eedc0fd11642eda9740c03cf93f1106dc Mon Sep 17 00:00:00 2001 From: Anoop Kapoor Date: Wed, 11 Jun 2025 21:49:13 -0700 Subject: [PATCH 2/2] removing commented code --- tools/flaskIfc/flaskIfc.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/flaskIfc/flaskIfc.py b/tools/flaskIfc/flaskIfc.py index e2945f680dfc1..bba12448a1177 100644 --- a/tools/flaskIfc/flaskIfc.py +++ b/tools/flaskIfc/flaskIfc.py @@ -52,8 +52,6 @@ def submit(): # Currently the baudrate is hard coded to 921600 but can be parameterized baudrate = '921600' script_path = "/usr/bin/tsi/v0.1.1.tsv31_06_06_2025/bin/run_platform_test.sh" - #command = script_path prompt tokens model backend - #command = script_path command = f"{script_path} \"{prompt}\" {tokens} {model_path} {backend}" @@ -94,4 +92,4 @@ def abort(): return "

No job running.

Home" if __name__ == '__main__': - app.run(debug=True, port=5001) + app.run(debug=True, port=5000)