Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
@@ -1,61 +1,63 @@
|
|
1 |
-
import re
|
2 |
-
import requests
|
3 |
-
|
4 |
-
from
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
response.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
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 |
+
)
|