Spaces:
Sleeping
Sleeping
File size: 7,797 Bytes
9b5b26a a6ba120 9b5b26a a6ba120 c19d193 a6ba120 8fe992b 9b5b26a a6ba120 9b5b26a 8c01ffb a6ba120 8c01ffb 6aae614 e121372 a6ba120 13d500a 8c01ffb 9b5b26a a6ba120 8c01ffb 861422e 9b5b26a 8c01ffb 8fe992b a6ba120 8c01ffb 861422e 8fe992b 8c01ffb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
import datetime
import os
import pytz
import requests
import yaml
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
from Gradio_UI import GradioUI
from classes.get_url_report import GetURLReportResponse, Data, Attributes, Stats
from classes.ip_address_report import IPAddressReport, TotalVotes, AnalysisStats
from classes.scan_url import DataAnalysis, Links, ScanResponse
from tools.final_answer import FinalAnswerTool
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
# Create timezone object
tz = pytz.timezone(timezone)
# Get current time in that timezone
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
@tool
def get_my_ip_address() -> str:
"""
Retrieves the public IP address of the machine running this code.
Returns:
str: The public IP address.
Raises:
Exception: If the request to the external service fails.
"""
url = "https://api.ipify.org?format=json"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data["ip"]
else:
raise Exception(f"Failed to retrieve IP address: {response.status_code} - {response.text}")
@tool
def get_ip_address_report(ip_address: str) -> IPAddressReport:
"""
Fetches the IP address report from the VirusTotal API and returns it as an IPAddressReport object.
Args:
ip_address: The IP address to fetch the report for.
Returns:
IPAddressReport: An object containing the IP address report.
Raises:
Exception: If the request to the VirusTotal API fails.
"""
url = f"https://www.virustotal.com/api/v3/ip_addresses/{ip_address}"
headers = {
"accept": "application/json",
"x-apikey": os.getenv('VT_API_KEY')
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
report = IPAddressReport(
id=data["data"]["id"],
type=data["data"]["type"],
reputation=data["data"]["attributes"]["reputation"],
continent=data["data"]["attributes"]["continent"],
as_owner=data["data"]["attributes"]["as_owner"],
country=data["data"]["attributes"]["country"],
tags=data["data"]["attributes"]["tags"],
total_votes=TotalVotes(
harmless=data["data"]["attributes"]["total_votes"]["harmless"],
malicious=data["data"]["attributes"]["total_votes"]["malicious"]
),
network=data["data"]["attributes"]["network"],
last_analysis_stats=AnalysisStats(
malicious=data["data"]["attributes"]["last_analysis_stats"]["malicious"],
suspicious=data["data"]["attributes"]["last_analysis_stats"]["suspicious"],
undetected=data["data"]["attributes"]["last_analysis_stats"]["undetected"],
harmless=data["data"]["attributes"]["last_analysis_stats"]["harmless"],
timeout=data["data"]["attributes"]["last_analysis_stats"]["timeout"]
)
)
return report
else:
raise Exception(f"Failed to retrieve data: {response.status_code} - {response.text}")
@tool
def scan_url(url: str) -> ScanResponse:
"""
Request a scan of a given URL using the VirusTotal API.
Args:
url: The URL to scan.
Returns:
ScanResponse: The response from the VirusTotal API.
Raises:
Exception: If the request to the external service fails.
"""
endpoint = "https://www.virustotal.com/api/v3/urls"
payload = { "url" : url }
headers = {
"accept": "application/json",
"x-apikey": os.getenv('VT_API_KEY') ,
"content-type": "application/x-www-form-urlencoded"
}
# Send a POST request to the VirusTotal API
response = requests.post(endpoint, headers=headers, data=payload)
print(response.text)
try:
# Raise an exception if the request was unsuccessful
response.raise_for_status()
response_json = response.json()
return ScanResponse(
data=DataAnalysis(
type=response_json["data"]["type"],
id=response_json["data"]["id"],
links=Links(self_url=response_json["data"]["links"]["self"])
)
)
except requests.exceptions.RequestException as e:
# Handle any errors that occur during the request
raise Exception(f"Failed to retrieve data: {response.status_code} - {response.text}")
@tool
def get_scan_report(scan: ScanResponse) -> GetURLReportResponse:
"""
Fetch a report of a scan of a given URL using the VirusTotal API.
Args:
scan: The ScanResponse object returned by calling scan_url tool.
Returns:
GetURLReportResponse: The response from the VirusTotal API.
Raises:
Exception: If the request to the external service fails.
"""
headers = {
"accept": "application/json",
"x-apikey": os.getenv('VT_API_KEY')
}
# Send a GET request to the VirusTotal API
response = requests.get(scan.data.links.self_url, headers=headers)
print(response.text)
try:
# Raise an exception if the request was unsuccessful
response.raise_for_status()
response_json = response.json()
# Creating an instance of the data class from the JSON response
response = GetURLReportResponse(
data=Data(
id=response_json["data"]["id"],
type=response_json["data"]["type"],
attributes=Attributes(
date=response_json["data"]["attributes"]["date"],
status=response_json["data"]["attributes"]["status"],
stats=Stats(
malicious=response_json["data"]["attributes"]["stats"]["malicious"],
suspicious=response_json["data"]["attributes"]["stats"]["suspicious"],
undetected=response_json["data"]["attributes"]["stats"]["undetected"],
harmless=response_json["data"]["attributes"]["stats"]["harmless"],
timeout=response_json["data"]["attributes"]["stats"]["timeout"]
)
)
)
)
return response
except requests.exceptions.RequestException as e:
# Handle any errors that occur during the request
raise Exception(f"Failed to retrieve data: {response.status_code} - {response.text}")
final_answer = FinalAnswerTool()
model = HfApiModel(
token=os.getenv('HF_TOKEN'),
max_tokens=2096,
temperature=0.5,
model_id=os.getenv('MODEL'),
custom_role_conversions=None,
)
# Import tool from Hub
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
web_search_tool = DuckDuckGoSearchTool()
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, get_my_ip_address, get_ip_address_report, scan_url, get_scan_report], ## add your tools here (don't remove final answer)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch() |