Spaces:
Sleeping
Sleeping
Update smolagents_agent.py
Browse files- smolagents_agent.py +31 -5
smolagents_agent.py
CHANGED
|
@@ -546,15 +546,18 @@ class OptimizedSmolagentsGAIAgent:
|
|
| 546 |
def _initialize_model(self):
|
| 547 |
hf_token = os.getenv("HF_TOKEN")
|
| 548 |
if not hf_token:
|
| 549 |
-
print("
|
| 550 |
return None
|
| 551 |
try:
|
| 552 |
-
|
| 553 |
-
model =
|
| 554 |
-
|
|
|
|
|
|
|
|
|
|
| 555 |
return model
|
| 556 |
except Exception as e:
|
| 557 |
-
print(f"Error initializing
|
| 558 |
return None
|
| 559 |
|
| 560 |
def classify_question(self, question: str) -> Dict[str, Any]:
|
|
@@ -641,6 +644,29 @@ class OptimizedSmolagentsGAIAgent:
|
|
| 641 |
result = self.agent.run(prompt)
|
| 642 |
result_str = str(result)
|
| 643 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
# Cache the result
|
| 645 |
self.cache.set(cache_key, result_str)
|
| 646 |
|
|
|
|
| 546 |
def _initialize_model(self):
|
| 547 |
hf_token = os.getenv("HF_TOKEN")
|
| 548 |
if not hf_token:
|
| 549 |
+
print("HF_TOKEN not found. Please set it in environment variables")
|
| 550 |
return None
|
| 551 |
try:
|
| 552 |
+
from smolagents import InferenceClientModel
|
| 553 |
+
model = InferenceClientModel(
|
| 554 |
+
model_id="allenai/Olmo-3-32B-Think"
|
| 555 |
+
token=hf_token
|
| 556 |
+
)
|
| 557 |
+
print("Using HuggingFace model")
|
| 558 |
return model
|
| 559 |
except Exception as e:
|
| 560 |
+
print(f"Error initializing HuggingFace model: {e}")
|
| 561 |
return None
|
| 562 |
|
| 563 |
def classify_question(self, question: str) -> Dict[str, Any]:
|
|
|
|
| 644 |
result = self.agent.run(prompt)
|
| 645 |
result_str = str(result)
|
| 646 |
|
| 647 |
+
# Format response for HuggingFace compatibility
|
| 648 |
+
# Wrap any code blocks in <code> tags as expected by the parsing system
|
| 649 |
+
if "```" in result_str or "def " in result_str or "import " in result_str:
|
| 650 |
+
# Extract code blocks and wrap them properly
|
| 651 |
+
import re
|
| 652 |
+
# Find Python code blocks (either fenced with ```python or ``` or plain code)
|
| 653 |
+
code_pattern = r'```(?:python)?\n(.*?)\n```|```\n(.*?)\n```|([a-zA-Z_][a-zA-Z0-9_]*\s*=\s*.*|def\s+\w+.*|import\s+\w+.*|from\s+\w+.*import.*)'
|
| 654 |
+
matches = re.findall(code_pattern, result_str, re.DOTALL)
|
| 655 |
+
|
| 656 |
+
if matches:
|
| 657 |
+
formatted_result = result_str
|
| 658 |
+
for match in matches:
|
| 659 |
+
code_content = match[0] if match[0] else match[1] if match[1] else match[2]
|
| 660 |
+
if code_content:
|
| 661 |
+
# Wrap in <code> tags as expected by HuggingFace parser
|
| 662 |
+
wrapped_code = f"<code>{code_content.strip()}</code>"
|
| 663 |
+
formatted_result = formatted_result.replace(code_content.strip(), wrapped_code)
|
| 664 |
+
result_str = formatted_result
|
| 665 |
+
else:
|
| 666 |
+
# If no code blocks found but response contains code-like patterns, wrap entire response
|
| 667 |
+
if any(keyword in result_str.lower() for keyword in ['def ', 'import ', 'return ', 'print(', '# ']):
|
| 668 |
+
result_str = f"<code>{result_str}</code>"
|
| 669 |
+
|
| 670 |
# Cache the result
|
| 671 |
self.cache.set(cache_key, result_str)
|
| 672 |
|