Spaces:
Sleeping
Sleeping
dummy agent
Browse files- .env.example +2 -0
- .gitignore +2 -1
- agent.py +19 -2
- app.py +11 -1
- requirements.txt +10 -1
- tools/tools.py +63 -0
.env.example
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SPACE_ID=
|
| 2 |
+
HF_TOKEN=
|
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
__pycache__
|
| 2 |
-
__pycache__/*
|
|
|
|
|
|
| 1 |
__pycache__
|
| 2 |
+
__pycache__/*
|
| 3 |
+
.env
|
agent.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# --- Basic Agent Definition ---
|
| 2 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 3 |
|
|
@@ -5,7 +11,18 @@ class BasicAgent:
|
|
| 5 |
def __init__(self):
|
| 6 |
print("BasicAgent initialized.")
|
| 7 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 9 |
-
fixed_answer = "This is a default answer."
|
| 10 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 11 |
-
return fixed_answer
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from smolagents import CodeAgent, HfApiModel, InferenceClientModel
|
| 4 |
+
|
| 5 |
+
import tools.tools as tls
|
| 6 |
+
|
| 7 |
# --- Basic Agent Definition ---
|
| 8 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 9 |
|
|
|
|
| 11 |
def __init__(self):
|
| 12 |
print("BasicAgent initialized.")
|
| 13 |
def __call__(self, question: str) -> str:
|
| 14 |
+
model = HfApiModel(model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud", provider="together", use_auth_token=True)
|
| 15 |
+
|
| 16 |
+
agent = CodeAgent(
|
| 17 |
+
tools=[tls.search_tool, tls.calculate_cargo_travel_time],
|
| 18 |
+
model=InferenceClientModel(),
|
| 19 |
+
additional_authorized_imports=["pandas"],
|
| 20 |
+
max_steps=20,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
fixed_answer = agent.run(question)
|
| 24 |
+
|
| 25 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 26 |
+
# fixed_answer = "This is a default answer."
|
| 27 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 28 |
+
return str(fixed_answer)
|
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
import agent
|
| 8 |
|
|
@@ -160,7 +162,7 @@ with gr.Blocks() as demo:
|
|
| 160 |
# """
|
| 161 |
# )
|
| 162 |
|
| 163 |
-
gr.LoginButton()
|
| 164 |
|
| 165 |
gr.ChatInterface(test_init_agent_for_chat, type="messages")
|
| 166 |
|
|
@@ -177,6 +179,14 @@ with gr.Blocks() as demo:
|
|
| 177 |
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 181 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 182 |
space_host_startup = os.getenv("SPACE_HOST")
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
import agent
|
| 10 |
|
|
|
|
| 162 |
# """
|
| 163 |
# )
|
| 164 |
|
| 165 |
+
# gr.LoginButton()
|
| 166 |
|
| 167 |
gr.ChatInterface(test_init_agent_for_chat, type="messages")
|
| 168 |
|
|
|
|
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
load_dotenv()
|
| 183 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 184 |
+
if hf_token:
|
| 185 |
+
login(hf_token)
|
| 186 |
+
else:
|
| 187 |
+
print("ℹ️ HF_TOKEN environment variable not found (running locally?).")
|
| 188 |
+
|
| 189 |
+
|
| 190 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 191 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 192 |
space_host_startup = os.getenv("SPACE_HOST")
|
requirements.txt
CHANGED
|
@@ -1,2 +1,11 @@
|
|
| 1 |
gradio
|
| 2 |
-
requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
| 3 |
+
smolagents[litellm]
|
| 4 |
+
matplotlib
|
| 5 |
+
geopandas
|
| 6 |
+
shapely
|
| 7 |
+
kaleido
|
| 8 |
+
smolagents
|
| 9 |
+
typing
|
| 10 |
+
duckduckgo-search
|
| 11 |
+
huggingface_hub
|
tools/tools.py
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
from typing import Optional, Tuple
|
| 3 |
+
|
| 4 |
+
from smolagents import tool, DuckDuckGoSearchTool, VisitWebpageTool
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
search_tool = DuckDuckGoSearchTool()
|
| 8 |
+
|
| 9 |
+
@tool
|
| 10 |
+
def calculate_cargo_travel_time(
|
| 11 |
+
origin_coords: Tuple[float, float],
|
| 12 |
+
destination_coords: Tuple[float, float],
|
| 13 |
+
cruising_speed_kmh: Optional[float] = 750.0, # Average speed for cargo planes
|
| 14 |
+
) -> float:
|
| 15 |
+
"""
|
| 16 |
+
Calculate the travel time for a cargo plane between two points on Earth using great-circle distance.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
origin_coords: Tuple of (latitude, longitude) for the starting point
|
| 20 |
+
destination_coords: Tuple of (latitude, longitude) for the destination
|
| 21 |
+
cruising_speed_kmh: Optional cruising speed in km/h (defaults to 750 km/h for typical cargo planes)
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
float: The estimated travel time in hours
|
| 25 |
+
|
| 26 |
+
Example:
|
| 27 |
+
>>> # Chicago (41.8781° N, 87.6298° W) to Sydney (33.8688° S, 151.2093° E)
|
| 28 |
+
>>> result = calculate_cargo_travel_time((41.8781, -87.6298), (-33.8688, 151.2093))
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
def to_radians(degrees: float) -> float:
|
| 32 |
+
return degrees * (math.pi / 180)
|
| 33 |
+
|
| 34 |
+
# Extract coordinates
|
| 35 |
+
lat1, lon1 = map(to_radians, origin_coords)
|
| 36 |
+
lat2, lon2 = map(to_radians, destination_coords)
|
| 37 |
+
|
| 38 |
+
# Earth's radius in kilometers
|
| 39 |
+
EARTH_RADIUS_KM = 6371.0
|
| 40 |
+
|
| 41 |
+
# Calculate great-circle distance using the haversine formula
|
| 42 |
+
dlon = lon2 - lon1
|
| 43 |
+
dlat = lat2 - lat1
|
| 44 |
+
|
| 45 |
+
a = (
|
| 46 |
+
math.sin(dlat / 2) ** 2
|
| 47 |
+
+ math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
|
| 48 |
+
)
|
| 49 |
+
c = 2 * math.asin(math.sqrt(a))
|
| 50 |
+
distance = EARTH_RADIUS_KM * c
|
| 51 |
+
|
| 52 |
+
# Add 10% to account for non-direct routes and air traffic controls
|
| 53 |
+
actual_distance = distance * 1.1
|
| 54 |
+
|
| 55 |
+
# Calculate flight time
|
| 56 |
+
# Add 1 hour for takeoff and landing procedures
|
| 57 |
+
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
| 58 |
+
|
| 59 |
+
# Format the results
|
| 60 |
+
return round(flight_time, 2)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
print(calculate_cargo_travel_time((41.8781, -87.6298), (-33.8688, 151.2093)))
|