Mo0ostafa commited on
Commit
b2456c5
·
verified ·
1 Parent(s): ae7a494

add the image_generator tool

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -4,20 +4,14 @@ 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 my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
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
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
@@ -33,8 +27,26 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
 
 
38
 
39
  # 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:
40
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
@@ -48,14 +60,14 @@ custom_role_conversions=None,
48
 
49
 
50
  # Import tool from Hub
51
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
 
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from diffusers import StableDiffusionPipeline
8
+ import torch
9
 
10
+ from tools.final_answer import FinalAnswerTool
11
+ from tools.visit_webpage import VisitWebpageTool # Import tool function
12
+ from tools.web_search import DuckDuckGoSearchTool # Import tool function
13
  from Gradio_UI import GradioUI
14
 
 
 
 
 
 
 
 
 
 
 
 
15
  @tool
16
  def get_current_time_in_timezone(timezone: str) -> str:
17
  """A tool that fetches the current local time in a specified timezone.
 
27
  except Exception as e:
28
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
29
 
30
+ @tool
31
+ def image_generator(prompt: str, model_name: str = "CompVis/stable-diffusion-v1-4"):
32
+ """A tool that Generate an image from a text prompt using a free Hugging Face model.
33
+
34
+ Args:
35
+ prompt (str): Text prompt to generate image.
36
+ model_name (str): Model to use from Hugging Face hub (default is Stable Diffusion v1.4).
37
+
38
+ Returns:
39
+ PIL.Image.Image: Generated image.
40
+ """
41
+ pipe = StableDiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16)
42
+ pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
43
+
44
+ image = pipe(prompt).images[0]
45
+ return image
46
 
47
  final_answer = FinalAnswerTool()
48
+ visit_webpage = VisitWebpageTool()
49
+ web_search = DuckDuckGoSearchTool()
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'
 
60
 
61
 
62
  # Import tool from Hub
63
+ # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
64
 
65
  with open("prompts.yaml", 'r') as stream:
66
  prompt_templates = yaml.safe_load(stream)
67
 
68
  agent = CodeAgent(
69
  model=model,
70
+ tools=[final_answer, visit_webpage, web_search, get_current_time_in_timezone, image_generator],
71
  max_steps=6,
72
  verbosity_level=1,
73
  grammar=None,