zerito commited on
Commit
726256e
·
verified ·
1 Parent(s): 763e5ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -16
app.py CHANGED
@@ -1,7 +1,10 @@
1
- from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
  import requests
 
3
  from tools.final_answer import FinalAnswerTool
4
  from Gradio_UI import GradioUI
 
 
5
 
6
  @tool
7
  def get_btc_price() -> str:
@@ -16,16 +19,11 @@ def get_btc_price() -> str:
16
  return '{"error": "Bitcoin price not found"}'
17
  return f"The current Bitcoin price is ${price:.2f} USD."
18
 
19
-
20
-
21
-
22
  @tool
23
  def visit_webpage(url: str) -> str:
24
  """Visits a webpage at the given URL and returns its content as a markdown string.
25
-
26
  Args:
27
  url: The URL of the webpage to visit.
28
-
29
  Returns:
30
  The content of the webpage converted to Markdown, or an error message if the request fails.
31
  """
@@ -35,35 +33,29 @@ def visit_webpage(url: str) -> str:
35
  response.raise_for_status() # Raise an exception for bad status codes
36
 
37
  # Convert the HTML content to Markdown
38
- markdown_content = markdownify(response.text).strip()
39
 
40
  # Remove multiple line breaks
41
  markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
42
 
43
  return markdown_content
44
 
45
- except RequestException as e:
46
  return f"Error fetching the webpage: {str(e)}"
47
  except Exception as e:
48
  return f"An unexpected error occurred: {str(e)}"
49
 
50
-
51
-
52
  # Initialize the FinalAnswerTool
53
  final_answer = FinalAnswerTool()
54
 
55
  # Initialize the model
56
  model = HfApiModel(
57
- max_tokens=1000,
58
  temperature=0.5,
59
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
60
  custom_role_conversions=None,
61
  )
62
 
63
- # Load additional tools if needed
64
- # For example, to load an image generation tool:
65
- # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
66
-
67
  # Create the agent
68
  agent = CodeAgent(
69
  model=model,
@@ -79,4 +71,3 @@ agent = CodeAgent(
79
 
80
  # Launch the Gradio interface
81
  GradioUI(agent).launch()
82
-
 
1
+ from smolagents import CodeAgent, HfApiModel, tool
2
  import requests
3
+ from requests.exceptions import RequestException # Add this import
4
  from tools.final_answer import FinalAnswerTool
5
  from Gradio_UI import GradioUI
6
+ import markdownify
7
+ import re
8
 
9
  @tool
10
  def get_btc_price() -> str:
 
19
  return '{"error": "Bitcoin price not found"}'
20
  return f"The current Bitcoin price is ${price:.2f} USD."
21
 
 
 
 
22
  @tool
23
  def visit_webpage(url: str) -> str:
24
  """Visits a webpage at the given URL and returns its content as a markdown string.
 
25
  Args:
26
  url: The URL of the webpage to visit.
 
27
  Returns:
28
  The content of the webpage converted to Markdown, or an error message if the request fails.
29
  """
 
33
  response.raise_for_status() # Raise an exception for bad status codes
34
 
35
  # Convert the HTML content to Markdown
36
+ markdown_content = markdownify.markdownify(response.text).strip()
37
 
38
  # Remove multiple line breaks
39
  markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
40
 
41
  return markdown_content
42
 
43
+ except RequestException as e: # Updated to use the imported RequestException
44
  return f"Error fetching the webpage: {str(e)}"
45
  except Exception as e:
46
  return f"An unexpected error occurred: {str(e)}"
47
 
 
 
48
  # Initialize the FinalAnswerTool
49
  final_answer = FinalAnswerTool()
50
 
51
  # Initialize the model
52
  model = HfApiModel(
53
+ max_tokens=1000, # Reduced from 2096 to avoid token limit errors
54
  temperature=0.5,
55
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
56
  custom_role_conversions=None,
57
  )
58
 
 
 
 
 
59
  # Create the agent
60
  agent = CodeAgent(
61
  model=model,
 
71
 
72
  # Launch the Gradio interface
73
  GradioUI(agent).launch()