AdamyaG commited on
Commit
156d9d6
·
verified ·
1 Parent(s): 48306ac

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +63 -61
tools.py CHANGED
@@ -1,61 +1,63 @@
1
- import re
2
- import requests
3
- from PIL import Image
4
- from IPython.display import display
5
- from markdownify import markdownify
6
- from requests.exceptions import RequestException
7
- from smolagents import (
8
- Tool,
9
- tool
10
- )
11
-
12
- @tool
13
- def visit_webpage(url: str) -> str:
14
- """Visits a webpage at the given URL and returns its content as a markdown string.
15
-
16
- Args:
17
- url: The URL of the webpage to visit.
18
-
19
- Returns:
20
- The content of the webpage converted to Markdown, or an error message if the request fails.
21
- """
22
- try:
23
- # Send a GET request to the URL
24
- response = requests.get(url)
25
- response.raise_for_status() # Raise an exception for bad status codes
26
-
27
- # Convert the HTML content to Markdown
28
- markdown_content = markdownify(response.text).strip()
29
-
30
- # Remove multiple line breaks
31
- markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
32
-
33
- return markdown_content
34
-
35
- except RequestException as e:
36
- return f"Error fetching the webpage: {str(e)}"
37
- except Exception as e:
38
- return f"An unexpected error occurred: {str(e)}"
39
-
40
- @tool
41
- def image_diplay_tool(image_path : str) -> object:
42
- """
43
- This is a tool that returns the image object and displays it
44
-
45
- Args:
46
- image_path: The images's path for displaying.
47
- """
48
- try:
49
- # Open the .webp image using Pillow
50
- img = Image.open(image_path)
51
- # Display the image
52
- display(img)
53
- except Exception as e:
54
- print(f"Error displaying image: {e}")
55
- return img
56
-
57
- image_generation_tool = Tool.from_space(
58
- "black-forest-labs/FLUX.1-schnell",
59
- name="image_generator",
60
- description="Generate an image from a prompt and return its path. Make sure to improve the prompt. Another tool MUST be called for displaying image"
61
- )
 
 
 
1
+ import re
2
+ import requests
3
+ import streamlit as st
4
+ from PIL import Image
5
+ from IPython.display import display
6
+ from markdownify import markdownify
7
+ from requests.exceptions import RequestException
8
+ from smolagents import (
9
+ Tool,
10
+ tool
11
+ )
12
+
13
+ @tool
14
+ def visit_webpage(url: str) -> str:
15
+ """Visits a webpage at the given URL and returns its content as a markdown string.
16
+
17
+ Args:
18
+ url: The URL of the webpage to visit.
19
+
20
+ Returns:
21
+ The content of the webpage converted to Markdown, or an error message if the request fails.
22
+ """
23
+ try:
24
+ # Send a GET request to the URL
25
+ response = requests.get(url)
26
+ response.raise_for_status() # Raise an exception for bad status codes
27
+
28
+ # Convert the HTML content to Markdown
29
+ markdown_content = markdownify(response.text).strip()
30
+
31
+ # Remove multiple line breaks
32
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
33
+
34
+ return markdown_content
35
+
36
+ except RequestException as e:
37
+ return f"Error fetching the webpage: {str(e)}"
38
+ except Exception as e:
39
+ return f"An unexpected error occurred: {str(e)}"
40
+
41
+ @tool
42
+ def image_diplay_tool(image_path : str) -> object:
43
+ """
44
+ This is a tool that returns the image object and displays it
45
+
46
+ Args:
47
+ image_path: The images's path for displaying.
48
+ """
49
+ try:
50
+ # Open the .webp image using Pillow
51
+ img = Image.open(image_path)
52
+ # Display the image
53
+ # display(img)
54
+ st.image(image_path)
55
+ except Exception as e:
56
+ print(f"Error displaying image: {e}")
57
+ return img
58
+
59
+ image_generation_tool = Tool.from_space(
60
+ "black-forest-labs/FLUX.1-schnell",
61
+ name="image_generator",
62
+ description="Generate an image from a prompt and return its path. Make sure to improve the prompt. Another tool MUST be called for displaying image"
63
+ )