Spaces:
Running
Running
| # Prompt builder utilities for the Open Subtitle Translator | |
| STYLE_PREFIX = """You are a professional subtitle translator. | |
| HARD CONSTRAINTS: | |
| - Output SRT only between <<<SRT>>> and <<<END>>>. | |
| - Do NOT change block numbers or timecodes. | |
| - Do NOT add/remove lines within a block; preserve exact line breaks. | |
| - Keep any tags/speaker labels (e.g., <i>, ♪) exactly as-is. | |
| - No commentary or explanations outside the SRT. | |
| """ | |
| RTL_LANGS = {"he", "ar", "fa", "ur"} | |
| def build_prompt(source_lang: str, | |
| target_lang: str, | |
| batch_srt: str, | |
| glossary_text: str | None, | |
| extra_instructions: str | None, | |
| prev_source: str | None, | |
| prev_target: str | None) -> str: | |
| """ | |
| Compose a cache-friendly prompt. Keep the prefix byte-identical across calls | |
| to leverage provider-side prompt caching where available. | |
| """ | |
| prefix = STYLE_PREFIX + f""" | |
| TASK: | |
| - Translate from {source_lang} to {target_lang}. | |
| - Input format is SRT blocks. | |
| STYLE & GLOSSARY (project-provided; must follow if applicable): | |
| """ | |
| if glossary_text and glossary_text.strip(): | |
| prefix += glossary_text.strip() + "\n" | |
| if extra_instructions and extra_instructions.strip(): | |
| prefix += "\nEXTRA INSTRUCTIONS (apply carefully):\n" + extra_instructions.strip() + "\n" | |
| context = "\nCONTEXT (do not modify):\n" | |
| if prev_source: | |
| context += "[Previous batch — Source]\n" + prev_source.strip() + "\n" | |
| if prev_target: | |
| context += "[Previous batch — Target]\n" + prev_target.strip() + "\n" | |
| task = ( | |
| "\nCURRENT BATCH TO TRANSLATE:\n" | |
| + batch_srt | |
| + "\nReturn only:\n<<<SRT>>>\n[Translated SRT blocks]\n<<<END>>>" | |
| ) | |
| return prefix + context + task | |