Yuthana commited on
Commit
dccda33
·
verified ·
1 Parent(s): 5994087

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -9,32 +9,35 @@ 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:
13
- """A tool that searches for 3 PDF files related to the user's request using DuckDuckGoSearchTool.
14
 
15
  Args:
16
- arg1: the first argument (string), interpreted as the search query.
17
- arg2: the second argument (integer), potentially controlling how many results to fetch.
18
  """
19
- # We'll use DuckDuckGoSearchRun from langchain to perform the actual search.
20
- search = DuckDuckGoSearchRun()
21
-
22
- # Construct the query, searching specifically for PDF documents.
23
- query = f"{arg1} filetype:pdf"
24
-
25
- # Use arg2 to limit the number of results to fetch (if you prefer, you can hardcode num_results=3).
26
- num_results = min(arg2, 3) if arg2 > 0 else 3
 
 
 
 
 
27
 
28
- # Execute the search
29
- results = search.run(query, num_results=num_results)
 
 
 
 
30
 
31
- # If results is a string describing the search outcome, just return it.
32
- # If results is a list of links, you might parse them. For simplicity, we'll cast it to string and return.
33
- if not results:
34
- return "No PDF files found."
35
-
36
- # Convert the results to a string for returning; you can format or parse further if desired.
37
- return f"Top {num_results} PDF results for '{arg1}':\n\n{str(results)}"
38
 
39
  @tool
40
  def get_current_time_in_timezone(timezone: str) -> str:
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ def search_image_urls(query: str, num_images: int) -> str:
13
+ """A tool that searches for image URLs using DuckDuckGo.
14
 
15
  Args:
16
+ query: The user-provided search keyword(s), e.g. 'cat'
17
+ num_images: The number of image URLs to return
18
  """
19
+ from duckduckgo_search import ddg_images
20
+
21
+ # Validate number of images
22
+ if num_images < 1:
23
+ num_images = 1
24
+ if num_images > 5:
25
+ # Let's limit to 5 for demonstration
26
+ num_images = 5
27
+
28
+ # Perform the image search
29
+ results = ddg_images(query, max_results=num_images)
30
+ if not results:
31
+ return f"No images found for '{query}'."
32
 
33
+ # Format results in a neat way
34
+ output_lines = []
35
+ for index, result in enumerate(results, start=1):
36
+ thumbnail = result.get("image", "No image URL found")
37
+ title = result.get("title", "No title")
38
+ output_lines.append(f"{index}. {title}\n {thumbnail}")
39
 
40
+ return "\n\n".join(output_lines)
 
 
 
 
 
 
41
 
42
  @tool
43
  def get_current_time_in_timezone(timezone: str) -> str: