rphrp1985 commited on
Commit
7dbbd34
·
verified ·
1 Parent(s): 026db1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py CHANGED
@@ -2,6 +2,51 @@ import spaces
2
  import os
3
  import json
4
  import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from llama_cpp import Llama
6
  from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
7
  from llama_cpp_agent.providers import LlamaCppPythonProvider
 
2
  import os
3
  import json
4
  import subprocess
5
+
6
+ ### monkey patch
7
+
8
+ Yes ✅ — you must patch after importing llama_cpp but BEFORE initializing Llama().
9
+
10
+ You cannot patch before importing because the class doesn’t exist yet.
11
+
12
+ Here is the correct order 👇
13
+
14
+ ✅ Correct Working Code Order
15
+ # 1️⃣ Import llama_cpp FIRST
16
+ import llama_cpp._internals as internals
17
+
18
+
19
+ # 2️⃣ Monkey patch BEFORE creating Llama()
20
+
21
+ _original_close = internals.LlamaModel.close
22
+
23
+ def safe_close(self):
24
+ try:
25
+ if hasattr(self, "sampler") and self.sampler is not None:
26
+ return _original_close(self)
27
+ except Exception:
28
+ pass
29
+
30
+ internals.LlamaModel.close = safe_close
31
+
32
+
33
+ def safe_del(self):
34
+ try:
35
+ self.close()
36
+ except Exception:
37
+ pass
38
+
39
+ internals.LlamaModel.__del__ = safe_del
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
  from llama_cpp import Llama
51
  from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
52
  from llama_cpp_agent.providers import LlamaCppPythonProvider