Spaces:
Runtime error
Runtime error
tools
Browse files
tools.py
CHANGED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 2 |
+
from langchain.tools import Tool
|
| 3 |
+
import random
|
| 4 |
+
from langchain.tools import Tool
|
| 5 |
+
from huggingface_hub import list_models
|
| 6 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 7 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 8 |
+
from langchain_community.document_loaders.csv_loader import CSVLoader
|
| 9 |
+
import math
|
| 10 |
+
from langgraph.prebuilt import tools_condition
|
| 11 |
+
from langgraph.prebuilt import ToolNode
|
| 12 |
+
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_text(img_path: str) -> str:
|
| 20 |
+
"""
|
| 21 |
+
Extract text from an image file using a multimodal model.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
img_path: A local image file path (strings).
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
A single string containing the concatenated text extracted from each image.
|
| 28 |
+
"""
|
| 29 |
+
vision_llm = ChatOpenAI(model="gpt-4o")
|
| 30 |
+
all_text = ""
|
| 31 |
+
try:
|
| 32 |
+
|
| 33 |
+
# Read image and encode as base64
|
| 34 |
+
with open(img_path, "rb") as image_file:
|
| 35 |
+
image_bytes = image_file.read()
|
| 36 |
+
|
| 37 |
+
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 38 |
+
|
| 39 |
+
# Prepare the prompt including the base64 image data
|
| 40 |
+
message = [
|
| 41 |
+
HumanMessage(
|
| 42 |
+
content=[
|
| 43 |
+
{
|
| 44 |
+
"type": "text",
|
| 45 |
+
"text": (
|
| 46 |
+
"Extract all the text from this image. "
|
| 47 |
+
"Return only the extracted text, no explanations."
|
| 48 |
+
),
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"type": "image_url",
|
| 52 |
+
"image_url": {
|
| 53 |
+
"url": f"data:image/png;base64,{image_base64}"
|
| 54 |
+
},
|
| 55 |
+
},
|
| 56 |
+
]
|
| 57 |
+
)
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
# Call the vision-capable model
|
| 61 |
+
response = vision_llm.invoke(message)
|
| 62 |
+
|
| 63 |
+
# Append extracted text
|
| 64 |
+
all_text += response.content + "\n\n"
|
| 65 |
+
|
| 66 |
+
return all_text.strip()
|
| 67 |
+
except Exception as e:
|
| 68 |
+
# You can choose whether to raise or just return an empty string / error message
|
| 69 |
+
error_msg = f"Error extracting text: {str(e)}"
|
| 70 |
+
print(error_msg)
|
| 71 |
+
return ""
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def divide(a: int, b: int) -> float:
|
| 75 |
+
"""Divide a and b."""
|
| 76 |
+
return a / b
|
| 77 |
+
|
| 78 |
+
def add(a: int, b: int) -> float:
|
| 79 |
+
"""Add a and b."""
|
| 80 |
+
return a + b
|
| 81 |
+
|
| 82 |
+
def subtract(a: int, b: int) -> float:
|
| 83 |
+
"""Subtract a and b."""
|
| 84 |
+
return a - b
|
| 85 |
+
|
| 86 |
+
def multiply(a: int, b: int) -> float:
|
| 87 |
+
"""Multiply a and b."""
|
| 88 |
+
return a*b
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def calculate_cargo_travel_time(
|
| 92 |
+
origin_coords: Tuple[float, float],
|
| 93 |
+
destination_coords: Tuple[float, float],
|
| 94 |
+
cruising_speed_kmh: Optional[float] = 750.0, # Average speed for cargo planes
|
| 95 |
+
) -> float:
|
| 96 |
+
"""
|
| 97 |
+
Calculate the travel time for a cargo plane between two points on Earth using great-circle distance.
|
| 98 |
+
|
| 99 |
+
Args:
|
| 100 |
+
origin_coords: Tuple of (latitude, longitude) for the starting point
|
| 101 |
+
destination_coords: Tuple of (latitude, longitude) for the destination
|
| 102 |
+
cruising_speed_kmh: Optional cruising speed in km/h (defaults to 750 km/h for typical cargo planes)
|
| 103 |
+
|
| 104 |
+
Returns:
|
| 105 |
+
float: The estimated travel time in hours
|
| 106 |
+
|
| 107 |
+
Example:
|
| 108 |
+
>>> # Chicago (41.8781° N, 87.6298° W) to Sydney (33.8688° S, 151.2093° E)
|
| 109 |
+
>>> result = calculate_cargo_travel_time((41.8781, -87.6298), (-33.8688, 151.2093))
|
| 110 |
+
"""
|
| 111 |
+
|
| 112 |
+
def to_radians(degrees: float) -> float:
|
| 113 |
+
return degrees * (math.pi / 180)
|
| 114 |
+
|
| 115 |
+
# Extract coordinates
|
| 116 |
+
lat1, lon1 = map(to_radians, origin_coords)
|
| 117 |
+
lat2, lon2 = map(to_radians, destination_coords)
|
| 118 |
+
|
| 119 |
+
# Earth's radius in kilometers
|
| 120 |
+
EARTH_RADIUS_KM = 6371.0
|
| 121 |
+
|
| 122 |
+
# Calculate great-circle distance using the haversine formula
|
| 123 |
+
dlon = lon2 - lon1
|
| 124 |
+
dlat = lat2 - lat1
|
| 125 |
+
|
| 126 |
+
a = (
|
| 127 |
+
math.sin(dlat / 2) ** 2
|
| 128 |
+
+ math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
|
| 129 |
+
)
|
| 130 |
+
c = 2 * math.asin(math.sqrt(a))
|
| 131 |
+
distance = EARTH_RADIUS_KM * c
|
| 132 |
+
|
| 133 |
+
# Add 10% to account for non-direct routes and air traffic controls
|
| 134 |
+
actual_distance = distance * 1.1
|
| 135 |
+
|
| 136 |
+
# Calculate flight time
|
| 137 |
+
# Add 1 hour for takeoff and landing procedures
|
| 138 |
+
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
| 139 |
+
|
| 140 |
+
# Format the results
|
| 141 |
+
return round(flight_time, 2)
|