temesgen0 commited on
Commit
84de8d5
·
verified ·
1 Parent(s): 66dc195

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -25
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
@@ -6,61 +6,80 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- # Custom tool for web search to find authentic Ethiopian food images
 
10
  @tool
11
- def search_ethiopian_food_authentic(food_name: str) -> str:
12
- """Search for authentic Ethiopian food images to understand exact appearance
13
  Args:
14
- food_name: Name of the Ethiopian food (e.g., "injera", "doro wat", "tibs")
15
  """
16
  try:
17
- # Use web search to find authentic images and descriptions
18
- search_query = f"authentic Ethiopian {food_name} traditional appearance ingredients presentation"
19
- # This would connect to your web_search_exa tool
20
- return f"Searching for authentic Ethiopian {food_name}..."
 
21
  except Exception as e:
22
- return f"Error searching for {food_name}: {str(e)}"
23
 
24
- # Custom tool for generating Ethiopian food images
25
  @tool
26
- def generate_ethiopian_food_image(food_description: str, style: str = "authentic") -> str:
27
- """Generate an authentic Ethiopian food image based on research
28
  Args:
29
- food_description: Detailed description of the food to generate
30
- style: Style of image - "authentic", "restaurant", "home-cooked"
31
  """
32
  try:
33
- # This will use the gr1_qwen_image_fast_generate_image tool
34
- return f"Generating {style} Ethiopian food image: {food_description}"
 
 
35
  except Exception as e:
36
- return f"Error generating image: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  final_answer = FinalAnswerTool()
39
 
 
 
 
 
 
 
40
  # Model configuration
41
  model = HfApiModel(
42
  max_tokens=2096,
43
  temperature=0.5,
44
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
45
  custom_role_conversions=None,
46
  )
47
 
48
- # Load prompt templates
49
  with open("prompts.yaml", 'r') as stream:
50
  prompt_templates = yaml.safe_load(stream)
51
 
52
- # Create the Ethiopian food agent
53
  agent = CodeAgent(
54
  model=model,
55
- tools=[search_ethiopian_food_authentic, generate_ethiopian_food_image, final_answer],
56
  max_steps=6,
57
  verbosity_level=1,
58
  grammar=None,
59
  planning_interval=None,
60
- name="ethiopian_food_image_generator",
61
- description="Generates authentic Ethiopian food images by researching exact appearance first",
62
  prompt_templates=prompt_templates
63
  )
64
 
65
- # Launch the Gradio UI
66
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
+ # Define custom tools
10
+
11
  @tool
12
+ def search_images(query: str) -> str:
13
+ """A tool that searches for images using DuckDuckGo and returns URLs of the top 3 results.
14
  Args:
15
+ query: The search query for images (e.g., 'Ethiopian injera food').
16
  """
17
  try:
18
+ from duckduckgo_search import DDGS
19
+ with DDGS() as ddgs:
20
+ results = ddgs.images(query, max_results=3)
21
+ image_urls = [result['image'] for result in results]
22
+ return "\n".join(image_urls)
23
  except Exception as e:
24
+ return f"Error searching images: {str(e)}"
25
 
 
26
  @tool
27
+ def visit_webpage(url: str) -> str:
28
+ """A tool that visits a webpage and returns its content in markdown format.
29
  Args:
30
+ url: The URL of the webpage to visit.
 
31
  """
32
  try:
33
+ response = requests.get(url)
34
+ response.raise_for_status()
35
+ from markdownify import markdownify as md
36
+ return md(response.text)
37
  except Exception as e:
38
+ return f"Error visiting webpage: {str(e)}"
39
+
40
+ @tool
41
+ def get_current_time_in_timezone(timezone: str) -> str:
42
+ """A tool that fetches the current local time in a specified timezone.
43
+ Args:
44
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
45
+ """
46
+ try:
47
+ tz = pytz.timezone(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
  final_answer = FinalAnswerTool()
54
 
55
+ # Load image generation tool from Hub
56
+ image_generator = load_tool("agents-course/text-to-image", trust_remote_code=True)
57
+
58
+ # Instantiate web search tool
59
+ web_search = DuckDuckGoSearchTool()
60
+
61
  # Model configuration
62
  model = HfApiModel(
63
  max_tokens=2096,
64
  temperature=0.5,
65
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Use another endpoint if overloaded
66
  custom_role_conversions=None,
67
  )
68
 
 
69
  with open("prompts.yaml", 'r') as stream:
70
  prompt_templates = yaml.safe_load(stream)
71
 
72
+ # Initialize the agent with all tools
73
  agent = CodeAgent(
74
  model=model,
75
+ tools=[web_search, search_images, visit_webpage, image_generator, final_answer],
76
  max_steps=6,
77
  verbosity_level=1,
78
  grammar=None,
79
  planning_interval=None,
80
+ name=None,
81
+ description=None,
82
  prompt_templates=prompt_templates
83
  )
84
 
 
85
  GradioUI(agent).launch()