Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit 0014378

Browse files
committed
Update to use /tasks endpoint without task id to avoid cached response (?)
1 parent a7e9a32 commit 0014378

2 files changed

Lines changed: 49 additions & 32 deletions

File tree

lyrics_transcriber/transcribers/audioshake.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AudioShakeConfig:
1616
api_token: Optional[str] = None
1717
base_url: str = "https://api.audioshake.ai"
1818
output_prefix: Optional[str] = None
19-
timeout_minutes: int = 10 # Added timeout configuration
19+
timeout_minutes: int = 20 # Added timeout configuration
2020

2121

2222
class AudioShakeAPI:
@@ -73,12 +73,13 @@ def wait_for_task_result(self, task_id: str) -> Dict[str, Any]:
7373
"""Poll for task completion and return results."""
7474
self.logger.info(f"Getting task result for task {task_id}")
7575

76-
url = f"{self.config.base_url}/tasks/{task_id}"
76+
# Use the list endpoint which has fresh data, not the individual task endpoint which caches
77+
url = f"{self.config.base_url}/tasks"
7778
start_time = time.time()
7879
last_status_log = start_time
7980
timeout_seconds = self.config.timeout_minutes * 60
8081

81-
# Add initial retry logic for 404 errors (task ID not yet available)
82+
# Add initial retry logic for when task is not found yet
8283
initial_retry_count = 0
8384
max_initial_retries = 5
8485
initial_retry_delay = 2 # seconds
@@ -99,7 +100,24 @@ def wait_for_task_result(self, task_id: str) -> Dict[str, Any]:
99100
try:
100101
response = requests.get(url, headers=self._get_headers())
101102
response.raise_for_status()
102-
task_data = response.json()
103+
tasks_list = response.json()
104+
105+
# Find our specific task in the list
106+
task_data = None
107+
for task in tasks_list:
108+
if task.get("id") == task_id:
109+
task_data = task
110+
break
111+
112+
if not task_data:
113+
# Task not found in list yet
114+
if initial_retry_count < max_initial_retries:
115+
initial_retry_count += 1
116+
self.logger.info(f"Task not found in list yet (attempt {initial_retry_count}/{max_initial_retries}), retrying in {initial_retry_delay} seconds...")
117+
time.sleep(initial_retry_delay)
118+
continue
119+
else:
120+
raise TranscriptionError(f"Task {task_id} not found in task list after {max_initial_retries} retries")
103121

104122
# Log the full response for debugging
105123
self.logger.debug(f"Task status response: {task_data}")
@@ -130,15 +148,7 @@ def wait_for_task_result(self, task_id: str) -> Dict[str, Any]:
130148
initial_retry_count = 0
131149

132150
except requests.exceptions.HTTPError as e:
133-
if e.response.status_code == 404 and initial_retry_count < max_initial_retries:
134-
# Task ID not yet available, retry with delay
135-
initial_retry_count += 1
136-
self.logger.info(f"Task ID not yet available (attempt {initial_retry_count}/{max_initial_retries}), retrying in {initial_retry_delay} seconds...")
137-
time.sleep(initial_retry_delay)
138-
continue
139-
else:
140-
# Re-raise the error if it's not a 404 or we've exceeded retries
141-
raise
151+
raise
142152

143153
time.sleep(30) # Wait before next poll
144154

tests/unit/transcribers/test_audioshake.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ def test_create_task(self, mock_post, api):
8989
@patch("requests.get")
9090
def test_wait_for_task_result_success(self, mock_get, api):
9191
mock_response = Mock()
92-
mock_response.json.return_value = {
93-
"id": "task123",
94-
"targets": [{"model": "alignment", "status": "completed"}],
95-
"data": "test"
96-
}
92+
# Return a list of tasks (as the /tasks endpoint does)
93+
mock_response.json.return_value = [
94+
{
95+
"id": "task123",
96+
"targets": [{"model": "alignment", "status": "completed"}],
97+
"data": "test"
98+
}
99+
]
97100
mock_get.return_value = mock_response
98101

99102
result = api.wait_for_task_result("task123")
@@ -105,10 +108,12 @@ def test_wait_for_task_result_success(self, mock_get, api):
105108
@patch("requests.get")
106109
def test_wait_for_task_result_failure(self, mock_get, api):
107110
mock_response = Mock()
108-
mock_response.json.return_value = {
109-
"id": "task123",
110-
"targets": [{"model": "alignment", "status": "failed", "error": "test error"}]
111-
}
111+
mock_response.json.return_value = [
112+
{
113+
"id": "task123",
114+
"targets": [{"model": "alignment", "status": "failed", "error": "test error"}]
115+
}
116+
]
112117
mock_get.return_value = mock_response
113118

114119
with pytest.raises(Exception, match="Target alignment failed: test error"):
@@ -119,9 +124,9 @@ def test_wait_for_task_result_failure(self, mock_get, api):
119124
def test_wait_for_task_result_polling(self, mock_sleep, mock_get, api):
120125
"""Test polling behavior with in-progress status before completion"""
121126
mock_responses = [
122-
Mock(json=lambda: {"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}),
123-
Mock(json=lambda: {"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}),
124-
Mock(json=lambda: {"id": "task123", "targets": [{"model": "alignment", "status": "completed"}], "data": "test"}),
127+
Mock(json=lambda: [{"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}]),
128+
Mock(json=lambda: [{"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}]),
129+
Mock(json=lambda: [{"id": "task123", "targets": [{"model": "alignment", "status": "completed"}], "data": "test"}]),
125130
]
126131
mock_get.side_effect = mock_responses
127132

@@ -148,10 +153,12 @@ def test_wait_for_task_result_with_retries(self, mock_get, api):
148153
def test_wait_for_task_result_timeout(self, mock_time, mock_get, api):
149154
"""Test that task polling times out after configured duration"""
150155
mock_time.side_effect = [0, api.config.timeout_minutes * 60 + 1] # Simulate timeout
151-
mock_get.return_value = Mock(json=lambda: {
152-
"id": "task123",
153-
"targets": [{"model": "alignment", "status": "processing"}]
154-
})
156+
mock_get.return_value = Mock(json=lambda: [
157+
{
158+
"id": "task123",
159+
"targets": [{"model": "alignment", "status": "processing"}]
160+
}
161+
])
155162

156163
with pytest.raises(TranscriptionError, match=f"Transcription timed out after {api.config.timeout_minutes} minutes"):
157164
api.wait_for_task_result("task123")
@@ -163,9 +170,9 @@ def test_wait_for_task_result_logs_status(self, mock_sleep, mock_time, mock_get,
163170
"""Test that task polling logs status periodically"""
164171
mock_time.side_effect = [0, 30, 61, 90] # Simulate time passing
165172
mock_get.side_effect = [
166-
Mock(json=lambda: {"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}),
167-
Mock(json=lambda: {"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}),
168-
Mock(json=lambda: {"id": "task123", "targets": [{"model": "alignment", "status": "completed"}], "data": "test"}),
173+
Mock(json=lambda: [{"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}]),
174+
Mock(json=lambda: [{"id": "task123", "targets": [{"model": "alignment", "status": "processing"}]}]),
175+
Mock(json=lambda: [{"id": "task123", "targets": [{"model": "alignment", "status": "completed"}], "data": "test"}]),
169176
]
170177

171178
result = api.wait_for_task_result("task123")

0 commit comments

Comments
 (0)