Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -3,12 +3,19 @@ import hashlib
|
|
| 3 |
import json
|
| 4 |
import logging
|
| 5 |
from smolagents import CodeAgent, tool
|
| 6 |
-
from smolagents.models import Model
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Cache for answers
|
| 13 |
CACHE_FILE = "answer_cache.json"
|
| 14 |
if os.path.exists(CACHE_FILE):
|
|
@@ -69,12 +76,18 @@ def web_search(query: str) -> str:
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"Search error: {e}"
|
| 71 |
|
| 72 |
-
# ---------- Custom model
|
| 73 |
class CustomHFModel(Model):
|
| 74 |
-
def __init__(self, model_id="
|
| 75 |
super().__init__(**kwargs)
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
self.model_id = model_id
|
|
|
|
| 78 |
|
| 79 |
def generate(
|
| 80 |
self,
|
|
@@ -85,19 +98,18 @@ class CustomHFModel(Model):
|
|
| 85 |
temperature=0.7,
|
| 86 |
**kwargs
|
| 87 |
):
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
return response.choices[0].message.content
|
| 101 |
|
| 102 |
# ---------- Assemble the agent ----------
|
| 103 |
tools = [calculator]
|
|
@@ -108,7 +120,7 @@ try:
|
|
| 108 |
except ImportError:
|
| 109 |
logger.warning("duckduckgo-search not installed, web_search disabled.")
|
| 110 |
|
| 111 |
-
model = CustomHFModel() #
|
| 112 |
agent = CodeAgent(tools=tools, model=model)
|
| 113 |
|
| 114 |
# ---------- The class expected by app.py ----------
|
|
|
|
| 3 |
import json
|
| 4 |
import logging
|
| 5 |
from smolagents import CodeAgent, tool
|
| 6 |
+
from smolagents.models import Model
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
+
# Check HF_TOKEN at startup
|
| 13 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 14 |
+
if hf_token:
|
| 15 |
+
logger.info("HF_TOKEN is set (first 4 chars: %s...)", hf_token[:4])
|
| 16 |
+
else:
|
| 17 |
+
logger.warning("HF_TOKEN is NOT set - inference may fail")
|
| 18 |
+
|
| 19 |
# Cache for answers
|
| 20 |
CACHE_FILE = "answer_cache.json"
|
| 21 |
if os.path.exists(CACHE_FILE):
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
return f"Search error: {e}"
|
| 78 |
|
| 79 |
+
# ---------- Custom model with explicit Hugging Face endpoint ----------
|
| 80 |
class CustomHFModel(Model):
|
| 81 |
+
def __init__(self, model_id="mistralai/Mistral-7B-Instruct-v0.2", **kwargs):
|
| 82 |
super().__init__(**kwargs)
|
| 83 |
+
# Force Hugging Face inference endpoint
|
| 84 |
+
self.client = InferenceClient(
|
| 85 |
+
model=model_id,
|
| 86 |
+
token=os.getenv("HF_TOKEN"),
|
| 87 |
+
base_url="https://api-inference.huggingface.co" # Explicit HF URL
|
| 88 |
+
)
|
| 89 |
self.model_id = model_id
|
| 90 |
+
logger.info(f"CustomHFModel initialized with model: {model_id}")
|
| 91 |
|
| 92 |
def generate(
|
| 93 |
self,
|
|
|
|
| 98 |
temperature=0.7,
|
| 99 |
**kwargs
|
| 100 |
):
|
| 101 |
+
try:
|
| 102 |
+
response = self.client.chat_completion(
|
| 103 |
+
messages=messages,
|
| 104 |
+
max_tokens=max_tokens,
|
| 105 |
+
temperature=temperature,
|
| 106 |
+
stop=stop_sequences,
|
| 107 |
+
**kwargs
|
| 108 |
+
)
|
| 109 |
+
return response.choices[0].message.content
|
| 110 |
+
except Exception as e:
|
| 111 |
+
# Wrap error with model info for debugging
|
| 112 |
+
raise Exception(f"Model {self.model_id} inference failed: {e}")
|
|
|
|
| 113 |
|
| 114 |
# ---------- Assemble the agent ----------
|
| 115 |
tools = [calculator]
|
|
|
|
| 120 |
except ImportError:
|
| 121 |
logger.warning("duckduckgo-search not installed, web_search disabled.")
|
| 122 |
|
| 123 |
+
model = CustomHFModel() # You can change model_id here if needed
|
| 124 |
agent = CodeAgent(tools=tools, model=model)
|
| 125 |
|
| 126 |
# ---------- The class expected by app.py ----------
|