jcleee commited on
Commit
419fd58
·
verified ·
1 Parent(s): b8f293f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -9
app.py CHANGED
@@ -10,14 +10,28 @@ from Gradio_UI import GradioUI
10
 
11
  @tool
12
  def web_search(query: str) -> str:
13
- """Searches DuckDuckGo and returns top results as a string."""
 
 
 
 
 
 
 
14
  search_tool = DuckDuckGoSearchTool()
15
  results = search_tool(query)
16
  return "\n".join(results)
17
 
18
  @tool
19
  def visit_webpage(url: str) -> str:
20
- """Fetches the content of a web page."""
 
 
 
 
 
 
 
21
  try:
22
  response = requests.get(url, timeout=5)
23
  return response.text[:5000]
@@ -26,24 +40,38 @@ def visit_webpage(url: str) -> str:
26
 
27
  @tool
28
  def text_splitter(text: str) -> List[str]:
29
- """Splits text into chunks using langchain's CharacterTextSplitter."""
 
 
 
 
 
 
 
30
  splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
31
  return splitter.split_text(text)
32
 
33
  @tool
34
  def get_current_time_in_timezone(timezone: str) -> str:
35
- """Gets current time in a given timezone."""
 
 
 
 
 
 
 
36
  try:
37
  tz = pytz.timezone(timezone)
38
  return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
39
  except Exception as e:
40
  return f"Error fetching time: {e}"
41
 
42
- # Load prompts.yaml
43
  with open("prompts.yaml", "r") as stream:
44
  prompt_templates = yaml.safe_load(stream)
45
 
46
- # Define model
47
  model = HfApiModel(
48
  max_tokens=2096,
49
  temperature=0.5,
@@ -53,13 +81,13 @@ model = HfApiModel(
53
  custom_role_conversions=None,
54
  )
55
 
56
- # Required final answer tool
57
  final_answer = FinalAnswerTool()
58
 
59
  # Optional additional tool
60
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
61
 
62
- # Build agent
63
  agent = CodeAgent(
64
  model=model,
65
  tools=[final_answer, web_search, visit_webpage, text_splitter, image_generation_tool],
@@ -72,5 +100,5 @@ agent = CodeAgent(
72
  prompt_templates=prompt_templates
73
  )
74
 
75
- # Launch UI with visible errors
76
  GradioUI(agent).launch(show_error=True)
 
10
 
11
  @tool
12
  def web_search(query: str) -> str:
13
+ """Searches DuckDuckGo and returns top results as a string.
14
+
15
+ Args:
16
+ query (str): The search query.
17
+
18
+ Returns:
19
+ str: The search results concatenated into a string.
20
+ """
21
  search_tool = DuckDuckGoSearchTool()
22
  results = search_tool(query)
23
  return "\n".join(results)
24
 
25
  @tool
26
  def visit_webpage(url: str) -> str:
27
+ """Fetches the content of a web page.
28
+
29
+ Args:
30
+ url (str): The URL to visit.
31
+
32
+ Returns:
33
+ str: The raw text content of the page (first 5000 characters).
34
+ """
35
  try:
36
  response = requests.get(url, timeout=5)
37
  return response.text[:5000]
 
40
 
41
  @tool
42
  def text_splitter(text: str) -> List[str]:
43
+ """Splits text into chunks using langchain's CharacterTextSplitter.
44
+
45
+ Args:
46
+ text (str): The input text to split.
47
+
48
+ Returns:
49
+ List[str]: A list of text chunks.
50
+ """
51
  splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
52
  return splitter.split_text(text)
53
 
54
  @tool
55
  def get_current_time_in_timezone(timezone: str) -> str:
56
+ """Gets the current time in a specified timezone.
57
+
58
+ Args:
59
+ timezone (str): A timezone string, like 'America/New_York'.
60
+
61
+ Returns:
62
+ str: The local time formatted as a string.
63
+ """
64
  try:
65
  tz = pytz.timezone(timezone)
66
  return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
67
  except Exception as e:
68
  return f"Error fetching time: {e}"
69
 
70
+ # Load prompt templates
71
  with open("prompts.yaml", "r") as stream:
72
  prompt_templates = yaml.safe_load(stream)
73
 
74
+ # Define the model
75
  model = HfApiModel(
76
  max_tokens=2096,
77
  temperature=0.5,
 
81
  custom_role_conversions=None,
82
  )
83
 
84
+ # Load the final answer tool
85
  final_answer = FinalAnswerTool()
86
 
87
  # Optional additional tool
88
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
89
 
90
+ # Create the agent
91
  agent = CodeAgent(
92
  model=model,
93
  tools=[final_answer, web_search, visit_webpage, text_splitter, image_generation_tool],
 
100
  prompt_templates=prompt_templates
101
  )
102
 
103
+ # Launch the UI
104
  GradioUI(agent).launch(show_error=True)