Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -3,6 +3,7 @@ import requests
|
|
| 3 |
from markdownify import markdownify
|
| 4 |
from requests.exceptions import RequestException
|
| 5 |
from smolagents import tool
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
@tool
|
|
@@ -32,3 +33,40 @@ def visit_webpage(url: str) -> str:
|
|
| 32 |
return f"Error fetching the webpage: {str(e)}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"An unexpected error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from markdownify import markdownify
|
| 4 |
from requests.exceptions import RequestException
|
| 5 |
from smolagents import tool
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
|
| 9 |
@tool
|
|
|
|
| 33 |
return f"Error fetching the webpage: {str(e)}"
|
| 34 |
except Exception as e:
|
| 35 |
return f"An unexpected error occurred: {str(e)}"
|
| 36 |
+
|
| 37 |
+
@tool
|
| 38 |
+
def analyze_image(url: str, prompt: str) -> str:
|
| 39 |
+
"""Uses a vision model to identify features in an describe an image.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
url: The URL of the image to analyze
|
| 43 |
+
prompt: Specific questions or things you are looking for in the image. Can also specify how to format a response. The model will return a general description if this is blank.
|
| 44 |
+
|
| 45 |
+
Retruns:
|
| 46 |
+
Answers to your question(s) or else a textual description of the image
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
model_id = "Qwen/Qwen2.5-VL-32B-Instruct"
|
| 50 |
+
client = InferenceClient()
|
| 51 |
+
image_url = "https://agents-course-unit4-scoring.hf.space/files/cca530fc-4052-43b2-b130-b30968d8aa44"
|
| 52 |
+
|
| 53 |
+
if prompt is None:
|
| 54 |
+
prompt = "Describe the content of the image in detail."
|
| 55 |
+
|
| 56 |
+
model_prompt = [
|
| 57 |
+
{
|
| 58 |
+
"role": "user",
|
| 59 |
+
"content": [
|
| 60 |
+
{"type": "image_url", "image_url": {"url": image_url}},
|
| 61 |
+
{"type": "text", "text": prompt}
|
| 62 |
+
]
|
| 63 |
+
}
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
response = client.chat_completion(
|
| 67 |
+
model=model_id,
|
| 68 |
+
messages=model_prompt,
|
| 69 |
+
max_tokens=1000,
|
| 70 |
+
temperature=0.7
|
| 71 |
+
)
|
| 72 |
+
description = response.choices[0].message.content
|