Commit ·
6348ca6
1
Parent(s): 66e8b15
Allow to start and stop worker
Browse files
app.py
CHANGED
|
@@ -8,6 +8,35 @@ from urllib.parse import urlparse
|
|
| 8 |
import asyncio
|
| 9 |
import threading
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
previous_url = ""
|
| 12 |
ml_worker = None
|
| 13 |
|
|
@@ -19,14 +48,20 @@ def run_ml_worker(ml_worker: MLWorker):
|
|
| 19 |
loop.run_until_complete(ml_worker.start())
|
| 20 |
loop.close()
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def start_ml_worker(url, api_key, hf_token):
|
| 24 |
global ml_worker, previous_url
|
| 25 |
-
# Always run an external ML worker
|
| 26 |
if ml_worker is not None:
|
| 27 |
print(f"Stopping ML worker for {previous_url}")
|
| 28 |
ml_worker.stop()
|
| 29 |
print("ML worker stopped")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
parsed_url = urlparse(url)
|
| 32 |
backend_url = AnyHttpUrl(
|
|
@@ -43,5 +78,24 @@ def start_ml_worker(url, api_key, hf_token):
|
|
| 43 |
thread.start()
|
| 44 |
return f"ML worker running for {backend_url}"
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
iface.launch()
|
|
|
|
| 8 |
import asyncio
|
| 9 |
import threading
|
| 10 |
|
| 11 |
+
import sys
|
| 12 |
+
|
| 13 |
+
LOG_FILE = "output.log"
|
| 14 |
+
|
| 15 |
+
class Logger:
|
| 16 |
+
def __init__(self, filename):
|
| 17 |
+
self.terminal = sys.stdout
|
| 18 |
+
self.log = open(filename, "w")
|
| 19 |
+
|
| 20 |
+
def write(self, message):
|
| 21 |
+
self.terminal.write(message)
|
| 22 |
+
self.log.write(message)
|
| 23 |
+
|
| 24 |
+
def flush(self):
|
| 25 |
+
self.terminal.flush()
|
| 26 |
+
self.log.flush()
|
| 27 |
+
|
| 28 |
+
def isatty(self):
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
+
sys.stdout = Logger(LOG_FILE)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def read_logs():
|
| 35 |
+
sys.stdout.flush()
|
| 36 |
+
with open(LOG_FILE, "r") as f:
|
| 37 |
+
return f.read()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
previous_url = ""
|
| 41 |
ml_worker = None
|
| 42 |
|
|
|
|
| 48 |
loop.run_until_complete(ml_worker.start())
|
| 49 |
loop.close()
|
| 50 |
|
| 51 |
+
def stop_ml_worker():
|
|
|
|
| 52 |
global ml_worker, previous_url
|
|
|
|
| 53 |
if ml_worker is not None:
|
| 54 |
print(f"Stopping ML worker for {previous_url}")
|
| 55 |
ml_worker.stop()
|
| 56 |
print("ML worker stopped")
|
| 57 |
+
return "ML worker stopped"
|
| 58 |
+
return "ML worker not started"
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def start_ml_worker(url, api_key, hf_token):
|
| 62 |
+
global ml_worker, previous_url
|
| 63 |
+
# Always run an external ML worker
|
| 64 |
+
stop_ml_worker()
|
| 65 |
|
| 66 |
parsed_url = urlparse(url)
|
| 67 |
backend_url = AnyHttpUrl(
|
|
|
|
| 78 |
thread.start()
|
| 79 |
return f"ML worker running for {backend_url}"
|
| 80 |
|
| 81 |
+
|
| 82 |
+
with gr.Blocks() as iface:
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column():
|
| 85 |
+
url_input = gr.Textbox(label="Giskard Hub URL")
|
| 86 |
+
api_key_input = gr.Textbox(label="Giskard Hub API Key", placeholder="gsk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
|
| 87 |
+
hf_token_input = gr.Textbox(label="Hugging Face Spaces Token")
|
| 88 |
+
|
| 89 |
+
output = gr.Textbox(label="Status")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
run_btn = gr.Button("Run")
|
| 92 |
+
run_btn.click(start_ml_worker, [url_input, api_key_input, hf_token_input], output)
|
| 93 |
+
|
| 94 |
+
stop_btn = gr.Button("Stop")
|
| 95 |
+
stop_btn.click(stop_ml_worker, None, output)
|
| 96 |
+
|
| 97 |
+
logs = gr.Textbox(label="Giskard ML worker log:")
|
| 98 |
+
iface.load(read_logs, None, logs, every=0.5)
|
| 99 |
+
|
| 100 |
+
iface.queue()
|
| 101 |
iface.launch()
|