dlaima commited on
Commit
e953cb6
·
verified ·
1 Parent(s): 4eee7ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -68
app.py CHANGED
@@ -1,94 +1,72 @@
1
- #from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- from smolagents import CodeAgent,DuckDuckGoSearchTool, OpenAIServerModel,load_tool,tool
3
  import datetime
4
- import requests
5
  import pytz
6
  import yaml
7
  from tools.final_answer import FinalAnswerTool
 
8
 
9
- from Gradio_UI import GradioUI
10
-
11
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
- #@tool
13
- #def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
14
- #Keep this format for the description / args / args description but feel free to modify the tool
15
- # """A tool that does nothing yet
16
- # Args:
17
- # arg1: the first argument
18
- # arg2: the second argument
19
- # """
20
- # return "What magic will you build ?"
21
-
22
-
23
- # Tool to create a personalized greeting repeated a specific number of times.
24
  @tool
25
  def my_custom_tool(arg1: str, arg2: int) -> str:
26
- """
27
- A tool that creates a personalized greeting repeated multiple times.
28
-
29
- Args:
30
- arg1: the name or phrase to greet
31
- arg2: the number of times to repeat the greeting
32
- """
33
  greeting = f"Hello, {arg1}!"
34
  return " ".join([greeting] * arg2)
35
 
36
-
37
- # Tool to get the current local time in your chosen timezone.
38
  @tool
39
  def get_current_time_in_timezone(timezone: str) -> str:
40
- """A tool that fetches the current local time in a specified timezone.
41
- Args:
42
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
43
- """
44
  try:
45
- # Create timezone object
46
  tz = pytz.timezone(timezone)
47
- # Get current time in that timezone
48
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
49
  return f"The current local time in {timezone} is: {local_time}"
50
  except Exception as e:
51
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
52
 
53
-
54
  final_answer = FinalAnswerTool()
55
 
56
- # 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:
57
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
58
-
59
- #model = HfApiModel(
60
- #max_tokens=2096,
61
- #temperature=0.5,
62
- #model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
63
- #custom_role_conversions=None,
64
- #)
65
- model = OpenAIServerModel(
66
- max_tokens=2096,
67
- temperature=0.5,
68
- model_id="gpt-4o",
69
- custom_role_conversions=None,
70
- )
71
-
72
-
73
 
74
- # Import tool from Hub
75
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
76
 
77
- with open("prompts.yaml", 'r') as stream:
78
- prompt_templates = yaml.safe_load(stream)
79
-
80
- agent = CodeAgent(
81
- model=model,
82
- #tools=[final_answer], ## add your tools here (don't remove final answer)
83
- tools=[my_custom_tool, get_current_time_in_timezone, final_answer], ## add your tools here (don't remove final answer)
84
- max_steps=6,
85
- verbosity_level=1,
86
- grammar=None,
87
- planning_interval=None,
88
- name=None,
89
- description=None,
90
- prompt_templates=prompt_templates
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  )
92
 
93
-
94
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, load_tool, tool
 
2
  import datetime
 
3
  import pytz
4
  import yaml
5
  from tools.final_answer import FinalAnswerTool
6
+ import gradio as gr
7
 
8
+ # ----- Tools -----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  @tool
10
  def my_custom_tool(arg1: str, arg2: int) -> str:
11
+ """A tool that creates a personalized greeting repeated multiple times."""
 
 
 
 
 
 
12
  greeting = f"Hello, {arg1}!"
13
  return " ".join([greeting] * arg2)
14
 
 
 
15
  @tool
16
  def get_current_time_in_timezone(timezone: str) -> str:
17
+ """Fetches the current local time in a specified timezone."""
 
 
 
18
  try:
 
19
  tz = pytz.timezone(timezone)
 
20
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
21
  return f"The current local time in {timezone} is: {local_time}"
22
  except Exception as e:
23
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
24
 
 
25
  final_answer = FinalAnswerTool()
26
 
27
+ # Load prompt templates
28
+ with open("prompts.yaml", 'r') as stream:
29
+ prompt_templates = yaml.safe_load(stream)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # Load additional tools from Hugging Face Hub
32
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
33
 
34
+ # ----- Function to initialize agent dynamically -----
35
+ def initialize_agent(api_key: str):
36
+ model = OpenAIServerModel(
37
+ max_tokens=2096,
38
+ temperature=0.5,
39
+ model_id="gpt-4o",
40
+ custom_role_conversions=None,
41
+ openai_api_key=api_key # Pass the user-provided API key
42
+ )
43
+
44
+ agent = CodeAgent(
45
+ model=model,
46
+ tools=[my_custom_tool, get_current_time_in_timezone, final_answer],
47
+ max_steps=6,
48
+ verbosity_level=1,
49
+ grammar=None,
50
+ planning_interval=None,
51
+ name=None,
52
+ description=None,
53
+ prompt_templates=prompt_templates
54
+ )
55
+
56
+ return agent
57
+
58
+ # ----- Gradio UI -----
59
+ def launch_agent(api_key: str):
60
+ agent = initialize_agent(api_key)
61
+ return GradioUI(agent).launch(share=True)
62
+
63
+ # Interface for the user to enter their OpenAI API key
64
+ iface = gr.Interface(
65
+ fn=launch_agent,
66
+ inputs=gr.Textbox(label="Enter your OpenAI API Key", type="password"),
67
+ outputs="text",
68
+ title="Custom SmolAgent App",
69
+ description="Enter your OpenAI API key to use the agent."
70
  )
71
 
72
+ iface.launch()