ekjotsingh commited on
Commit
f41cf72
Β·
verified Β·
1 Parent(s): fec01e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -69
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/model_sovereign.gguf"
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 existing sovereign weights.")
29
  return True
30
 
31
  if not HF_TOKEN or not SECRET_KEY_HEX:
32
- log_status("❌ [SECURITY] Credentials missing. Verify HF_TOKEN and DECRYPTION_KEY.")
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 GGUF weights...")
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] Weight integrity verified.")
54
  return True
55
  except Exception as e:
56
- log_status(f"❌ [CRITICAL] Boot failure: {str(e)}")
57
- log_status(traceback.format_exc())
58
  return False
59
 
60
- # --- ENGINE INITIALIZATION (PERFORMANCE TUNED) ---
61
  llm = None
62
  if initialize_weights():
63
  try:
64
- log_status("🧠 [ENGINE] Initializing Neural Infrastructure...")
65
  llm = Llama(
66
  model_path=TEMP_DECRYPTED,
67
  n_ctx=2048,
68
- n_threads=2, # Locked to HF Free Tier vCPU limit
69
- n_batch=512, # Optimized for prompt ingestion speed
70
- use_mlock=True, # Pin model to RAM
71
  verbose=False
72
  )
73
- log_status("πŸš€ [SYSTEM] Sovereign Node Online.")
74
  except Exception as e:
75
- log_status(f"❌ [ENGINE ERROR] Neural load failed: {e}")
76
 
77
- # --- SECURED API CORE (CONVEX BRIDGE) ---
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 API Access. Use the Gradio UI instead."}
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
- # --- PREMIUM UI LOGIC (STREAMING) ---
102
  def ui_chat(msg, hist):
103
  if not llm:
104
- yield f"🚨 **SYSTEM ARCHITECTURE FAILURE**\n\nLatest Diagnostics:\n```\n" + "\n".join(DIAGNOSTIC_LOG[-3:]) + "\n```"
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
- delta = chunk['choices'][0]['delta']
117
- if 'content' in delta:
118
- partial_text += delta['content']
 
119
  yield partial_text
120
 
121
- # --- METANTHROPIC BRANDED INTERFACE ---
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
- <div id="title-container">
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