Spaces:
Running
Running
File size: 21,573 Bytes
717a9f0 3750a8d 717a9f0 44d06c0 717a9f0 4cda29c 58a3354 717a9f0 3750a8d 717a9f0 e397d59 717a9f0 e397d59 44d06c0 3750a8d 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 4cda29c 717a9f0 3750a8d 717a9f0 e397d59 717a9f0 4cda29c 717a9f0 58a3354 717a9f0 58a3354 717a9f0 3750a8d 717a9f0 44d06c0 717a9f0 3750a8d 44d06c0 717a9f0 58a3354 44d06c0 717a9f0 44d06c0 58a3354 717a9f0 58a3354 717a9f0 58a3354 717a9f0 3750a8d 717a9f0 44d06c0 717a9f0 44d06c0 717a9f0 58a3354 44d06c0 717a9f0 44d06c0 717a9f0 44d06c0 717a9f0 e397d59 717a9f0 e397d59 717a9f0 4cda29c 717a9f0 44d06c0 717a9f0 3750a8d 44d06c0 717a9f0 44d06c0 717a9f0 44d06c0 717a9f0 44d06c0 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 4cda29c 717a9f0 2d96a84 717a9f0 2d96a84 717a9f0 58a3354 2d96a84 717a9f0 2d96a84 717a9f0 4cda29c 717a9f0 4cda29c 717a9f0 2d96a84 58a3354 717a9f0 2d96a84 717a9f0 58a3354 2d96a84 717a9f0 2d96a84 717a9f0 58a3354 717a9f0 58a3354 4cda29c 717a9f0 4cda29c 717a9f0 4cda29c 717a9f0 4cda29c 717a9f0 4cda29c 717a9f0 58a3354 717a9f0 4cda29c 717a9f0 4cda29c 717a9f0 4cda29c 717a9f0 4cda29c 717a9f0 e397d59 717a9f0 e397d59 717a9f0 e397d59 717a9f0 58a3354 717a9f0 44d06c0 717a9f0 4cda29c 717a9f0 e397d59 717a9f0 4cda29c 717a9f0 e397d59 717a9f0 e397d59 717a9f0 58a3354 717a9f0 e397d59 58a3354 717a9f0 58a3354 717a9f0 58a3354 717a9f0 e397d59 717a9f0 ff0a824 717a9f0 ff0a824 717a9f0 |
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
"""
Ollama Instance & Model Scanner for Hugging Face Space
This application scans for publicly accessible Ollama instances, retrieves model information,
and provides a secure interface for browsing discovered models.
Security Architecture:
- Server-side authorization based on environment variables
- Strict input sanitization
- Comprehensive error handling
- Asynchronous endpoint checking
- Efficient dataset management
"""
import os
import re
import json
import asyncio
import logging
import gradio as gr
import shodan
import aiohttp
from datasets import load_dataset, Dataset
from typing import Dict, List, Optional, Any, Tuple, Union
from datetime import datetime
from functools import wraps
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
logger = logging.getLogger(__name__)
# Security layer - Authorization functions
def authorization_required(func):
"""
Decorator that enforces server-side authorization for protected functions.
Authorization is determined by environment variables, not client parameters.
Args:
func: The function to protect with authorization
Returns:
A wrapped function that performs authorization check
"""
@wraps(func)
def wrapper(*args, **kwargs):
if not verify_admin_authorization():
logger.warning(f"Unauthorized access attempt to {func.__name__}")
return {"error": "Unauthorized access"} if kwargs.get("return_error", False) else None
return func(*args, **kwargs)
return wrapper
def verify_admin_authorization() -> bool:
"""
Perform server-side verification of admin authorization.
Authorization is based on environment variables, not client data.
Returns:
bool: True if valid admin credentials exist
"""
try:
# Check for the existence of the Shodan API key
api_key = os.getenv("SHODAN_API_KEY")
hf_token = os.getenv("HF_TOKEN")
return (api_key is not None and
len(api_key.strip()) > 10 and
hf_token is not None and
len(hf_token.strip()) > 10)
except Exception as e:
logger.error(f"Error verifying admin authorization: {str(e)}")
return False
# Security layer - Input validation
def sanitize_input(input_string: str) -> str:
"""
Sanitize user input to prevent injection attacks.
Args:
input_string: User input string to sanitize
Returns:
str: Sanitized string
"""
if not isinstance(input_string, str):
return ""
# Remove potentially harmful characters
sanitized = re.sub(r'[^\w\s\-\.]', '', input_string)
# Limit length to prevent DoS
return sanitized[:100]
def get_env_variables() -> Dict[str, str]:
"""
Get all required environment variables.
Returns:
Dict[str, str]: Dictionary containing environment variables
Raises:
ValueError: If any required environment variable is missing
"""
env_vars = {
"SHODAN_API_KEY": os.getenv("SHODAN_API_KEY"),
"SHODAN_QUERY": os.getenv("SHODAN_QUERY", "product:Ollama port:11434"),
"HF_TOKEN": os.getenv("HF_TOKEN")
}
missing_vars = [name for name, value in env_vars.items() if not value]
if missing_vars:
error_msg = f"Missing required environment variables: {', '.join(missing_vars)}"
logger.error(error_msg)
raise ValueError(error_msg)
return env_vars
# Data access layer
def load_or_create_dataset() -> Dataset:
"""
Load the dataset from Hugging Face Hub or create it if it doesn't exist.
Returns:
Dataset: Loaded or created dataset
Raises:
Exception: If dataset loading or creation fails
"""
try:
# Attempt to get environment variables - this will raise ValueError if missing
env_vars = get_env_variables()
logger.info("Attempting to load dataset from Hugging Face Hub")
dataset = load_dataset("latterworks/llama_checker_results", use_auth_token=env_vars["HF_TOKEN"])
dataset = dataset['train']
logger.info(f"Successfully loaded dataset with {len(dataset)} entries")
return dataset
except ValueError as e:
# Re-raise environment variable errors
raise
except FileNotFoundError:
# Only create dataset if admin authorization is verified
if not verify_admin_authorization():
logger.error("Unauthorized attempt to create dataset")
raise ValueError("Unauthorized: Only admins can create the dataset")
logger.info("Dataset not found, creating a new one")
env_vars = get_env_variables()
dataset = Dataset.from_dict({
"ip": [],
"port": [],
"country": [],
"region": [],
"org": [],
"models": []
})
dataset.push_to_hub("latterworks/llama_checker_results", token=env_vars["HF_TOKEN"])
logger.info("Created and pushed empty dataset to Hugging Face Hub")
# Reload the dataset to ensure consistency
dataset = load_dataset("latterworks/llama_checker_results", use_auth_token=env_vars["HF_TOKEN"])['train']
return dataset
except Exception as e:
error_msg = f"Failed to load or create dataset: {str(e)}"
logger.error(error_msg)
raise
async def check_single_endpoint(ip: str, port: int, timeout: int = 5) -> Optional[List[Dict[str, Any]]]:
"""
Check a single Ollama endpoint for available models.
Args:
ip: IP address of the Ollama instance
port: Port number of the Ollama instance
timeout: Timeout in seconds for the HTTP request
Returns:
Optional[List[Dict[str, Any]]]: List of model information dictionaries, or None if endpoint check fails
"""
url = f"http://{ip}:{port}/api/tags"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=timeout) as response:
if response.status == 200:
data = await response.json()
if "models" in data and isinstance(data["models"], list):
logger.info(f"Successfully retrieved {len(data['models'])} models from {ip}:{port}")
return data["models"]
else:
logger.warning(f"Unexpected response format from {ip}:{port}")
else:
logger.warning(f"Received status code {response.status} from {ip}:{port}")
except aiohttp.ClientError as e:
logger.warning(f"Connection error for {ip}:{port}: {str(e)}")
except asyncio.TimeoutError:
logger.warning(f"Connection timeout for {ip}:{port}")
except Exception as e:
logger.warning(f"Unexpected error checking {ip}:{port}: {str(e)}")
return None
@authorization_required
async def check_ollama_endpoints(dataset: Dataset, progress: Optional[gr.Progress] = None) -> Dataset:
"""
Check all Ollama endpoints in the dataset for available models.
Requires admin authorization.
Args:
dataset: Dataset containing Ollama endpoints
progress: Optional Gradio progress bar
Returns:
Dataset: Updated dataset with model information
"""
if progress:
progress(0, desc="Preparing to check endpoints...")
# Build a list of tasks to execute
total_endpoints = len(dataset)
tasks = []
for i, item in enumerate(dataset):
ip = item["ip"]
port = item["port"]
tasks.append(check_single_endpoint(ip, port))
# Execute tasks in batches to avoid overwhelming resources
batch_size = 10
updated_dataset = dataset.copy()
for i in range(0, len(tasks), batch_size):
if progress:
progress(i / len(tasks), desc=f"Checking endpoints {i+1}-{min(i+batch_size, len(tasks))} of {len(tasks)}...")
batch_tasks = tasks[i:i+batch_size]
batch_results = await asyncio.gather(*batch_tasks)
for j, result in enumerate(batch_results):
idx = i + j
if idx < len(dataset):
if result:
updated_dataset = updated_dataset.add_item({
"ip": dataset[idx]["ip"],
"port": dataset[idx]["port"],
"country": dataset[idx]["country"],
"region": dataset[idx]["region"],
"org": dataset[idx]["org"],
"models": result
})
if progress:
progress(1.0, desc="Endpoint checking complete!")
logger.info(f"Checked {total_endpoints} endpoints, found models on {sum(1 for item in updated_dataset if item['models'])} endpoints")
# Push updated dataset to Hugging Face Hub
env_vars = get_env_variables()
updated_dataset.push_to_hub("latterworks/llama_checker_results", token=env_vars["HF_TOKEN"])
logger.info("Successfully pushed updated dataset to Hugging Face Hub")
return updated_dataset
@authorization_required
def scan_shodan(progress: Optional[gr.Progress] = None) -> str:
"""
Scan Shodan for Ollama instances and update the dataset.
Requires admin authorization.
Args:
progress: Optional Gradio progress bar
Returns:
str: Status message
"""
try:
# Get environment variables
env_vars = get_env_variables()
# Load dataset
dataset = load_or_create_dataset()
# Initialize Shodan API client
api = shodan.Shodan(env_vars["SHODAN_API_KEY"])
query = env_vars["SHODAN_QUERY"]
if progress:
progress(0, desc="Starting Shodan search...")
# Get total results count
count_result = api.count(query)
total_results = count_result.get('total', 0)
if total_results == 0:
return "No Ollama instances found on Shodan."
logger.info(f"Found {total_results} potential Ollama instances on Shodan")
# Search Shodan
new_instances = []
results_processed = 0
for result in api.search_cursor(query):
results_processed += 1
if progress:
progress(results_processed / total_results,
desc=f"Processing Shodan result {results_processed}/{total_results}")
ip = result.get('ip_str')
port = result.get('port', 11434)
# Skip if instance already exists in dataset
if any(item["ip"] == ip and item["port"] == port for item in dataset):
continue
# Extract location information
country = result.get('location', {}).get('country_name', '')
region = result.get('location', {}).get('region_name', '')
org = result.get('org', '')
new_instances.append({
"ip": ip,
"port": port,
"country": country,
"region": region,
"org": org,
"models": []
})
if progress:
progress(1.0, desc="Shodan search complete!")
# Add new instances to dataset
updated_dataset = dataset.copy()
for instance in new_instances:
updated_dataset = updated_dataset.add_item(instance)
logger.info(f"Added {len(new_instances)} new instances to dataset")
# Check Ollama endpoints asynchronously
if new_instances:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
updated_dataset = loop.run_until_complete(check_ollama_endpoints(updated_dataset, progress))
loop.close()
status_message = f"Scan complete! Found {len(new_instances)} new Ollama instances."
return status_message
except shodan.APIError as e:
error_msg = f"Shodan API error: {str(e)}"
logger.error(error_msg)
return error_msg
except Exception as e:
error_msg = f"Error during Shodan scan: {str(e)}"
logger.error(error_msg)
return error_msg
def get_unique_values(dataset: Dataset, field: str) -> List[str]:
"""
Get unique values for a specific field in the dataset.
Args:
dataset: Dataset to extract values from
field: Field name to extract values from
Returns:
List[str]: List of unique values
"""
unique_values = set()
if field == "family" or field == "parameter_size" or field == "quantization_level":
for item in dataset:
models = item.get("models", [])
if not models:
continue
for model in models:
details = model.get("details", {})
if details and field in details:
value = details.get(field)
if value:
unique_values.add(value)
return sorted(list(unique_values))
def search_models(dataset: Dataset, name_search: str = "", family: str = "", parameter_size: str = "") -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
"""
Search for models in the dataset based on filters.
Authorization is determined server-side.
Args:
dataset: Dataset to search
name_search: Model name search string
family: Model family filter
parameter_size: Parameter size filter
Returns:
Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]: Filtered model list and detailed model list
"""
# Server-side authorization check
is_admin = verify_admin_authorization()
name_search = sanitize_input(name_search).lower()
family = sanitize_input(family)
parameter_size = sanitize_input(parameter_size)
filtered_models = []
detailed_models = []
for item in dataset:
models = item.get("models", [])
if not models:
continue
ip = item.get("ip", "")
port = item.get("port", 0)
country = item.get("country", "")
region = item.get("region", "")
org = item.get("org", "")
for model in models:
model_name = model.get("name", "").lower()
details = model.get("details", {})
model_family = details.get("family", "")
model_parameter_size = details.get("parameter_size", "")
model_quantization = details.get("quantization_level", "")
model_size = model.get("size", 0)
model_size_gb = round(model_size / (1024**3), 2) if model_size else 0
# Apply filters
if name_search and name_search not in model_name:
continue
if family and family != model_family:
continue
if parameter_size and parameter_size != model_parameter_size:
continue
# Prepare filtered model entry
filtered_model = {
"name": model.get("name", ""),
"family": model_family,
"parameter_size": model_parameter_size,
"quantization_level": model_quantization,
"size_gb": model_size_gb
}
# Add IP and port information only for admins - server-side check
if is_admin:
filtered_model["ip"] = ip
filtered_model["port"] = port
filtered_models.append(filtered_model)
# Prepare detailed model entry
detailed_model = {
"name": model.get("name", ""),
"family": model_family,
"parameter_size": model_parameter_size,
"quantization_level": model_quantization,
"size_gb": model_size_gb,
"digest": model.get("digest", ""),
"modified_at": model.get("modified_at", ""),
"country": country,
"region": region,
"org": org
}
# Add IP and port information only for admins - server-side check
if is_admin:
detailed_model["ip"] = ip
detailed_model["port"] = port
detailed_models.append(detailed_model)
return filtered_models, detailed_models
def create_ui() -> gr.Blocks:
"""
Create the Gradio user interface with server-side authorization.
Returns:
gr.Blocks: Gradio interface
"""
# Load dataset
try:
dataset = load_or_create_dataset()
except Exception as e:
# Fallback to empty dataset if loading fails
logger.error(f"Failed to load dataset: {str(e)}")
dataset = Dataset.from_dict({
"ip": [],
"port": [],
"country": [],
"region": [],
"org": [],
"models": []
})
# Server-side authorization check
is_admin = verify_admin_authorization()
# Get unique values for dropdowns
families = [""] + get_unique_values(dataset, "family")
parameter_sizes = [""] + get_unique_values(dataset, "parameter_size")
# Initial search results
initial_results, initial_details = search_models(dataset)
with gr.Blocks(title="Ollama Instance & Model Browser") as app:
gr.Markdown("# Ollama Instance & Model Browser")
with gr.Tabs() as tabs:
with gr.Tab("Browse Models"):
with gr.Row():
with gr.Column(scale=1):
name_search = gr.Textbox(label="Model Name Search")
family_dropdown = gr.Dropdown(
choices=families,
label="Model Family",
value=""
)
parameter_size_dropdown = gr.Dropdown(
choices=parameter_sizes,
label="Parameter Size",
value=""
)
search_button = gr.Button("Search Models")
with gr.Row():
model_results = gr.DataFrame(
value=initial_results,
label="Model Results",
interactive=False
)
with gr.Row():
model_details = gr.JSON(label="Model Details")
def search_callback(name, family, parameter_size):
results, details = search_models(dataset, name, family, parameter_size)
return results, None
def select_model(evt: gr.SelectData):
results, details = search_models(dataset, name_search.value,
family_dropdown.value,
parameter_size_dropdown.value)
if evt.index[0] < len(details):
return details[evt.index[0]]
return None
search_button.click(
search_callback,
inputs=[name_search, family_dropdown, parameter_size_dropdown],
outputs=[model_results, model_details]
)
model_results.select(
select_model,
None,
model_details
)
# Only show Shodan Scan tab for admins - server-side check
if is_admin:
with gr.Tab("Shodan Scan"):
gr.Markdown("## Scan for Ollama Instances")
gr.Markdown("**Note:** This scan will update the dataset with new Ollama instances.")
scan_button = gr.Button("Start Scan")
scan_output = gr.Textbox(label="Scan Status")
scan_button.click(
lambda progress=gr.Progress(): scan_shodan(progress),
outputs=scan_output
)
# Refresh dataset when the app starts
def refresh_data():
nonlocal dataset
try:
dataset = load_or_create_dataset()
except Exception as e:
logger.error(f"Failed to refresh dataset: {str(e)}")
# Continue with existing dataset
results, details = search_models(dataset)
return results
app.load(
fn=refresh_data,
outputs=model_results
)
return app
# Main entry point
if __name__ == "__main__":
try:
ui = create_ui()
ui.launch()
except Exception as e:
logger.critical(f"Failed to start application: {str(e)}") |