Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,37 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
|
|
1 |
+
# 1) Import the real model class
|
2 |
+
from smolagents.models import HfApiModel
|
3 |
+
|
4 |
+
# 2) Keep a reference to the original __call__
|
5 |
+
_original_hfapi_call = HfApiModel.__call__
|
6 |
+
|
7 |
+
# 3) Define a safe wrapper that never blows up on None usage
|
8 |
+
def _safe_hfapi_call(self, *args, **kwargs):
|
9 |
+
# 3a) Invoke the original method
|
10 |
+
response = _original_hfapi_call(self, *args, **kwargs)
|
11 |
+
|
12 |
+
# 3b) Now defensively pull out token‐usage
|
13 |
+
try:
|
14 |
+
usage = response.usage or type("U", (), {})()
|
15 |
+
# fallback to 0 if either is None or missing
|
16 |
+
self.last_input_token_count = getattr(usage, "prompt_tokens", 0) or 0
|
17 |
+
self.last_output_token_count = getattr(usage, "completion_tokens", 0) or 0
|
18 |
+
except Exception:
|
19 |
+
self.last_input_token_count = 0
|
20 |
+
self.last_output_token_count = 0
|
21 |
+
|
22 |
+
# 3c) Return the same ChatMessage the original returned
|
23 |
+
return response.choices[0].message
|
24 |
+
|
25 |
+
# 4) Monkey-patch the method in place
|
26 |
+
HfApiModel.__call__ = _safe_hfapi_call
|
27 |
+
|
28 |
+
# -----------------------------------------------------------------------------
|
29 |
+
# Now proceed with your normal imports and setup:
|
30 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
#from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
35 |
import datetime
|
36 |
import requests
|
37 |
import pytz
|