hugyoubabe commited on
Commit
05ffcef
·
verified ·
1 Parent(s): 8b12c0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -42
app.py CHANGED
@@ -1,83 +1,74 @@
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
-
9
- import google.generativeai as genai
10
  import os
11
 
12
- # Get the API key from the Hugging Face secret
13
- gemini_api_key = os.environ.get("GEMINI_API_KEY")
 
 
14
 
15
- # Configure the generative AI client
16
- genai.configure(api_key=gemini_api_key)
 
 
17
 
18
- # Create the generative model
19
- model = genai.GenerativeModel('gemini-1.5-flash')
20
- from Gradio_UI import GradioUI
21
 
22
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
23
  @tool
24
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
25
- #Keep this format for the description / args / args description but feel free to modify the tool
26
- """A tool that does nothing yet
27
  Args:
28
  arg1: the first argument
29
  arg2: the second argument
30
  """
31
- return "What magic will you build ?"
32
 
33
  @tool
34
  def get_current_time_in_timezone(timezone: str) -> str:
35
  """A tool that fetches the current local time in a specified timezone.
36
  Args:
37
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
38
  """
39
  try:
40
- # Create timezone object
41
  tz = pytz.timezone(timezone)
42
- # Get current time in that timezone
43
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
44
  return f"The current local time in {timezone} is: {local_time}"
45
  except Exception as e:
46
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
47
 
 
48
 
 
49
  final_answer = FinalAnswerTool()
50
 
51
- # 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:
52
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
53
-
54
-
55
- '''
56
- model = HfApiModel(
57
- max_tokens=2096,
58
- temperature=0.5,
59
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
60
- custom_role_conversions=None,
61
- )
62
- '''
63
-
64
  # Import tool from Hub
65
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
66
 
 
 
 
 
 
 
 
 
 
67
  with open("prompts.yaml", 'r') as stream:
68
  prompt_templates = yaml.safe_load(stream)
69
-
 
70
  agent = CodeAgent(
71
  model=model,
72
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
73
  max_steps=6,
74
  verbosity_level=1,
75
- grammar=None,
76
- planning_interval=None,
77
- name=None,
78
- description=None,
79
  prompt_templates=prompt_templates
80
  )
81
 
82
-
83
  GradioUI(agent).launch()
 
 
1
  import datetime
 
 
2
  import yaml
 
 
 
 
3
  import os
4
 
5
+ from smolagents import CodeAgent, LiteLLMModel, load_tool, tool
6
+ from tools.final_answer import FinalAnswerTool
7
+ from Gradio_UI import GradioUI
8
+ import pytz
9
 
10
+ # It's crucial to set your GEMINI_API_KEY in your environment secrets.
11
+ # The code will fail if the key is not found.
12
+ if "GEMINI_API_KEY" not in os.environ:
13
+ raise ValueError("GEMINI_API_KEY environment variable not set! Please add it to your secrets.")
14
 
15
+ # --- Tool Definitions ---
 
 
16
 
 
17
  @tool
18
+ def my_custom_tool(arg1: str, arg2: int) -> str:
19
+ """A tool that does nothing yet
 
20
  Args:
21
  arg1: the first argument
22
  arg2: the second argument
23
  """
24
+ return "What magic will you build?"
25
 
26
  @tool
27
  def get_current_time_in_timezone(timezone: str) -> str:
28
  """A tool that fetches the current local time in a specified timezone.
29
  Args:
30
+ timezone: A string representing a valid timezone (e.g., 'America/New_York', 'Europe/Berlin').
31
  """
32
  try:
 
33
  tz = pytz.timezone(timezone)
 
34
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
35
  return f"The current local time in {timezone} is: {local_time}"
36
  except Exception as e:
37
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
38
 
39
+ # --- Main Application Setup ---
40
 
41
+ # Initialize the mandatory final answer tool
42
  final_answer = FinalAnswerTool()
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  # Import tool from Hub
45
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
46
 
47
+ # Define the model using the LiteLLMModel wrapper for Gemini
48
+ # This is the correct way to integrate external APIs with smolagents.
49
+ model = LiteLLMModel(
50
+ model_id="gemini/gemini-1.5-flash", # Using the LiteLLM identifier for the Gemini model
51
+ max_tokens=2096,
52
+ temperature=0.5,
53
+ )
54
+
55
+ # Load prompt templates from the YAML file
56
  with open("prompts.yaml", 'r') as stream:
57
  prompt_templates = yaml.safe_load(stream)
58
+
59
+ # Initialize the CodeAgent with the Gemini model and the defined tools
60
  agent = CodeAgent(
61
  model=model,
62
+ tools=[
63
+ final_answer,
64
+ my_custom_tool,
65
+ get_current_time_in_timezone,
66
+ image_generation_tool
67
+ ],
68
  max_steps=6,
69
  verbosity_level=1,
 
 
 
 
70
  prompt_templates=prompt_templates
71
  )
72
 
73
+ # Launch the Gradio user interface
74
  GradioUI(agent).launch()