geraldaton20 commited on
Commit
77d5211
·
verified ·
1 Parent(s): d56a62d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -55
app.py CHANGED
@@ -1,73 +1,46 @@
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 search_duckduckgo(query: str) -> str:
13
- """Performs a web search using DuckDuckGo for real-time information.
14
-
15
- Args:
16
- query: A search phrase or question (e.g., 'latest AI research news').
17
- """
18
- try:
19
- return duckduckgo(query)
20
- except Exception as e:
21
- return f"Search failed: {str(e)}"
22
-
23
- @tool
24
- def get_current_time_in_timezone(timezone: str) -> str:
25
- """A tool that fetches the current local time in a specified timezone.
26
- Args:
27
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
28
- """
29
- try:
30
- # Create timezone object
31
- tz = pytz.timezone(timezone)
32
- # Get current time in that timezone
33
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
34
- return f"The current local time in {timezone} is: {local_time}"
35
- except Exception as e:
36
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
-
38
-
39
 
 
40
  final_answer = FinalAnswerTool()
41
 
42
- # 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:
43
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
44
 
 
45
  model = HfApiModel(
46
- max_tokens=2096,
47
- temperature=0.5,
48
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
49
- custom_role_conversions=None,
50
  )
51
 
52
-
53
- # Import tool from Hub
54
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
55
-
56
- with open("prompts.yaml", 'r') as stream:
57
- prompt_templates = yaml.safe_load(stream)
58
 
 
 
 
 
 
 
 
 
 
59
  agent = CodeAgent(
60
  model=model,
61
- tools=[final_answer,search_duckduckgo,
62
- get_current_time_in_timezone,image_generation_tool], ## add your tools here (don't remove final answer)
63
  max_steps=6,
64
  verbosity_level=1,
65
- grammar=None,
66
- planning_interval=None,
67
- name=None,
68
- description=None,
69
  prompt_templates=prompt_templates
70
  )
71
 
72
-
73
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
 
 
 
 
2
  from tools.final_answer import FinalAnswerTool
3
+ import yaml
4
  from Gradio_UI import GradioUI
5
 
6
+ # Load the image generation tool from the Hugging Face Hub
7
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Define the final answer tool
10
  final_answer = FinalAnswerTool()
11
 
12
+ # (Optional) Load prompt templates
13
+ with open("prompts.yaml", 'r') as stream:
14
+ prompt_templates = yaml.safe_load(stream)
15
 
16
+ # Load the language model
17
  model = HfApiModel(
18
+ max_tokens=2048,
19
+ temperature=0.5,
20
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct"
 
21
  )
22
 
23
+ # Wrap the image generation function as a tool
24
+ @tool
25
+ def generate_image(prompt: str) -> str:
26
+ """Generate an image from a text prompt using the image generation tool.
 
 
27
 
28
+ Args:
29
+ prompt: A description of the image to generate.
30
+ """
31
+ try:
32
+ return image_generation_tool(prompt)
33
+ except Exception as e:
34
+ return f"Image generation failed: {str(e)}"
35
+
36
+ # Define the agent with the tools
37
  agent = CodeAgent(
38
  model=model,
39
+ tools=[final_answer, generate_image],
 
40
  max_steps=6,
41
  verbosity_level=1,
 
 
 
 
42
  prompt_templates=prompt_templates
43
  )
44
 
45
+ # Launch the UI
46
+ GradioUI(agent).launch()