sarjan109 commited on
Commit
5b59a99
·
verified ·
1 Parent(s): b9f6f2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -6,54 +6,68 @@ from tools.final_answer import FinalAnswerTool
6
  from Gradio_UI import GradioUI
7
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  @tool
10
  def get_current_time_in_timezone(timezone: str) -> str:
11
  """
12
- Get the current local time in a specified timezone.
13
 
14
  Args:
15
- timezone (str): A valid timezone string such as 'Asia/Kolkata' or 'America/New_York'.
16
 
17
  Returns:
18
- str: The current local time in the specified timezone.
19
  """
20
  try:
21
  tz = pytz.timezone(timezone)
22
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
23
- return f"The current local time in {timezone} is: {local_time}"
24
  except Exception as e:
25
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
26
 
27
 
28
- # Final answer tool (mandatory for agent)
29
  final_answer = FinalAnswerTool()
30
 
31
 
32
- # LLM model for agent reasoning
33
  model = HfApiModel(
34
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
35
- max_tokens=2096,
36
- temperature=0.5,
37
- )
38
-
39
-
40
- # Image generation tool from Hugging Face Agents course
41
- image_generation_tool = load_tool(
42
- "agents-course/text-to-image",
43
- trust_remote_code=True
44
  )
45
 
46
 
47
- # Load prompt templates
48
- with open("prompts.yaml", "r") as stream:
49
- prompt_templates = yaml.safe_load(stream)
50
 
51
 
52
- # Create agent
53
  agent = CodeAgent(
54
  model=model,
55
  tools=[
56
- image_generation_tool,
57
  DuckDuckGoSearchTool(),
58
  get_current_time_in_timezone,
59
  final_answer
@@ -61,10 +75,10 @@ agent = CodeAgent(
61
  max_steps=6,
62
  verbosity_level=2,
63
  name="multitool_agent",
64
- description="Agent that can search the web, generate images, and fetch timezone information.",
65
  prompt_templates=prompt_templates
66
  )
67
 
68
 
69
- # Launch Gradio UI
70
  GradioUI(agent).launch()
 
6
  from Gradio_UI import GradioUI
7
 
8
 
9
+ # Load HF image generation tool
10
+ hf_image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
11
+
12
+
13
+ @tool
14
+ def generate_image(prompt: str) -> str:
15
+ """
16
+ Generate an image from a text prompt.
17
+
18
+ Args:
19
+ prompt (str): Description of the image.
20
+
21
+ Returns:
22
+ str: File path of the generated image.
23
+ """
24
+ img = hf_image_tool(prompt=prompt)
25
+ path = "/tmp/generated_image.png"
26
+ img.save(path)
27
+ return path
28
+
29
+
30
  @tool
31
  def get_current_time_in_timezone(timezone: str) -> str:
32
  """
33
+ Get current time for a timezone.
34
 
35
  Args:
36
+ timezone (str): Example 'Asia/Kolkata'.
37
 
38
  Returns:
39
+ str: Current time in that timezone.
40
  """
41
  try:
42
  tz = pytz.timezone(timezone)
43
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
44
+ return f"Time in {timezone}: {local_time}"
45
  except Exception as e:
46
+ return str(e)
47
 
48
 
49
+ # Mandatory final answer tool
50
  final_answer = FinalAnswerTool()
51
 
52
 
53
+ # LLM for reasoning
54
  model = HfApiModel(
55
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
56
+ max_tokens=2048,
57
+ temperature=0.5
 
 
 
 
 
 
 
58
  )
59
 
60
 
61
+ # Prompt templates
62
+ with open("prompts.yaml", "r") as f:
63
+ prompt_templates = yaml.safe_load(f)
64
 
65
 
66
+ # Agent
67
  agent = CodeAgent(
68
  model=model,
69
  tools=[
70
+ generate_image,
71
  DuckDuckGoSearchTool(),
72
  get_current_time_in_timezone,
73
  final_answer
 
75
  max_steps=6,
76
  verbosity_level=2,
77
  name="multitool_agent",
78
+ description="Agent that can generate images, search the web, and get timezone time.",
79
  prompt_templates=prompt_templates
80
  )
81
 
82
 
83
+ # Launch UI
84
  GradioUI(agent).launch()