ekjotsingh commited on
Commit
0822b66
Β·
verified Β·
1 Parent(s): 64ce0ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -67
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
- # --- CONFIGURATION ---
 
 
 
 
 
 
 
9
  SOURCE_REPO = "metanthropic/metanthropic-phi3-encrypted"
10
  SOURCE_FILENAME = "metanthropic-phi3-v1.mguf"
11
- TEMP_DECRYPTED = "/tmp/session_model.gguf"
12
-
13
- # Secrets
14
  SECRET_KEY_HEX = os.environ.get("DECRYPTION_KEY")
15
  HF_TOKEN = os.environ.get("HF_TOKEN")
16
 
17
- print("πŸ”„ [SYSTEM] Metanthropic Node Starting...")
18
-
19
- def setup_model():
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
- # Download
30
- print(f"⬇️ [NETWORK] Downloading model from {SOURCE_REPO}...")
31
- try:
32
- encrypted_path = hf_hub_download(
33
- repo_id=SOURCE_REPO,
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
- aesgcm = AESGCM(key)
48
 
49
- with open(encrypted_path, "rb") as f_in, open(TEMP_DECRYPTED, "wb") as f_out:
50
  nonce = f_in.read(12)
51
- header_len = struct.unpack("<I", f_in.read(4))[0]
52
- encrypted_header = f_in.read(header_len)
53
-
54
- # Decrypt Header
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
- setup_model()
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"❌ CRITICAL BOOT ERROR: {e}")
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
- demo = gr.ChatInterface(fn=chat, title="Metanthropic Node (Phi-3)")
 
 
 
86
 
87
- if __name__ == "__main__":
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)