Akhil15Sharma commited on
Commit
9cc8180
·
1 Parent(s): a06a87c

Add code agent with Gradio UI

Browse files
Files changed (3) hide show
  1. app.py +67 -0
  2. prompts.yaml +3 -0
  3. requirement.txt +5 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Import Gradio UI (Ensure Gradio_UI is correctly implemented)
9
+ from Gradio_UI import GradioUI
10
+
11
+ # Custom tool that does nothing (placeholder)
12
+ @tool
13
+ def my_custom_tool(arg1: str, arg2: int) -> str:
14
+ """A tool that does nothing yet.
15
+ Args:
16
+ arg1: The first argument
17
+ arg2: The second argument
18
+ """
19
+ return "What magic will you build?"
20
+
21
+ # Tool to get the current time in a specified timezone
22
+ @tool
23
+ def get_current_time_in_timezone(timezone: str) -> str:
24
+ """A tool that fetches the current local time in a specified timezone.
25
+ Args:
26
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
27
+ """
28
+ try:
29
+ tz = pytz.timezone(timezone)
30
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
31
+ return f"The current local time in {timezone} is: {local_time}"
32
+ except Exception as e:
33
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
34
+
35
+ # Initialize the final answer tool
36
+ final_answer = FinalAnswerTool()
37
+
38
+ # Initialize the model
39
+ model = HfApiModel(
40
+ max_tokens=2096,
41
+ temperature=0.5,
42
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
43
+ custom_role_conversions=None,
44
+ )
45
+
46
+ # Import image generation tool from Hub
47
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
48
+
49
+ # Load system prompt from prompt.yaml file
50
+ with open("prompts.yaml", 'r') as stream:
51
+ prompt_templates = yaml.safe_load(stream)
52
+
53
+ # Create the CodeAgent with tools and prompt templates
54
+ agent = CodeAgent(
55
+ model=model,
56
+ tools=[final_answer, my_custom_tool, get_current_time_in_timezone, image_generation_tool], # Include all tools
57
+ max_steps=6,
58
+ verbosity_level=1,
59
+ grammar=None,
60
+ planning_interval=None,
61
+ name="Code Agent",
62
+ description="An intelligent agent for performing various tasks.",
63
+ prompt_templates=prompt_templates
64
+ )
65
+
66
+ # Launch the Gradio UI
67
+ GradioUI(agent).launch()
prompts.yaml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ prompts:
2
+ - role: system
3
+ content: "You are an assistant designed to code."
requirement.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ smolagents
2
+ gradio
3
+ pytz
4
+ PyYAML
5
+ requests