Repository navigation
Expand file tree
/
Copy pathlive_digest.py
More file actions
141 lines (118 loc) · 3.87 KB
/
Copy pathlive_digest.py
File metadata and controls
141 lines (118 loc) · 3.87 KB
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
"""
Trio API - Stream Summarization
This example shows how to generate a narrative summary of what's
happening on a live stream over a time window.
Usage:
export TRIO_API_KEY="your_api_key"
python live_digest.py
The API captures frames at intervals and generates a cohesive
summary of the activity.
Note:
This example uses SSE (Server-Sent Events) streaming to receive
summaries in real-time. For webhook-based delivery, provide a
webhook_url parameter.
"""
import os
import requests
TRIO_API = "https://trio.machinefi.com/api"
API_KEY = os.environ.get("TRIO_API_KEY", "YOUR_API_KEY")
# Example: Summarize activity on a traffic cam
STREAM_URL = "https://www.youtube.com/watch?v=jfKfPfyJRdk"
def start_live_digest_sse(
stream_url: str,
window_minutes: int = 5,
capture_interval_seconds: int = 30
):
"""
Start a summarization job and stream results via SSE.
Args:
stream_url: YouTube Live URL to summarize
window_minutes: Duration of summary window (1-60)
capture_interval_seconds: How often to capture frames (10-300)
Yields:
SSE events with progress and summary data
"""
response = requests.post(
f"{TRIO_API}/live-digest",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"Accept": "text/event-stream"
},
json={
"stream_url": stream_url,
"window_minutes": window_minutes,
"capture_interval_seconds": capture_interval_seconds
},
stream=True
)
response.raise_for_status()
# Parse SSE events
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
yield line[6:] # Remove 'data: ' prefix
def start_live_digest_webhook(
stream_url: str,
webhook_url: str,
window_minutes: int = 5,
capture_interval_seconds: int = 30
) -> dict:
"""
Start a summarization job with webhook delivery.
Args:
stream_url: YouTube Live URL to summarize
webhook_url: URL to receive the summary
window_minutes: Duration of summary window (1-60)
capture_interval_seconds: How often to capture frames (10-300)
Returns:
API response with job_id
"""
response = requests.post(
f"{TRIO_API}/live-digest",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"stream_url": stream_url,
"webhook_url": webhook_url,
"window_minutes": window_minutes,
"capture_interval_seconds": capture_interval_seconds
}
)
response.raise_for_status()
return response.json()
def get_job_status(job_id: str) -> dict:
"""Check the status of a job."""
response = requests.get(
f"{TRIO_API}/jobs/{job_id}",
headers={"Authorization": f"Bearer {API_KEY}"}
)
response.raise_for_status()
return response.json()
if __name__ == "__main__":
import json
print(f"Starting stream digest (SSE mode)...")
print(f"Stream: {STREAM_URL}")
print(f"Window: 5 minutes")
print(f"Capture interval: 30 seconds")
print()
print("Waiting for summary events...")
print("-" * 50)
for event_data in start_live_digest_sse(
stream_url=STREAM_URL,
window_minutes=5,
capture_interval_seconds=30
):
try:
event = json.loads(event_data)
if "summary" in event:
print(f"\nSummary: {event['summary']}")
elif "frame_count" in event:
print(f"Progress: {event['frame_count']} frames captured")
elif "job_id" in event:
print(f"Job started: {event['job_id']}")
except json.JSONDecodeError:
print(f"Event: {event_data}")