mohantest commited on
Commit
c34464d
·
verified ·
1 Parent(s): 56ca616

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -2,9 +2,9 @@ import logging
2
  import hashlib
3
  import json
4
  import os
5
- from smolagents import CodeAgent, HfApiModel, tool
6
 
7
- # Set up logging to see what's happening (logs appear in the Space logs)
8
  logging.basicConfig(level=logging.INFO)
9
  logger = logging.getLogger(__name__)
10
 
@@ -27,7 +27,7 @@ def calculator(expression: str) -> str:
27
  Safely evaluate a mathematical expression.
28
  Input should be a simple arithmetic expression (e.g., '2 + 2').
29
  """
30
- # Use a safe evaluator that only allows numbers and operators
31
  allowed_chars = set("0123456789+-*/(). ")
32
  if not all(c in allowed_chars for c in expression):
33
  return "Error: Expression contains disallowed characters."
@@ -42,7 +42,7 @@ def calculator(expression: str) -> str:
42
  def web_search(query: str) -> str:
43
  """
44
  Search the web for up-to-date information.
45
- Uses DuckDuckGo (free, no API key). Install duckduckgo-search first.
46
  """
47
  try:
48
  from duckduckgo_search import DDGS
@@ -50,7 +50,6 @@ def web_search(query: str) -> str:
50
  results = list(ddgs.text(query, max_results=3))
51
  if not results:
52
  return "No results found."
53
- # Format results as a simple string
54
  snippets = []
55
  for r in results:
56
  snippets.append(f"Title: {r['title']}\nBody: {r['body']}\nURL: {r['href']}")
@@ -61,16 +60,18 @@ def web_search(query: str) -> str:
61
  return f"Search error: {e}"
62
 
63
  # ---------- Agent ----------
 
64
  # You can specify a model explicitly, e.g.:
65
- # model = HfApiModel("meta-llama/Llama-2-7b-chat-hf")
66
- # Or use the default (zephyr-7b-beta)
67
- model = HfApiModel() # Uses your HF token if set, otherwise public inference
68
 
69
- # Add tools you need. Remove web_search if you don't have the package.
70
  tools = [calculator]
71
  try:
72
  import duckduckgo_search
73
  tools.append(web_search)
 
74
  except ImportError:
75
  logger.warning("duckduckgo-search not installed, web_search disabled.")
76
 
 
2
  import hashlib
3
  import json
4
  import os
5
+ from smolagents import CodeAgent, ApiModel, tool
6
 
7
+ # Set up logging (appears in Space logs)
8
  logging.basicConfig(level=logging.INFO)
9
  logger = logging.getLogger(__name__)
10
 
 
27
  Safely evaluate a mathematical expression.
28
  Input should be a simple arithmetic expression (e.g., '2 + 2').
29
  """
30
+ # Allow only numbers, basic operators, parentheses, and spaces
31
  allowed_chars = set("0123456789+-*/(). ")
32
  if not all(c in allowed_chars for c in expression):
33
  return "Error: Expression contains disallowed characters."
 
42
  def web_search(query: str) -> str:
43
  """
44
  Search the web for up-to-date information.
45
+ Uses DuckDuckGo (free, no API key). Requires duckduckgo-search package.
46
  """
47
  try:
48
  from duckduckgo_search import DDGS
 
50
  results = list(ddgs.text(query, max_results=3))
51
  if not results:
52
  return "No results found."
 
53
  snippets = []
54
  for r in results:
55
  snippets.append(f"Title: {r['title']}\nBody: {r['body']}\nURL: {r['href']}")
 
60
  return f"Search error: {e}"
61
 
62
  # ---------- Agent ----------
63
+ # Use ApiModel (the correct class name in current smolagents)
64
  # You can specify a model explicitly, e.g.:
65
+ # model = ApiModel("meta-llama/Llama-2-7b-chat-hf")
66
+ # The default uses Hugging Face's free inference API.
67
+ model = ApiModel()
68
 
69
+ # Only include web_search if the package is installed
70
  tools = [calculator]
71
  try:
72
  import duckduckgo_search
73
  tools.append(web_search)
74
+ logger.info("Web search tool enabled.")
75
  except ImportError:
76
  logger.warning("duckduckgo-search not installed, web_search disabled.")
77