Spaces:
Running on Zero
Running on Zero
Create app.py
Browse files
app.py
CHANGED
|
@@ -66,7 +66,8 @@ def build_latest_llama_server():
|
|
| 66 |
"-DCMAKE_BUILD_TYPE=Release",
|
| 67 |
"-DGGML_NATIVE=ON",
|
| 68 |
"-DGGML_OPENMP=ON",
|
| 69 |
-
"-DGGML_AVX512=
|
|
|
|
| 70 |
]
|
| 71 |
|
| 72 |
subprocess.run(cmake_cmd, cwd="llama.cpp", env=env, check=True)
|
|
@@ -120,14 +121,16 @@ def start_llama_server():
|
|
| 120 |
env = os.environ.copy()
|
| 121 |
env["LD_LIBRARY_PATH"] = f"{binary_dir}:{env.get('LD_LIBRARY_PATH', '')}"
|
| 122 |
|
|
|
|
|
|
|
| 123 |
cmd = [
|
| 124 |
binary_path,
|
| 125 |
"-m", model_path,
|
| 126 |
"-mm", mmproj_path,
|
| 127 |
"--port", "8000",
|
| 128 |
"--host", "127.0.0.1",
|
| 129 |
-
"-t",
|
| 130 |
-
"-tb",
|
| 131 |
"-fa", "on",
|
| 132 |
"--parallel", "1",
|
| 133 |
"--load-mode", "dio",
|
|
@@ -145,10 +148,41 @@ def start_llama_server():
|
|
| 145 |
|
| 146 |
process = subprocess.Popen(cmd, env=env)
|
| 147 |
_llama_server_started = True
|
| 148 |
-
time.sleep(5)
|
| 149 |
return process
|
| 150 |
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
@app.middleware("http")
|
| 153 |
async def proxy_middleware(request: Request, call_next):
|
| 154 |
path = request.url.path
|
|
@@ -199,5 +233,6 @@ async def proxy_middleware(request: Request, call_next):
|
|
| 199 |
|
| 200 |
|
| 201 |
if __name__ == "__main__":
|
| 202 |
-
start_llama_server()
|
|
|
|
| 203 |
app.launch(show_error=True)
|
|
|
|
| 66 |
"-DCMAKE_BUILD_TYPE=Release",
|
| 67 |
"-DGGML_NATIVE=ON",
|
| 68 |
"-DGGML_OPENMP=ON",
|
| 69 |
+
"-DGGML_AVX512=ON",
|
| 70 |
+
"-DGGML_AMX=OFF",
|
| 71 |
]
|
| 72 |
|
| 73 |
subprocess.run(cmake_cmd, cwd="llama.cpp", env=env, check=True)
|
|
|
|
| 121 |
env = os.environ.copy()
|
| 122 |
env["LD_LIBRARY_PATH"] = f"{binary_dir}:{env.get('LD_LIBRARY_PATH', '')}"
|
| 123 |
|
| 124 |
+
n_threads = str(os.cpu_count() or 2)
|
| 125 |
+
|
| 126 |
cmd = [
|
| 127 |
binary_path,
|
| 128 |
"-m", model_path,
|
| 129 |
"-mm", mmproj_path,
|
| 130 |
"--port", "8000",
|
| 131 |
"--host", "127.0.0.1",
|
| 132 |
+
"-t", n_threads,
|
| 133 |
+
"-tb", n_threads,
|
| 134 |
"-fa", "on",
|
| 135 |
"--parallel", "1",
|
| 136 |
"--load-mode", "dio",
|
|
|
|
| 148 |
|
| 149 |
process = subprocess.Popen(cmd, env=env)
|
| 150 |
_llama_server_started = True
|
|
|
|
| 151 |
return process
|
| 152 |
|
| 153 |
|
| 154 |
+
def wait_for_llama_server(process, poll_interval: float = 3.0, log_every: float = 30.0):
|
| 155 |
+
"""Block until llama-server answers on /health, instead of guessing with a
|
| 156 |
+
fixed sleep. This is what the old code was missing: app.launch() used to
|
| 157 |
+
run ~5s after starting llama-server no matter what, so Gradio started
|
| 158 |
+
accepting traffic (and the proxy started forwarding to it) long before a
|
| 159 |
+
multi-GB / multi-shard model had actually finished loading on CPU - which
|
| 160 |
+
is exactly what produced "Proxy Error: All connection attempts failed".
|
| 161 |
+
There is no hard timeout here on purpose: a large CPU-only model can
|
| 162 |
+
legitimately take several minutes to mmap and load."""
|
| 163 |
+
print("Waiting for llama-server to finish loading the model before accepting traffic...")
|
| 164 |
+
waited = 0.0
|
| 165 |
+
last_log = 0.0
|
| 166 |
+
while True:
|
| 167 |
+
if process is not None and process.poll() is not None:
|
| 168 |
+
raise RuntimeError(
|
| 169 |
+
f"llama-server exited unexpectedly (return code {process.returncode}) "
|
| 170 |
+
"while loading the model - scroll up in the Container logs for the real error."
|
| 171 |
+
)
|
| 172 |
+
try:
|
| 173 |
+
r = httpx.get("http://127.0.0.1:8000/health", timeout=5)
|
| 174 |
+
if r.status_code < 400:
|
| 175 |
+
print(f"llama-server is ready after ~{waited:.0f}s.")
|
| 176 |
+
return
|
| 177 |
+
except httpx.HTTPError:
|
| 178 |
+
pass
|
| 179 |
+
time.sleep(poll_interval)
|
| 180 |
+
waited += poll_interval
|
| 181 |
+
if waited - last_log >= log_every:
|
| 182 |
+
print(f"...still loading ({waited:.0f}s elapsed)")
|
| 183 |
+
last_log = waited
|
| 184 |
+
|
| 185 |
+
|
| 186 |
@app.middleware("http")
|
| 187 |
async def proxy_middleware(request: Request, call_next):
|
| 188 |
path = request.url.path
|
|
|
|
| 233 |
|
| 234 |
|
| 235 |
if __name__ == "__main__":
|
| 236 |
+
llama_process = start_llama_server()
|
| 237 |
+
wait_for_llama_server(llama_process)
|
| 238 |
app.launch(show_error=True)
|