Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
import struct
|
| 3 |
import gradio as gr
|
| 4 |
-
from llama_cpp import Llama
|
| 5 |
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
| 6 |
from huggingface_hub import hf_hub_download, login
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
SOURCE_REPO = "metanthropic/metanthropic-phi3-encrypted"
|
| 10 |
SOURCE_FILENAME = "metanthropic-phi3-v1.mguf"
|
| 11 |
-
TEMP_DECRYPTED = "/tmp/
|
| 12 |
-
|
| 13 |
-
# Secrets
|
| 14 |
SECRET_KEY_HEX = os.environ.get("DECRYPTION_KEY")
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
if os.path.exists(TEMP_DECRYPTED):
|
| 21 |
-
print("β‘ [CACHE] Model found locally.")
|
| 22 |
-
return
|
| 23 |
-
|
| 24 |
-
# Authenticate
|
| 25 |
-
if HF_TOKEN:
|
| 26 |
-
print("π Authenticating...")
|
| 27 |
-
login(token=HF_TOKEN)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
filename=SOURCE_FILENAME,
|
| 35 |
-
local_dir="."
|
| 36 |
-
)
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print(f"β Download Failed: {e}")
|
| 39 |
-
raise e
|
| 40 |
-
|
| 41 |
-
# Decrypt
|
| 42 |
-
if not SECRET_KEY_HEX:
|
| 43 |
-
raise ValueError("β DECRYPTION_KEY not set in Settings.")
|
| 44 |
-
|
| 45 |
-
print("π [SECURITY] Decrypting...")
|
| 46 |
key = bytes.fromhex(SECRET_KEY_HEX)
|
| 47 |
-
|
| 48 |
|
| 49 |
-
with open(
|
| 50 |
nonce = f_in.read(12)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
f_out.write(aesgcm.decrypt(nonce, encrypted_header, None))
|
| 56 |
-
|
| 57 |
-
# Stream Body
|
| 58 |
-
while True:
|
| 59 |
-
chunk = f_in.read(1024 * 1024 * 64) # 64MB chunks
|
| 60 |
-
if not chunk: break
|
| 61 |
-
f_out.write(chunk)
|
| 62 |
-
|
| 63 |
-
print("β
[SUCCESS] Model Ready.")
|
| 64 |
|
| 65 |
-
# --- LOAD ---
|
| 66 |
llm = None
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
llm = Llama(
|
| 70 |
-
model_path=TEMP_DECRYPTED,
|
| 71 |
-
n_ctx=2048,
|
| 72 |
-
n_threads=2
|
| 73 |
-
)
|
| 74 |
except Exception as e:
|
| 75 |
-
print(f"β
|
| 76 |
-
|
| 77 |
-
# --- UI ---
|
| 78 |
-
def chat(message, history):
|
| 79 |
-
if not llm: return "System Error: Model not loaded."
|
| 80 |
-
|
| 81 |
-
prompt = f"<|user|>\n{message}<|end|>\n<|assistant|>"
|
| 82 |
-
output = llm(prompt, max_tokens=512, stop=["<|end|>"], echo=False)
|
| 83 |
-
return output['choices'][0]['text'].strip()
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import struct
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
| 5 |
from huggingface_hub import hf_hub_download, login
|
| 6 |
|
| 7 |
+
# CRITICAL IMPORT: We do this inside a try block to catch the error early
|
| 8 |
+
try:
|
| 9 |
+
from llama_cpp import Llama
|
| 10 |
+
print("β
Llama-CPP Loaded Successfully.")
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"β Llama-CPP Load Failed: {e}")
|
| 13 |
+
|
| 14 |
+
# --- CONFIG ---
|
| 15 |
SOURCE_REPO = "metanthropic/metanthropic-phi3-encrypted"
|
| 16 |
SOURCE_FILENAME = "metanthropic-phi3-v1.mguf"
|
| 17 |
+
TEMP_DECRYPTED = "/tmp/model.gguf"
|
|
|
|
|
|
|
| 18 |
SECRET_KEY_HEX = os.environ.get("DECRYPTION_KEY")
|
| 19 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 20 |
|
| 21 |
+
def unlock():
|
| 22 |
+
if os.path.exists(TEMP_DECRYPTED): return
|
| 23 |
+
print(f"β¬οΈ Fetching {SOURCE_FILENAME}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
if HF_TOKEN: login(token=HF_TOKEN)
|
| 26 |
+
|
| 27 |
+
path = hf_hub_download(repo_id=SOURCE_REPO, filename=SOURCE_FILENAME)
|
| 28 |
+
|
| 29 |
+
print("π Decrypting...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
key = bytes.fromhex(SECRET_KEY_HEX)
|
| 31 |
+
aes = AESGCM(key)
|
| 32 |
|
| 33 |
+
with open(path, "rb") as f_in, open(TEMP_DECRYPTED, "wb") as f_out:
|
| 34 |
nonce = f_in.read(12)
|
| 35 |
+
h_len = struct.unpack("<I", f_in.read(4))[0]
|
| 36 |
+
f_out.write(aes.decrypt(nonce, f_in.read(h_len), None))
|
| 37 |
+
while chunk := f_in.read(64*1024*1024): f_out.write(chunk)
|
| 38 |
+
print("β
Ready.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
| 40 |
llm = None
|
| 41 |
try:
|
| 42 |
+
unlock()
|
| 43 |
+
llm = Llama(model_path=TEMP_DECRYPTED, n_ctx=2048, n_threads=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
+
print(f"β Boot Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
def chat(msg, history):
|
| 48 |
+
if not llm: return "System offline."
|
| 49 |
+
prompt = f"<|user|>\n{msg}<|end|>\n<|assistant|>"
|
| 50 |
+
return llm(prompt, max_tokens=512, stop=["<|end|>"])['choices'][0]['text'].strip()
|
| 51 |
|
| 52 |
+
gr.ChatInterface(chat).launch(server_name="0.0.0.0", server_port=7860)
|
|
|