-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
95 lines (82 loc) · 2.52 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import uvicorn
from typing import Dict, Callable, Optional
import multiprocess
import pandas as pd
from texture.models import DatasetSchema
from texture.server import get_server
from texture.utils import is_notebook
from texture.database.preprocess import preprocess
TEXTURE_SERVER_PROCESS = None
try:
multiprocess.set_start_method("spawn", force=True)
except RuntimeError as e:
print("RuntimeError: ", e)
pass
def run(
data: pd.DataFrame = None,
schema: DatasetSchema = None,
load_tables: Dict[str, pd.DataFrame] = None,
create_new_embedding_func: Optional[Callable] = None,
host: str = "localhost",
port: int = 8080,
api_key: str = None,
):
if data is None and (schema is None or load_tables is None):
raise ValueError(
"Must provide data as pd.DataFrame OR both a schema and load_tables."
)
if data is not None and (schema is None or load_tables is None):
print("Preprocessing data...")
schema, load_tables = preprocess(data)
if is_notebook():
global TEXTURE_SERVER_PROCESS
if TEXTURE_SERVER_PROCESS is not None:
print("Terminating existing server process")
TEXTURE_SERVER_PROCESS.terminate()
TEXTURE_SERVER_PROCESS.join()
print("Running from a notebook, starting a new process")
TEXTURE_SERVER_PROCESS = multiprocess.Process(
target=run_server,
args=(
schema,
load_tables,
api_key,
host,
port,
create_new_embedding_func,
),
)
TEXTURE_SERVER_PROCESS.start()
# below will block notebook from progressing past server cell
# TEXTURE_SERVER_PROCESS.join()
else:
run_server(
schema,
load_tables,
api_key,
host,
port,
create_new_embedding_func,
)
def run_server(
dataset_schema: DatasetSchema,
load_tables: Dict[str, pd.DataFrame],
api_key: Optional[str] = None,
host: Optional[str] = "localhost",
port: Optional[int] = 8080,
create_new_embedding_func: Optional[Callable] = None,
):
app = get_server(
dataset_schema,
load_tables,
create_new_embedding_func,
api_key,
)
print(f"\n\033[1mTexture\033[0m running on http://{host}:{port}\n")
uvicorn.run(
app,
host=host,
port=port,
log_level="warning", # info or warning
# reload=True,
)