Spaces:
Sleeping
Sleeping
Commit
·
ae0f92a
1
Parent(s):
d0e6839
train
Browse files
app.py
CHANGED
@@ -1,25 +1,94 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
Args:
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
"""
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
demo = gr.Interface(
|
18 |
-
fn=
|
19 |
-
inputs=["text", "text"],
|
20 |
-
outputs="
|
21 |
-
title="
|
22 |
-
description="
|
23 |
)
|
24 |
|
25 |
demo.launch(mcp_server=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
from nyct_gtfs import NYCTFeed
|
3 |
+
from datetime import datetime
|
4 |
+
from typing import Any
|
5 |
|
6 |
|
7 |
+
# Helper function to convert datetime objects to string
|
8 |
+
def convert_datetime_to_string(datetime_obj: Any) -> str | None:
|
9 |
+
if datetime_obj is None:
|
10 |
+
return None
|
11 |
+
return datetime_obj.strftime("%H:%M:%S")
|
12 |
+
|
13 |
+
|
14 |
+
# Helper function to process train data from NYCTFeed
|
15 |
+
def get_train_info_list(trains_data: Any) -> list[dict[str, Any]]:
|
16 |
+
train_info_list_data = []
|
17 |
+
for train in trains_data:
|
18 |
+
train_info = {
|
19 |
+
"name": str(train),
|
20 |
+
"line": train.route_id,
|
21 |
+
"direction": train.direction,
|
22 |
+
"stop_time_updates": [],
|
23 |
+
}
|
24 |
+
train_info["stop_time_updates"] = [
|
25 |
+
{"stop_name": x.stop_name, "arrival": convert_datetime_to_string(x.arrival)}
|
26 |
+
for x in train.stop_time_updates
|
27 |
+
]
|
28 |
+
train_info_list_data.append(train_info)
|
29 |
+
return train_info_list_data
|
30 |
+
|
31 |
+
|
32 |
+
def get_next_mta_train(
|
33 |
+
target_station: str, target_direction: str, feed_id: str = "1"
|
34 |
+
) -> str:
|
35 |
+
"""Get the next train arrival information for a given station and direction.
|
36 |
|
37 |
Args:
|
38 |
+
target_station: The name of the target station (e.g., "Times Sq-42 St", "14 St-Union Sq").
|
39 |
+
target_direction: The direction of the train ("N" for Northbound / Uptown, "S" for Southbound / Downtown).
|
40 |
+
feed_id: The GTFS feed ID for the subway lines (e.g., "1" for 1,2,3,4,5,6,S lines).
|
41 |
+
Common Feed IDs:
|
42 |
+
"1": 1, 2, 3, 4, 5, 6, 7, S (42 St Shuttle)
|
43 |
+
"A": A, C, E, S (Rockaway Shuttle)
|
44 |
+
"N": N, Q, R, W
|
45 |
+
"B": B, D, F, M, S (Franklin Ave)
|
46 |
+
"L": L
|
47 |
+
"G": G
|
48 |
+
"J": J, Z
|
49 |
+
"7": 7
|
50 |
+
"SIR": Staten Island Railway
|
51 |
|
52 |
+
This function returns a string with the next train arrival information for the given station and direction.
|
53 |
+
You can use this tool to get the next train arrival information for a given station and direction.
|
54 |
"""
|
55 |
+
try:
|
56 |
+
feed = NYCTFeed(feed_id)
|
57 |
+
trains_data = feed.trips
|
58 |
+
except Exception as e:
|
59 |
+
return f"Failed to load MTA feed data for feed ID {feed_id}: {e}"
|
60 |
+
|
61 |
+
if not trains_data:
|
62 |
+
return f"No train data found for feed ID {feed_id}."
|
63 |
+
|
64 |
+
train_info_processed = get_train_info_list(trains_data)
|
65 |
+
current_time = datetime.now().strftime("%H:%M:%S")
|
66 |
+
train_info_string = f"Current time: {current_time}\n"
|
67 |
+
for train in train_info_processed:
|
68 |
+
for stop in train["stop_time_updates"]:
|
69 |
+
if (
|
70 |
+
stop["stop_name"] == target_station
|
71 |
+
and train["direction"] == target_direction
|
72 |
+
):
|
73 |
+
# train_name = train["name"] # Original notebook had this, but it's often complex like "14:50 S 1 to South Ferry"
|
74 |
+
train_line = train["line"]
|
75 |
+
# train_direction = train["direction"] # Already have target_direction
|
76 |
+
train_arrival = stop["arrival"]
|
77 |
+
if train_arrival:
|
78 |
+
train_info_string += f"The next {target_direction} bound {train_line} train arriving at {target_station} will arrive at {train_arrival}.\n"
|
79 |
+
else:
|
80 |
+
train_info_string += f"The next {target_direction} bound {train_line} train is scheduled at {target_station}, but arrival time is not currently available.\n"
|
81 |
+
if train_info_string == "":
|
82 |
+
return f"No {target_direction} bound trains found for {target_station} on feed {feed_id} at this time."
|
83 |
+
return train_info_string
|
84 |
|
85 |
|
86 |
demo = gr.Interface(
|
87 |
+
fn=get_next_mta_train,
|
88 |
+
inputs=["text", "text", "text"],
|
89 |
+
outputs="text",
|
90 |
+
title="MTA Subway Tracker",
|
91 |
+
description="Get the next train arrival information for a given station and direction",
|
92 |
)
|
93 |
|
94 |
demo.launch(mcp_server=True)
|