Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,44 +15,49 @@ REPO_ID = "huzpsb/heru"
|
|
| 15 |
FILENAME = "qwq_q4k.gguf"
|
| 16 |
|
| 17 |
def setup_server():
|
| 18 |
-
"""下载并启动 llama-server,处理动态链接库"""
|
| 19 |
-
# 1. 下载模型
|
| 20 |
print(f"[*] Downloading model: {FILENAME}...")
|
| 21 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 22 |
|
| 23 |
-
# 2. 下载并完整解压 llama.cpp binary
|
| 24 |
if not os.path.exists(BINARY_NAME):
|
| 25 |
print("[*] Downloading llama.cpp binary package...")
|
| 26 |
response = requests.get(LLAMA_CPP_RELEASE_URL, stream=True)
|
| 27 |
with open("llama.tar.gz", "wb") as f:
|
| 28 |
f.write(response.content)
|
| 29 |
-
|
| 30 |
-
print("[*] Extracting
|
| 31 |
with tarfile.open("llama.tar.gz", "r:gz") as tar:
|
| 32 |
-
# 找到包含二进制文件的目录(通常在 build/bin/ 下)
|
| 33 |
-
# 我们直接把所有文件提取到当前目录,简化路径处理
|
| 34 |
for member in tar.getmembers():
|
|
|
|
|
|
|
|
|
|
| 35 |
if member.isfile():
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
member.name = os.path.basename(member.name)
|
| 39 |
tar.extract(member, path=".")
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if os.path.exists(BINARY_NAME):
|
| 43 |
os.chmod(BINARY_NAME, 0o755)
|
| 44 |
else:
|
| 45 |
raise Exception(f"Could not find {BINARY_NAME} in the extracted files.")
|
| 46 |
|
| 47 |
-
# 3. 启动后台进程
|
| 48 |
print("[*] Starting llama-server with LD_LIBRARY_PATH...")
|
| 49 |
-
|
| 50 |
-
# 关键修改:设置环境变量,让系统在当前目录 (.) 查找 .so 库文件
|
| 51 |
new_env = os.environ.copy()
|
| 52 |
current_dir = os.getcwd()
|
| 53 |
-
#
|
| 54 |
new_env["LD_LIBRARY_PATH"] = f"{current_dir}:{new_env.get('LD_LIBRARY_PATH', '')}"
|
| 55 |
-
|
| 56 |
cmd = [
|
| 57 |
f"./{BINARY_NAME}",
|
| 58 |
"-m", model_path,
|
|
@@ -62,18 +67,16 @@ def setup_server():
|
|
| 62 |
"--host", "127.0.0.1"
|
| 63 |
]
|
| 64 |
|
| 65 |
-
# 传入 env=new_env
|
| 66 |
proc = subprocess.Popen(
|
| 67 |
cmd,
|
| 68 |
stdout=sys.stdout,
|
| 69 |
stderr=sys.stderr,
|
| 70 |
env=new_env
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
# 4. 健康检查
|
| 74 |
print("[*] Waiting for server to respond...")
|
| 75 |
retries = 0
|
| 76 |
-
while retries < 60:
|
| 77 |
try:
|
| 78 |
r = requests.get(f"http://127.0.0.1:{SERVER_PORT}/health")
|
| 79 |
if r.status_code == 200:
|
|
@@ -82,10 +85,8 @@ def setup_server():
|
|
| 82 |
except:
|
| 83 |
time.sleep(2)
|
| 84 |
retries += 1
|
| 85 |
-
if retries % 5 == 0:
|
| 86 |
-
print(f"[*] Still waiting... ({retries}/60)")
|
| 87 |
|
| 88 |
-
raise Exception("Server failed to start. Check logs
|
| 89 |
|
| 90 |
# 初始化
|
| 91 |
server_process = setup_server()
|
|
|
|
| 15 |
FILENAME = "qwq_q4k.gguf"
|
| 16 |
|
| 17 |
def setup_server():
|
| 18 |
+
"""下载并启动 llama-server,处理动态链接库及符号链接"""
|
|
|
|
| 19 |
print(f"[*] Downloading model: {FILENAME}...")
|
| 20 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 21 |
|
|
|
|
| 22 |
if not os.path.exists(BINARY_NAME):
|
| 23 |
print("[*] Downloading llama.cpp binary package...")
|
| 24 |
response = requests.get(LLAMA_CPP_RELEASE_URL, stream=True)
|
| 25 |
with open("llama.tar.gz", "wb") as f:
|
| 26 |
f.write(response.content)
|
| 27 |
+
|
| 28 |
+
print("[*] Extracting files and handling symlinks...")
|
| 29 |
with tarfile.open("llama.tar.gz", "r:gz") as tar:
|
|
|
|
|
|
|
| 30 |
for member in tar.getmembers():
|
| 31 |
+
base_name = os.path.basename(member.name)
|
| 32 |
+
if not base_name: continue # 跳过目录本身
|
| 33 |
+
|
| 34 |
if member.isfile():
|
| 35 |
+
# 提取普通文件并去除路径前缀
|
| 36 |
+
member.name = base_name
|
|
|
|
| 37 |
tar.extract(member, path=".")
|
| 38 |
+
elif member.issym():
|
| 39 |
+
# 处理符号链接
|
| 40 |
+
link_target = os.path.basename(member.linkname)
|
| 41 |
+
# 如果链接已存在,先删除
|
| 42 |
+
if os.path.lexists(base_name):
|
| 43 |
+
os.remove(base_name)
|
| 44 |
+
try:
|
| 45 |
+
os.symlink(link_target, base_name)
|
| 46 |
+
print(f"[*] Created symlink: {base_name} -> {link_target}")
|
| 47 |
+
except OSError as e:
|
| 48 |
+
print(f"[!] Failed to create symlink {base_name}: {e}")
|
| 49 |
+
|
| 50 |
if os.path.exists(BINARY_NAME):
|
| 51 |
os.chmod(BINARY_NAME, 0o755)
|
| 52 |
else:
|
| 53 |
raise Exception(f"Could not find {BINARY_NAME} in the extracted files.")
|
| 54 |
|
|
|
|
| 55 |
print("[*] Starting llama-server with LD_LIBRARY_PATH...")
|
|
|
|
|
|
|
| 56 |
new_env = os.environ.copy()
|
| 57 |
current_dir = os.getcwd()
|
| 58 |
+
# 确保 LD_LIBRARY_PATH 包含当前目录,以便加载 .so 文件
|
| 59 |
new_env["LD_LIBRARY_PATH"] = f"{current_dir}:{new_env.get('LD_LIBRARY_PATH', '')}"
|
| 60 |
+
|
| 61 |
cmd = [
|
| 62 |
f"./{BINARY_NAME}",
|
| 63 |
"-m", model_path,
|
|
|
|
| 67 |
"--host", "127.0.0.1"
|
| 68 |
]
|
| 69 |
|
|
|
|
| 70 |
proc = subprocess.Popen(
|
| 71 |
cmd,
|
| 72 |
stdout=sys.stdout,
|
| 73 |
stderr=sys.stderr,
|
| 74 |
env=new_env
|
| 75 |
)
|
| 76 |
+
|
|
|
|
| 77 |
print("[*] Waiting for server to respond...")
|
| 78 |
retries = 0
|
| 79 |
+
while retries < 60:
|
| 80 |
try:
|
| 81 |
r = requests.get(f"http://127.0.0.1:{SERVER_PORT}/health")
|
| 82 |
if r.status_code == 200:
|
|
|
|
| 85 |
except:
|
| 86 |
time.sleep(2)
|
| 87 |
retries += 1
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
raise Exception("Server failed to start. Check logs for missing .so files.")
|
| 90 |
|
| 91 |
# 初始化
|
| 92 |
server_process = setup_server()
|