Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,49 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
-
#
|
| 6 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
model_path = hf_hub_download(
|
| 11 |
-
repo_id="BugTraceAI/BugTraceAI-Apex-G4-26B-Q4",
|
| 12 |
-
filename="BugTraceAI-Apex-G4-26B-Q4.gguf"
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
print(
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model_path=
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
)
|
| 33 |
-
return output["choices"][0]["text"]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from llama_cpp import Llama
|
| 5 |
|
| 6 |
+
# 1. Faster downloads
|
| 7 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 8 |
|
| 9 |
+
# 2. Define local paths
|
| 10 |
+
CACHE_DIR = os.path.join(os.getcwd(), "model_cache")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
print("--- Starting AI Sandbox ---")
|
| 13 |
+
try:
|
| 14 |
+
print("--- Downloading 26B Model (16.7GB) to local cache ---")
|
| 15 |
+
model_path = hf_hub_download(
|
| 16 |
+
repo_id="BugTraceAI/BugTraceAI-Apex-G4-26B-Q4",
|
| 17 |
+
filename="BugTraceAI-Apex-G4-26B-Q4.gguf",
|
| 18 |
+
cache_dir=CACHE_DIR
|
| 19 |
+
)
|
|
|
|
| 20 |
|
| 21 |
+
print(f"--- Loading Model: {model_path} ---")
|
| 22 |
+
# Low-RAM configuration for Free Tier
|
| 23 |
+
llm = Llama(
|
| 24 |
+
model_path=model_path,
|
| 25 |
+
n_ctx=2048,
|
| 26 |
+
n_threads=2,
|
| 27 |
+
use_mmap=True, # Critical for large models on 16GB RAM
|
| 28 |
+
n_gpu_layers=0 # CPU Only
|
| 29 |
)
|
|
|
|
| 30 |
|
| 31 |
+
def chat(prompt):
|
| 32 |
+
output = llm(
|
| 33 |
+
f"User: {prompt}\nAssistant:",
|
| 34 |
+
max_tokens=256,
|
| 35 |
+
stop=["User:", "\n"],
|
| 36 |
+
echo=False
|
| 37 |
+
)
|
| 38 |
+
return output["choices"][0]["text"]
|
| 39 |
+
|
| 40 |
+
print("\n✅ Sandbox Ready!")
|
| 41 |
+
print("To chat, use the logs terminal or call: chat('your prompt')")
|
| 42 |
+
|
| 43 |
+
# Keeps the container alive and interactive
|
| 44 |
+
import IPython
|
| 45 |
+
IPython.embed()
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"❌ Error during startup: {e}")
|
| 49 |
+
sys.exit(1)
|