Spaces:
Running
Running
Upload 2 files
Browse files- app.py +1 -1
- model_manager.py +20 -14
app.py
CHANGED
|
@@ -52,7 +52,7 @@ with st.sidebar:
|
|
| 52 |
|
| 53 |
if st.button("🚀 Initialize Engine"):
|
| 54 |
manager = BitNetManager()
|
| 55 |
-
if manager.setup_engine():
|
| 56 |
st.session_state.engine_ready = True
|
| 57 |
st.success("BitNet.cpp Ready!")
|
| 58 |
else:
|
|
|
|
| 52 |
|
| 53 |
if st.button("🚀 Initialize Engine"):
|
| 54 |
manager = BitNetManager()
|
| 55 |
+
if manager.setup_engine(model_id=model_id):
|
| 56 |
st.session_state.engine_ready = True
|
| 57 |
st.success("BitNet.cpp Ready!")
|
| 58 |
else:
|
model_manager.py
CHANGED
|
@@ -57,13 +57,19 @@ class BitNetManager:
|
|
| 57 |
f.write(patched_setup)
|
| 58 |
st.success("Patch applied to setup_env.py (unquoted)!")
|
| 59 |
|
| 60 |
-
def setup_engine(self):
|
| 61 |
"""Clone and compile utilizing official setup_env.py with log streaming."""
|
| 62 |
-
|
|
|
|
| 63 |
binary = self.get_binary_path()
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
return True
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if not os.path.exists(self.bitnet_dir):
|
| 69 |
st.info("Cloning BitNet repository...")
|
|
@@ -129,19 +135,19 @@ class BitNetManager:
|
|
| 129 |
return None
|
| 130 |
|
| 131 |
def download_model(self, model_id="1bitLLM/bitnet_b1_58-3B", filename="ggml-model-i2_s.gguf"):
|
| 132 |
-
"""
|
| 133 |
-
# setup_env.py
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
if os.path.exists(local_model_path):
|
|
|
|
| 136 |
return local_model_path
|
| 137 |
|
| 138 |
-
st.
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
return path
|
| 142 |
-
except Exception as e:
|
| 143 |
-
st.error(f"Model download failed: {e}")
|
| 144 |
-
return None
|
| 145 |
|
| 146 |
def run_inference(self, prompt, model_path):
|
| 147 |
"""Execute the bitnet binary with the provided prompt."""
|
|
|
|
| 57 |
f.write(patched_setup)
|
| 58 |
st.success("Patch applied to setup_env.py (unquoted)!")
|
| 59 |
|
| 60 |
+
def setup_engine(self, model_id="1bitLLM/bitnet_b1_58-3B"):
|
| 61 |
"""Clone and compile utilizing official setup_env.py with log streaming."""
|
| 62 |
+
model_name = model_id.split("/")[-1]
|
| 63 |
+
model_path = os.path.join(self.bitnet_dir, "models", model_name, "ggml-model-i2_s.gguf")
|
| 64 |
binary = self.get_binary_path()
|
| 65 |
+
|
| 66 |
+
# Check if already compiled AND model exists
|
| 67 |
+
if binary and os.path.exists(binary) and os.path.exists(model_path):
|
| 68 |
+
st.success(f"BitNet engine and model ({model_name}) are ready!")
|
| 69 |
return True
|
| 70 |
+
|
| 71 |
+
if binary and os.path.exists(binary):
|
| 72 |
+
st.info(f"Engine binary found, but model weights for {model_name} are missing. Starting setup...")
|
| 73 |
|
| 74 |
if not os.path.exists(self.bitnet_dir):
|
| 75 |
st.info("Cloning BitNet repository...")
|
|
|
|
| 135 |
return None
|
| 136 |
|
| 137 |
def download_model(self, model_id="1bitLLM/bitnet_b1_58-3B", filename="ggml-model-i2_s.gguf"):
|
| 138 |
+
"""Locate the model weights. These are generated locally by setup_env.py."""
|
| 139 |
+
# setup_env.py downloads weights to models/<model_name>/
|
| 140 |
+
# e.g. models/bitnet_b1_58-3B/
|
| 141 |
+
model_name = model_id.split("/")[-1]
|
| 142 |
+
local_model_path = os.path.join(self.bitnet_dir, "models", model_name, filename)
|
| 143 |
+
|
| 144 |
if os.path.exists(local_model_path):
|
| 145 |
+
st.success(f"Found local model: {model_name}")
|
| 146 |
return local_model_path
|
| 147 |
|
| 148 |
+
st.error(f"Model file not found at {local_model_path}")
|
| 149 |
+
st.info("The GGUF model must be generated by the 'Initialize Engine' process. Please run it again to download and convert the weights.")
|
| 150 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
def run_inference(self, prompt, model_path):
|
| 153 |
"""Execute the bitnet binary with the provided prompt."""
|