achilis1 commited on
Commit
73f7203
·
verified ·
1 Parent(s): a353716

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -20
app.py CHANGED
@@ -1,13 +1,13 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
 
 
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
  def web_researcher(query: str, max_results: int = 3) -> str:
13
  """A tool that performs web research using DuckDuckGo search.
@@ -29,6 +29,7 @@ def web_researcher(query: str, max_results: int = 3) -> str:
29
  f"Title: {result.get('title', 'No title')}\n"
30
  f"URL: {result.get('link', 'No URL')}\n"
31
  f"Snippet: {result.get('snippet', 'No description')}\n"
 
32
  )
33
  return "\n".join(formatted_results)
34
  except Exception as e:
@@ -41,15 +42,12 @@ def get_current_time_in_timezone(timezone: str) -> str:
41
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
42
  """
43
  try:
44
- # Create timezone object
45
  tz = pytz.timezone(timezone)
46
- # Get current time in that timezone
47
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
48
  return f"The current local time in {timezone} is: {local_time}"
49
  except Exception as e:
50
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
51
 
52
-
53
  # Custom Final Answer Processor
54
  class CustomFinalAnswerTool:
55
  def __init__(self):
@@ -77,28 +75,23 @@ class CustomFinalAnswerTool:
77
  except Exception as e:
78
  return f"Image processing error: {str(e)}"
79
 
80
-
81
- # final_answer = FinalAnswerTool()
82
  final_answer = CustomFinalAnswerTool()
83
- # Import tool from Hub
84
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
85
 
86
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
87
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
88
  model = HfApiModel(
89
- max_tokens=2096,
90
- temperature=0.5,
91
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
92
- custom_role_conversions=None,
93
  )
94
 
95
-
96
  with open("prompts.yaml", 'r') as stream:
97
  prompt_templates = yaml.safe_load(stream)
98
 
99
  agent = CodeAgent(
100
  model=model,
101
- tools=[final_answer, web_researcher, get_current_time_in_timezone, image_generation_tool], ## add your tools here (don't remove final answer)
102
  max_steps=10,
103
  verbosity_level=1,
104
  grammar=None,
@@ -116,6 +109,4 @@ class CustomGradioUI(GradioUI):
116
  return final_answer
117
  return super().process_final_answer(final_answer)
118
 
119
- CustomGradioUI(agent).launch()
120
-
121
- # GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
  from Gradio_UI import GradioUI
8
+ from PIL import Image
9
+ import io
10
 
 
11
  @tool
12
  def web_researcher(query: str, max_results: int = 3) -> str:
13
  """A tool that performs web research using DuckDuckGo search.
 
29
  f"Title: {result.get('title', 'No title')}\n"
30
  f"URL: {result.get('link', 'No URL')}\n"
31
  f"Snippet: {result.get('snippet', 'No description')}\n"
32
+ f"{'-'*50}"
33
  )
34
  return "\n".join(formatted_results)
35
  except Exception as e:
 
42
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
43
  """
44
  try:
 
45
  tz = pytz.timezone(timezone)
 
46
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
47
  return f"The current local time in {timezone} is: {local_time}"
48
  except Exception as e:
49
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
50
 
 
51
  # Custom Final Answer Processor
52
  class CustomFinalAnswerTool:
53
  def __init__(self):
 
75
  except Exception as e:
76
  return f"Image processing error: {str(e)}"
77
 
78
+ # Initialize tools
 
79
  final_answer = CustomFinalAnswerTool()
 
80
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
81
 
 
 
82
  model = HfApiModel(
83
+ max_tokens=2096,
84
+ temperature=0.5,
85
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
86
+ custom_role_conversions=None,
87
  )
88
 
 
89
  with open("prompts.yaml", 'r') as stream:
90
  prompt_templates = yaml.safe_load(stream)
91
 
92
  agent = CodeAgent(
93
  model=model,
94
+ tools=[final_answer.tool, web_researcher, get_current_time_in_timezone, image_generation_tool],
95
  max_steps=10,
96
  verbosity_level=1,
97
  grammar=None,
 
109
  return final_answer
110
  return super().process_final_answer(final_answer)
111
 
112
+ CustomGradioUI(agent).launch()