Spaces:
Runtime error
Runtime error
Update app_utils.py
Browse files- app_utils.py +28 -21
app_utils.py
CHANGED
|
@@ -1,34 +1,41 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
from pathlib import Path
|
| 4 |
from ctransformers import AutoModelForCausalLM
|
| 5 |
-
import requests
|
| 6 |
|
| 7 |
MODEL_URL = "https://huggingface.co/google/gemma-2b-it-GGUF/resolve/main/gemma-2b-it.Q4_K_M.gguf"
|
| 8 |
MODEL_PATH = Path("models/gemma-2b-it.Q4_K_M.gguf")
|
| 9 |
-
MODEL_TYPE = "gemma"
|
| 10 |
|
| 11 |
-
# Download model if not already present
|
| 12 |
def download_model():
|
| 13 |
-
if
|
| 14 |
-
print("
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def load_model():
|
| 25 |
download_model()
|
|
|
|
| 26 |
return AutoModelForCausalLM.from_pretrained(
|
| 27 |
-
MODEL_PATH,
|
| 28 |
-
model_type=
|
| 29 |
-
|
| 30 |
-
temperature=0.7,
|
| 31 |
-
repetition_penalty=1.1
|
| 32 |
)
|
| 33 |
|
| 34 |
llm = load_model()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
from pathlib import Path
|
| 4 |
from ctransformers import AutoModelForCausalLM
|
|
|
|
| 5 |
|
| 6 |
MODEL_URL = "https://huggingface.co/google/gemma-2b-it-GGUF/resolve/main/gemma-2b-it.Q4_K_M.gguf"
|
| 7 |
MODEL_PATH = Path("models/gemma-2b-it.Q4_K_M.gguf")
|
|
|
|
| 8 |
|
|
|
|
| 9 |
def download_model():
|
| 10 |
+
if MODEL_PATH.exists():
|
| 11 |
+
print("β
Model already exists.")
|
| 12 |
+
return
|
| 13 |
+
|
| 14 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
+
if not HF_TOKEN:
|
| 16 |
+
raise ValueError("β HF_TOKEN environment variable not found. Set it in your Space Secrets.")
|
| 17 |
+
|
| 18 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 19 |
+
print("π Downloading Gemma model...")
|
| 20 |
+
|
| 21 |
+
response = requests.get(MODEL_URL, headers=headers, stream=True)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
|
| 24 |
+
MODEL_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 25 |
+
with open(MODEL_PATH, "wb") as f:
|
| 26 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 27 |
+
if chunk:
|
| 28 |
+
f.write(chunk)
|
| 29 |
+
|
| 30 |
+
print("β
Model download complete.")
|
| 31 |
+
|
| 32 |
def load_model():
|
| 33 |
download_model()
|
| 34 |
+
print("π Loading Gemma model with ctransformers...")
|
| 35 |
return AutoModelForCausalLM.from_pretrained(
|
| 36 |
+
str(MODEL_PATH),
|
| 37 |
+
model_type="gemma",
|
| 38 |
+
gpu_layers=0 # For CPU inference
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
llm = load_model()
|