test-gradio / app.py
inoki-giskard's picture
Try to start ML worker in async main
93b6465
raw
history blame
990 Bytes
import gradio as gr
import asyncio
from giskard.ml_worker.ml_worker import MLWorker
from pydantic import AnyHttpUrl
from giskard.settings import settings
from urllib.parse import urlparse
def run_ml_worker(url, api_key, hf_token):
# Always run an external ML worker
parsed_url = urlparse(url)
backend_url = AnyHttpUrl(
url=f"{parsed_url.scheme if parsed_url.scheme else 'http'}://{parsed_url.hostname}"
f"/{parsed_url.path if parsed_url.path and len(parsed_url.path) else settings.ws_path}",
scheme=parsed_url.scheme,
host=parsed_url.hostname,
path=parsed_url.path if parsed_url.path and len(parsed_url.path) else settings.ws_path,
)
print(f"Starting ML worker for {backend_url}")
ml_worker = MLWorker(False, backend_url, api_key, hf_token)
asyncio.run(ml_worker.start())
return "ML worker finished its work"
iface = gr.Interface(fn=run_ml_worker, inputs=["text", "text", "text"], outputs="text")
iface.launch()