Spaces:
Runtime error
Runtime error
initialize_some
Browse files
tools.py
CHANGED
|
@@ -13,7 +13,9 @@ from langchain_core.tools import tool
|
|
| 13 |
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
| 14 |
import os
|
| 15 |
from typing import Optional, Tuple
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
def extract_image_text(img_path: str) -> str:
|
|
@@ -69,24 +71,87 @@ def extract_image_text(img_path: str) -> str:
|
|
| 69 |
error_msg = f"Error extracting text: {str(e)}"
|
| 70 |
print(error_msg)
|
| 71 |
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
"""
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
""
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
def
|
| 83 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
return a - b
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
def calculate_cargo_travel_time(
|
| 92 |
origin_coords: Tuple[float, float],
|
|
@@ -138,4 +203,33 @@ def calculate_cargo_travel_time(
|
|
| 138 |
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
| 139 |
|
| 140 |
# Format the results
|
|
|
|
| 141 |
return round(flight_time, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
| 14 |
import os
|
| 15 |
from typing import Optional, Tuple
|
| 16 |
+
from huggingface_hub import list_models
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
import json
|
| 19 |
|
| 20 |
|
| 21 |
def extract_image_text(img_path: str) -> str:
|
|
|
|
| 71 |
error_msg = f"Error extracting text: {str(e)}"
|
| 72 |
print(error_msg)
|
| 73 |
return ""
|
| 74 |
+
|
| 75 |
+
extract_image_text = Tool(
|
| 76 |
+
name="extract_image_text",
|
| 77 |
+
func=extract_image_text,
|
| 78 |
+
description="Extracts text that might be visible on an image by reading the image. Works with path/url to image."
|
| 79 |
+
)
|
| 80 |
|
| 81 |
|
| 82 |
+
def multiply(a: int, b: int) -> int:
|
| 83 |
+
"""Multiply two numbers.
|
| 84 |
+
Args:
|
| 85 |
+
a: first int
|
| 86 |
+
b: second int
|
| 87 |
+
"""
|
| 88 |
+
return a * b
|
| 89 |
|
| 90 |
+
multiply = Tool(
|
| 91 |
+
name="multiply",
|
| 92 |
+
func=multiply,
|
| 93 |
+
description="Multiplies two integer numbers to get the product."
|
| 94 |
+
)
|
| 95 |
|
| 96 |
+
def add(a: int, b: int) -> int:
|
| 97 |
+
"""Add two numbers.
|
| 98 |
+
|
| 99 |
+
Args:
|
| 100 |
+
a: first int
|
| 101 |
+
b: second int
|
| 102 |
+
"""
|
| 103 |
+
return a + b
|
| 104 |
+
|
| 105 |
+
add = Tool(
|
| 106 |
+
name="add",
|
| 107 |
+
func=add,
|
| 108 |
+
description="Adds two integer numbers to get the result."
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
def subtract(a: int, b: int) -> int:
|
| 112 |
+
"""Subtract two numbers.
|
| 113 |
+
|
| 114 |
+
Args:
|
| 115 |
+
a: first int
|
| 116 |
+
b: second int
|
| 117 |
+
"""
|
| 118 |
return a - b
|
| 119 |
+
subtract = Tool(
|
| 120 |
+
name="subtract",
|
| 121 |
+
func=subtract,
|
| 122 |
+
description="Subtracts two integer numbers to get the result."
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
def divide(a: int, b: int) -> int:
|
| 126 |
+
"""Divide two numbers.
|
| 127 |
+
|
| 128 |
+
Args:
|
| 129 |
+
a: first int
|
| 130 |
+
b: second int
|
| 131 |
+
"""
|
| 132 |
+
if b == 0:
|
| 133 |
+
raise ValueError("Cannot divide by zero.")
|
| 134 |
+
return a / b
|
| 135 |
+
divide = Tool(
|
| 136 |
+
name="divide",
|
| 137 |
+
func=divide,
|
| 138 |
+
description="Divides two integer numbers to get the result."
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
def modulus(a: int, b: int) -> int:
|
| 142 |
+
"""Get the modulus of two numbers.
|
| 143 |
+
|
| 144 |
+
Args:
|
| 145 |
+
a: first int
|
| 146 |
+
b: second int
|
| 147 |
+
"""
|
| 148 |
+
return a % b
|
| 149 |
+
|
| 150 |
+
modulus = Tool(
|
| 151 |
+
name="modulus",
|
| 152 |
+
func=modulus,
|
| 153 |
+
description="Calculates the modulus of two integer numbers, a mod b, to get the result."
|
| 154 |
+
)
|
| 155 |
|
| 156 |
def calculate_cargo_travel_time(
|
| 157 |
origin_coords: Tuple[float, float],
|
|
|
|
| 203 |
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
| 204 |
|
| 205 |
# Format the results
|
| 206 |
+
|
| 207 |
return round(flight_time, 2)
|
| 208 |
+
|
| 209 |
+
calculate_cargo_travel_time = Tool(
|
| 210 |
+
name="calculate_cargo_travel_time",
|
| 211 |
+
func=calculate_cargo_travel_time,
|
| 212 |
+
description="Calculates the travel time between two points on the earth based on an object with optionally set speed, usually a cargo plane."
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def get_hub_stats(author: str) -> str:
|
| 217 |
+
"""Fetches the most downloaded model from a specific author on the Hugging Face Hub."""
|
| 218 |
+
try:
|
| 219 |
+
# List models from the specified author, sorted by downloads
|
| 220 |
+
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
| 221 |
+
|
| 222 |
+
if models:
|
| 223 |
+
model = models[0]
|
| 224 |
+
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
| 225 |
+
else:
|
| 226 |
+
return f"No models found for author {author}."
|
| 227 |
+
except Exception as e:
|
| 228 |
+
return f"Error fetching models for {author}: {str(e)}"
|
| 229 |
+
|
| 230 |
+
# Initialize the tool
|
| 231 |
+
hub_stats_tool = Tool(
|
| 232 |
+
name="get_hub_stats",
|
| 233 |
+
func=get_hub_stats,
|
| 234 |
+
description="Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 235 |
+
)
|