Nafiul Huda commited on
Commit
e8f7730
·
verified ·
1 Parent(s): cfc4da1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -4,6 +4,7 @@ import time
4
  import requests
5
  import json
6
  import zipfile
 
7
  import stat
8
  import gradio as gr
9
  import spaces
@@ -40,9 +41,10 @@ def get_prebuilt_llama():
40
  try:
41
  resp = requests.get(api_url).json()
42
  target_asset = None
 
43
  for asset in resp.get("assets", []):
44
  name = asset["name"].lower()
45
- if "ubuntu-x64" in name and name.endswith(".zip") and not any(x in name for x in ["vulkan", "rocm", "sycl", "openvino", "cuda"]):
46
  target_asset = asset
47
  break
48
 
@@ -50,23 +52,28 @@ def get_prebuilt_llama():
50
  raise ValueError("No matching CPU asset found in latest release.")
51
 
52
  download_url = target_asset["browser_download_url"]
53
- zip_name = target_asset["name"]
54
 
55
  except Exception as e:
56
  print(f"GitHub API check failed ({e}), using fallback direct URL...")
57
- download_url = "https://github.com/ggerganov/llama.cpp/releases/download/b3400/llama-b3400-bin-ubuntu-x64.zip"
58
- zip_name = "llama-fallback.zip"
 
59
 
60
- print(f"Downloading {zip_name} from {download_url}...")
61
  with requests.get(download_url, stream=True) as r:
62
  r.raise_for_status()
63
- with open(zip_name, "wb") as f:
64
  for chunk in r.iter_content(chunk_size=8192):
65
  f.write(chunk)
66
 
67
- print("Extracting zip...")
68
- with zipfile.ZipFile(zip_name, 'r') as zip_ref:
69
- zip_ref.extractall("./llama_extracted")
 
 
 
 
70
 
71
  # Locate the binary inside the extracted folder
72
  found_bin = None
@@ -76,7 +83,7 @@ def get_prebuilt_llama():
76
  break
77
 
78
  if not found_bin:
79
- raise RuntimeError("llama-server binary not found in the extracted zip!")
80
 
81
  # Move it to root and clean up
82
  os.rename(found_bin, server_bin)
@@ -97,20 +104,19 @@ def start_server(model_path, server_bin_path):
97
  "--port", "8080",
98
  "-t", "2",
99
  "--cache-type-k", "q8_0",
100
- "--cache-type-v", "q8_0", # Safest compatibility flag
101
- "-c", "8192", # Safely reduced from 131072 to avoid immediate RAM OOM Kill
102
  "-n", "4096"
103
  ]
104
 
105
  print("Booting local llama-server with command:")
106
  print(" ".join(cmd))
107
 
108
- # By NOT hiding stdout/stderr, C++ logs will now stream beautifully to your Space terminal!
109
  server_process = subprocess.Popen(cmd)
110
 
111
  print("Waiting for llama-server to initialize...")
112
  for _ in range(120):
113
- # Fail-fast: if the process crashed, stop waiting and alert the user
114
  if server_process.poll() is not None:
115
  raise RuntimeError(f"llama-server crashed instantly with return code {server_process.returncode}! Check the C++ logs above for the reason.")
116
 
 
4
  import requests
5
  import json
6
  import zipfile
7
+ import tarfile
8
  import stat
9
  import gradio as gr
10
  import spaces
 
41
  try:
42
  resp = requests.get(api_url).json()
43
  target_asset = None
44
+ # Support both .zip and .tar.gz (Linux recently migrated to tar.gz)
45
  for asset in resp.get("assets", []):
46
  name = asset["name"].lower()
47
+ if "ubuntu-x64" in name and (name.endswith(".zip") or name.endswith(".tar.gz")) and not any(x in name for x in ["vulkan", "rocm", "sycl", "openvino", "cuda"]):
48
  target_asset = asset
49
  break
50
 
 
52
  raise ValueError("No matching CPU asset found in latest release.")
53
 
54
  download_url = target_asset["browser_download_url"]
55
+ archive_name = target_asset["name"]
56
 
57
  except Exception as e:
58
  print(f"GitHub API check failed ({e}), using fallback direct URL...")
59
+ # Updated to a highly recent July 2026 build that supports MiniCPM architecture natively
60
+ download_url = "https://github.com/ggml-org/llama.cpp/releases/download/b9940/llama-b9940-bin-ubuntu-x64.tar.gz"
61
+ archive_name = "llama-fallback.tar.gz"
62
 
63
+ print(f"Downloading {archive_name} from {download_url}...")
64
  with requests.get(download_url, stream=True) as r:
65
  r.raise_for_status()
66
+ with open(archive_name, "wb") as f:
67
  for chunk in r.iter_content(chunk_size=8192):
68
  f.write(chunk)
69
 
70
+ print("Extracting archive...")
71
+ if archive_name.endswith(".zip"):
72
+ with zipfile.ZipFile(archive_name, 'r') as zip_ref:
73
+ zip_ref.extractall("./llama_extracted")
74
+ elif archive_name.endswith(".tar.gz"):
75
+ with tarfile.open(archive_name, "r:gz") as tar_ref:
76
+ tar_ref.extractall("./llama_extracted")
77
 
78
  # Locate the binary inside the extracted folder
79
  found_bin = None
 
83
  break
84
 
85
  if not found_bin:
86
+ raise RuntimeError("llama-server binary not found in the extracted files!")
87
 
88
  # Move it to root and clean up
89
  os.rename(found_bin, server_bin)
 
104
  "--port", "8080",
105
  "-t", "2",
106
  "--cache-type-k", "q8_0",
107
+ "--cache-type-v", "q8_0",
108
+ "-c", "8192",
109
  "-n", "4096"
110
  ]
111
 
112
  print("Booting local llama-server with command:")
113
  print(" ".join(cmd))
114
 
 
115
  server_process = subprocess.Popen(cmd)
116
 
117
  print("Waiting for llama-server to initialize...")
118
  for _ in range(120):
119
+ # Fail-fast check
120
  if server_process.poll() is not None:
121
  raise RuntimeError(f"llama-server crashed instantly with return code {server_process.returncode}! Check the C++ logs above for the reason.")
122