inoki-giskard commited on
Commit
59166be
1 Parent(s): 6348ca6

Use a subprocess to run ML worker

Browse files
Files changed (1) hide show
  1. app.py +15 -44
app.py CHANGED
@@ -1,36 +1,14 @@
1
  import gradio as gr
2
 
3
- from giskard.ml_worker.ml_worker import MLWorker
4
- from pydantic import AnyHttpUrl
5
- from giskard.settings import settings
6
  from urllib.parse import urlparse
7
 
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:
@@ -40,43 +18,36 @@ def read_logs():
40
  previous_url = ""
41
  ml_worker = None
42
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- def run_ml_worker(ml_worker: MLWorker):
45
- loop = asyncio.new_event_loop()
46
- asyncio.set_event_loop(loop)
47
-
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(
68
- url=f"{parsed_url.scheme if parsed_url.scheme else 'http'}://{parsed_url.hostname}"
69
- f"/{parsed_url.path if parsed_url.path and len(parsed_url.path) else settings.ws_path}",
70
- scheme=parsed_url.scheme,
71
- host=parsed_url.hostname,
72
- path=parsed_url.path if parsed_url.path and len(parsed_url.path) else settings.ws_path,
73
- )
74
- print(f"Starting ML worker for {backend_url}")
75
- ml_worker = MLWorker(False, backend_url, api_key, hf_token)
76
- previous_url = backend_url
77
- thread = threading.Thread(target=run_ml_worker, args=(ml_worker,))
78
  thread.start()
79
- return f"ML worker running for {backend_url}"
80
 
81
 
82
  with gr.Blocks() as iface:
 
1
  import gradio as gr
2
 
 
 
 
3
  from urllib.parse import urlparse
4
 
5
+ import subprocess
6
  import threading
7
 
8
  import sys
9
 
10
  LOG_FILE = "output.log"
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def read_logs():
13
  sys.stdout.flush()
14
  with open(LOG_FILE, "r") as f:
 
18
  previous_url = ""
19
  ml_worker = None
20
 
21
+ def run_ml_worker(url, api_key, hf_token):
22
+ global ml_worker, previous_url
23
+ previous_url = url
24
+ ml_worker = subprocess.run(
25
+ [
26
+ "giskard", "worker", "start",
27
+ "-u", f"{url}", "-k", f"{api_key}", "-t", f"{hf_token}"
28
+ ],
29
+ stdout=open(LOG_FILE, "w"), stderr=sys.stdout
30
+ )
31
 
 
 
 
 
 
 
32
 
33
  def stop_ml_worker():
34
  global ml_worker, previous_url
35
  if ml_worker is not None:
36
  print(f"Stopping ML worker for {previous_url}")
37
+ ml_worker.terminate()
38
  print("ML worker stopped")
39
  return "ML worker stopped"
40
  return "ML worker not started"
41
 
42
 
43
  def start_ml_worker(url, api_key, hf_token):
 
44
  # Always run an external ML worker
45
  stop_ml_worker()
46
 
47
+ print(f"Starting ML worker for {url}")
48
+ thread = threading.Thread(target=run_ml_worker, args=(url, api_key, hf_token))
 
 
 
 
 
 
 
 
 
 
49
  thread.start()
50
+ return f"ML worker running for {url}"
51
 
52
 
53
  with gr.Blocks() as iface: