Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ def log_status(msg):
|
|
| 17 |
# --- CONFIGURATION ---
|
| 18 |
SOURCE_REPO = "metanthropic/metanthropic-phi3-encrypted"
|
| 19 |
SOURCE_FILE = "metanthropic-phi3-v1.mguf"
|
| 20 |
-
TEMP_DECRYPTED = "/tmp/
|
| 21 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 22 |
SECRET_KEY_HEX = os.environ.get("DECRYPTION_KEY")
|
| 23 |
|
|
@@ -25,20 +25,17 @@ SECRET_KEY_HEX = os.environ.get("DECRYPTION_KEY")
|
|
| 25 |
def initialize_weights():
|
| 26 |
try:
|
| 27 |
if os.path.exists(TEMP_DECRYPTED):
|
| 28 |
-
log_status("β‘ [CACHE] Resuming from
|
| 29 |
return True
|
| 30 |
|
| 31 |
if not HF_TOKEN or not SECRET_KEY_HEX:
|
| 32 |
-
log_status("β [SECURITY] Credentials missing.
|
| 33 |
return False
|
| 34 |
|
| 35 |
-
log_status("π [AUTH] Establishing secure link to Hugging Face...")
|
| 36 |
login(token=HF_TOKEN)
|
| 37 |
-
|
| 38 |
-
log_status(f"β¬οΈ [NETWORK] Fetching {SOURCE_FILE}...")
|
| 39 |
path = hf_hub_download(repo_id=SOURCE_REPO, filename=SOURCE_FILE, local_dir=".")
|
| 40 |
|
| 41 |
-
log_status("π [DECRYPT] Unlocking
|
| 42 |
key = bytes.fromhex(SECRET_KEY_HEX)
|
| 43 |
aes = AESGCM(key)
|
| 44 |
|
|
@@ -50,60 +47,50 @@ def initialize_weights():
|
|
| 50 |
f_out.write(chunk)
|
| 51 |
|
| 52 |
os.remove(path)
|
| 53 |
-
log_status("β
[SYSTEM]
|
| 54 |
return True
|
| 55 |
except Exception as e:
|
| 56 |
-
log_status(f"β [
|
| 57 |
-
log_status(traceback.format_exc())
|
| 58 |
return False
|
| 59 |
|
| 60 |
-
# --- ENGINE
|
| 61 |
llm = None
|
| 62 |
if initialize_weights():
|
| 63 |
try:
|
| 64 |
-
log_status("π§ [ENGINE] Initializing
|
| 65 |
llm = Llama(
|
| 66 |
model_path=TEMP_DECRYPTED,
|
| 67 |
n_ctx=2048,
|
| 68 |
-
n_threads=2,
|
| 69 |
-
n_batch=512,
|
| 70 |
-
use_mlock=True,
|
| 71 |
verbose=False
|
| 72 |
)
|
| 73 |
-
log_status("π [SYSTEM]
|
| 74 |
except Exception as e:
|
| 75 |
-
log_status(f"β [ENGINE ERROR]
|
| 76 |
|
| 77 |
-
# ---
|
| 78 |
app = FastAPI()
|
| 79 |
|
| 80 |
@app.post("/run_inference")
|
| 81 |
async def run_inference(request: Request):
|
| 82 |
-
if not llm:
|
| 83 |
-
return {"error": "System Offline", "logs": DIAGNOSTIC_LOG[-5:]}
|
| 84 |
-
|
| 85 |
data = await request.json()
|
| 86 |
-
|
| 87 |
-
# π SECURITY HANDSHAKE
|
| 88 |
-
# We use the SECRET_KEY_HEX as a shared secret for API authorization.
|
| 89 |
if data.get("secretKey") != SECRET_KEY_HEX:
|
| 90 |
-
return {"error": "Unauthorized
|
| 91 |
|
| 92 |
prompt = data.get("prompt", "")
|
| 93 |
-
|
| 94 |
-
output = llm(
|
| 95 |
-
f"<|user|>\n{prompt}<|end|>\n<|assistant|>",
|
| 96 |
-
max_tokens=512,
|
| 97 |
-
stop=["<|end|>", "<|endoftext|>"]
|
| 98 |
-
)
|
| 99 |
return {"response": output['choices'][0]['text'].strip()}
|
| 100 |
|
| 101 |
-
# ---
|
| 102 |
def ui_chat(msg, hist):
|
| 103 |
if not llm:
|
| 104 |
-
yield
|
| 105 |
return
|
| 106 |
|
|
|
|
| 107 |
stream = llm(
|
| 108 |
f"<|user|>\n{msg}<|end|>\n<|assistant|>",
|
| 109 |
max_tokens=512,
|
|
@@ -113,48 +100,20 @@ def ui_chat(msg, hist):
|
|
| 113 |
|
| 114 |
partial_text = ""
|
| 115 |
for chunk in stream:
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
yield partial_text
|
| 120 |
|
| 121 |
-
# ---
|
| 122 |
-
custom_css = ""
|
| 123 |
-
footer {visibility: hidden}
|
| 124 |
-
.gradio-container {background-color: #050505 !important}
|
| 125 |
-
#title-container {text-align: center; margin-bottom: 30px}
|
| 126 |
-
#title-container h1 {color: #ffffff; font-family: 'Inter', sans-serif; font-weight: 800; letter-spacing: -1.5px}
|
| 127 |
-
.message.user {background-color: #1a1a1a !important; border: 1px solid #333 !important; border-radius: 12px !important}
|
| 128 |
-
.message.assistant {background-color: #0f0f0f !important; border: 1px solid #222 !important; border-radius: 12px !important}
|
| 129 |
-
"""
|
| 130 |
|
| 131 |
demo = gr.ChatInterface(
|
| 132 |
ui_chat,
|
| 133 |
title="METANTHROPIC Β· PHI-3 SOVEREIGN",
|
| 134 |
-
description=""
|
| 135 |
-
|
| 136 |
-
<p style="color: #a3a3a3; font-size: 1.1em; max-width: 600px; margin: 0 auto;">
|
| 137 |
-
Accessing <b>Node-01</b> of the Metanthropic Neural Infrastructure.
|
| 138 |
-
Secure inference via localized sovereign weights.
|
| 139 |
-
</p>
|
| 140 |
-
<div style="margin-top: 15px; display: flex; justify-content: center; gap: 20px;">
|
| 141 |
-
<span style="color: #22c55e; font-size: 0.85em; font-family: monospace;">β ENGINE: READY</span>
|
| 142 |
-
<span style="color: #3b82f6; font-size: 0.85em; font-family: monospace;">β ENCRYPTION: AES-GCM</span>
|
| 143 |
-
<span style="color: #a855f7; font-size: 0.85em; font-family: monospace;">β TYPE: STREAMING</span>
|
| 144 |
-
</div>
|
| 145 |
-
</div>
|
| 146 |
-
""",
|
| 147 |
-
theme=gr.themes.Soft(
|
| 148 |
-
primary_hue="slate",
|
| 149 |
-
neutral_hue="zinc",
|
| 150 |
-
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"],
|
| 151 |
-
).set(
|
| 152 |
-
body_background_fill="#050505",
|
| 153 |
-
block_background_fill="#0a0a0a",
|
| 154 |
-
block_border_width="1px",
|
| 155 |
-
button_primary_background_fill="#ffffff",
|
| 156 |
-
button_primary_text_color="#000000",
|
| 157 |
-
),
|
| 158 |
css=custom_css
|
| 159 |
)
|
| 160 |
|
|
|
|
| 17 |
# --- CONFIGURATION ---
|
| 18 |
SOURCE_REPO = "metanthropic/metanthropic-phi3-encrypted"
|
| 19 |
SOURCE_FILE = "metanthropic-phi3-v1.mguf"
|
| 20 |
+
TEMP_DECRYPTED = "/tmp/model_sovereign_v2.gguf"
|
| 21 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 22 |
SECRET_KEY_HEX = os.environ.get("DECRYPTION_KEY")
|
| 23 |
|
|
|
|
| 25 |
def initialize_weights():
|
| 26 |
try:
|
| 27 |
if os.path.exists(TEMP_DECRYPTED):
|
| 28 |
+
log_status("β‘ [CACHE] Resuming from sovereign weights.")
|
| 29 |
return True
|
| 30 |
|
| 31 |
if not HF_TOKEN or not SECRET_KEY_HEX:
|
| 32 |
+
log_status("β [SECURITY] Credentials missing.")
|
| 33 |
return False
|
| 34 |
|
|
|
|
| 35 |
login(token=HF_TOKEN)
|
|
|
|
|
|
|
| 36 |
path = hf_hub_download(repo_id=SOURCE_REPO, filename=SOURCE_FILE, local_dir=".")
|
| 37 |
|
| 38 |
+
log_status("π [DECRYPT] Unlocking weights...")
|
| 39 |
key = bytes.fromhex(SECRET_KEY_HEX)
|
| 40 |
aes = AESGCM(key)
|
| 41 |
|
|
|
|
| 47 |
f_out.write(chunk)
|
| 48 |
|
| 49 |
os.remove(path)
|
| 50 |
+
log_status("β
[SYSTEM] Integrity verified.")
|
| 51 |
return True
|
| 52 |
except Exception as e:
|
| 53 |
+
log_status(f"β [BOOT ERROR] {str(e)}")
|
|
|
|
| 54 |
return False
|
| 55 |
|
| 56 |
+
# --- ENGINE ---
|
| 57 |
llm = None
|
| 58 |
if initialize_weights():
|
| 59 |
try:
|
| 60 |
+
log_status("π§ [ENGINE] Initializing Llama...")
|
| 61 |
llm = Llama(
|
| 62 |
model_path=TEMP_DECRYPTED,
|
| 63 |
n_ctx=2048,
|
| 64 |
+
n_threads=2,
|
| 65 |
+
n_batch=512,
|
| 66 |
+
use_mlock=True,
|
| 67 |
verbose=False
|
| 68 |
)
|
| 69 |
+
log_status("π [SYSTEM] Node Online.")
|
| 70 |
except Exception as e:
|
| 71 |
+
log_status(f"β [ENGINE ERROR] {e}")
|
| 72 |
|
| 73 |
+
# --- API CORE ---
|
| 74 |
app = FastAPI()
|
| 75 |
|
| 76 |
@app.post("/run_inference")
|
| 77 |
async def run_inference(request: Request):
|
| 78 |
+
if not llm: return {"error": "Offline"}
|
|
|
|
|
|
|
| 79 |
data = await request.json()
|
|
|
|
|
|
|
|
|
|
| 80 |
if data.get("secretKey") != SECRET_KEY_HEX:
|
| 81 |
+
return {"error": "Unauthorized Access"}
|
| 82 |
|
| 83 |
prompt = data.get("prompt", "")
|
| 84 |
+
output = llm(f"<|user|>\n{prompt}<|end|>\n<|assistant|>", max_tokens=512, stop=["<|end|>"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
return {"response": output['choices'][0]['text'].strip()}
|
| 86 |
|
| 87 |
+
# --- UI LOGIC (FIXED DELTA ERROR) ---
|
| 88 |
def ui_chat(msg, hist):
|
| 89 |
if not llm:
|
| 90 |
+
yield "π¨ System Offline."
|
| 91 |
return
|
| 92 |
|
| 93 |
+
# High-level completion stream
|
| 94 |
stream = llm(
|
| 95 |
f"<|user|>\n{msg}<|end|>\n<|assistant|>",
|
| 96 |
max_tokens=512,
|
|
|
|
| 100 |
|
| 101 |
partial_text = ""
|
| 102 |
for chunk in stream:
|
| 103 |
+
# FIX: Access 'text' directly from choices[0]
|
| 104 |
+
token = chunk['choices'][0]['text']
|
| 105 |
+
if token:
|
| 106 |
+
partial_text += token
|
| 107 |
yield partial_text
|
| 108 |
|
| 109 |
+
# --- BRANDED UI ---
|
| 110 |
+
custom_css = "footer {visibility: hidden} .gradio-container {background-color: #050505 !important}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
demo = gr.ChatInterface(
|
| 113 |
ui_chat,
|
| 114 |
title="METANTHROPIC Β· PHI-3 SOVEREIGN",
|
| 115 |
+
description="Secure inference via decentralized sovereign weights.",
|
| 116 |
+
theme=gr.themes.Soft(primary_hue="slate", neutral_hue="zinc"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
css=custom_css
|
| 118 |
)
|
| 119 |
|