forked from defog-ai/sqlcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
95 lines (81 loc) · 2.96 KB
/
Copy pathcli.py
File metadata and controls
95 lines (81 loc) · 2.96 KB
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 os
import sys
import sqlcoder
import subprocess
from huggingface_hub import snapshot_download, hf_hub_download
USAGE_STRING = """
Usage: sqlcoder <command>
Available commands:
sqlcoder launch
sqlcoder serve-webserver
sqlcoder serve-static
"""
home_dir = os.path.expanduser("~")
def main():
if len(sys.argv) < 2:
print(USAGE_STRING)
sys.exit(1)
if sys.argv[1] == "launch":
launch()
elif sys.argv[1] == "serve-webserver":
serve_webserver()
elif sys.argv[1] == "serve-static":
serve_static()
else:
print(USAGE_STRING)
sys.exit(1)
def serve_webserver():
from sqlcoder.serve import app
import uvicorn
uvicorn.run(app, host="localhost", port=1235)
def serve_static():
import http.server
import socketserver
import webbrowser
port = 8002
directory = os.path.join(sqlcoder.__path__[0], "static")
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(
*args,
directory=directory,
**kwargs
)
webbrowser.open(f"http://localhost:{port}")
with socketserver.TCPServer(("", port), Handler) as httpd:
print(f"Static folder is {directory}")
httpd.extension_maps = {".html": "text/html", "": "text/html"}
httpd.serve_forever()
def launch():
home_dir = os.path.expanduser("~")
defog_path = os.path.join(home_dir, ".defog")
if not os.popen("lspci | grep -i nvidia").read():
# not a GPU machine
filepath = os.path.join(home_dir, ".defog", "sqlcoder-7b-q5_k_m.gguf")
if not os.path.exists(filepath):
print(
"Downloading the SQLCoder-7b-2 GGUF file. This is a ~5GB file and may take a long time to download. But once it's downloaded, it will be saved on your machine and you won't have to download it again."
)
hf_hub_download(repo_id="defog/sqlcoder-7b-2", filename="sqlcoder-7b-q5_k_m.gguf", local_dir=defog_path)
else:
# check if the huggingface model is already downloaded from hub. If not, download it
from huggingface_hub import snapshot_download
print(
"Downloading the SQLCoder-7b-2 model. This is a ~14GB file and may take a long time to download. But once it's downloaded, it will be saved on your machine and you won't have to download it again."
)
_ = snapshot_download("defog/sqlcoder-7b-2")
print("Starting SQLCoder server...")
static_process = subprocess.Popen(["sqlcoder", "serve-static"])
print("Serving static server...")
webserver_process = subprocess.Popen(["sqlcoder", "serve-webserver"])
print("Press Ctrl+C to exit.")
try:
while True:
pass
except KeyboardInterrupt:
print("Exiting...")
static_process.terminate()
webserver_process.terminate()
sys.exit(0)
if __name__ == "__main__":
main()