|
40 | 40 |
|
41 | 41 | from api.accent.align import align_accent |
42 | 42 | from api.accent.models import AccentResponse, ErrorInfo, WordAccentResult |
43 | | -from api.accent.ojad import get_ojad_result |
| 43 | +from api.accent.ojad import OJADUnavailableError, get_ojad_result |
44 | 44 | from api.accent.postprocess import ( |
45 | 45 | apply_furigana_toggles, |
46 | 46 | convert_furigana_script, |
@@ -145,7 +145,21 @@ async def process_accent_chunk( |
145 | 145 | # a normal contour; fugashi keeps the original surface so the |
146 | 146 | # tokenizer's acronym-merge preserves `Wifi.7` for display. |
147 | 147 | ojad_query_text = strip_acronym_dots_for_ojad(stripped_text) |
148 | | - _ojad_surface, ojad_results = await get_ojad_result(ojad_query_text, client) |
| 148 | + |
| 149 | + # OJAD only enriches the result with pitch accent. If it is down, fall |
| 150 | + # back to furigana-only output (align_accent emits a fallback word with |
| 151 | + # no pitch contour for every token when given an empty list) rather |
| 152 | + # than failing the request — see align_accent's `m == 0` branch. |
| 153 | + warning = None |
| 154 | + try: |
| 155 | + _ojad_surface, ojad_results = await get_ojad_result(ojad_query_text, client) |
| 156 | + except OJADUnavailableError as e: |
| 157 | + logger.warning(f"OJAD unavailable, degrading to furigana-only: {e}") |
| 158 | + ojad_results = [] |
| 159 | + warning = ( |
| 160 | + "OJAD pitch-accent service is unavailable; " |
| 161 | + "returning furigana without pitch accent." |
| 162 | + ) |
149 | 163 |
|
150 | 164 | final_results = await align_accent(furigana_results, ojad_results) |
151 | 165 | final_results = apply_accent_overrides(final_results) |
@@ -180,7 +194,7 @@ async def process_accent_chunk( |
180 | 194 | # script before serialisation. Hiragana is the no-op default. |
181 | 195 | final_results = convert_furigana_script(final_results, script) |
182 | 196 |
|
183 | | - return AccentResponse(status=200, result=final_results) |
| 197 | + return AccentResponse(status=200, result=final_results, warning=warning) |
184 | 198 |
|
185 | 199 | except Exception as e: |
186 | 200 | logger.exception(f"Unexpected error occurred: {text}") |
|
0 commit comments