feat: add lyric music to romanji tool
This commit is contained in:
59
app/services/lyric_romanji_translator.py
Normal file
59
app/services/lyric_romanji_translator.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import re
|
||||
from app.core.deepseek_client import chat_with_openai
|
||||
from app.models.lyric_romanji_translator import FileResult
|
||||
|
||||
timestamp_pattern = re.compile(r"^\[\d{2}:\d{2}\.\d{2}\]")
|
||||
|
||||
def needs_romaji(lines, idx):
|
||||
if idx + 1 < len(lines) and not timestamp_pattern.match(lines[idx + 1]):
|
||||
return False
|
||||
return True
|
||||
|
||||
async def get_romaji(text: str) -> str:
|
||||
messages = [
|
||||
{"role": "system", "content": "Convert Japanese text into romaji only. Output romaji without explanation."},
|
||||
{"role": "user", "content": text}
|
||||
]
|
||||
return await chat_with_openai(messages)
|
||||
|
||||
async def process_lrc_file(filepath: str) -> FileResult:
|
||||
added_lines = 0
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
new_lines = []
|
||||
for idx, line in enumerate(lines):
|
||||
new_lines.append(line)
|
||||
|
||||
if timestamp_pattern.match(line) and needs_romaji(lines, idx):
|
||||
japanese = line.strip().split("]", 1)[-1].strip()
|
||||
if japanese:
|
||||
romaji = await get_romaji(japanese)
|
||||
new_lines.append(f"{romaji}\n")
|
||||
added_lines += 1
|
||||
|
||||
if added_lines > 0:
|
||||
with open(filepath, "w", encoding="utf-8") as f:
|
||||
f.writelines(new_lines)
|
||||
|
||||
return FileResult(file=filepath, processed=added_lines > 0, added_lines=added_lines)
|
||||
|
||||
async def translate_lyric_romanji(folder_path: str):
|
||||
results = []
|
||||
|
||||
if not os.path.exists(folder_path):
|
||||
return {"results": [], "status": f"error: folder not found {folder_path}"}
|
||||
|
||||
for root, _, files in os.walk(folder_path):
|
||||
for file in files:
|
||||
if file.endswith(".lrc"):
|
||||
filepath = os.path.join(root, file)
|
||||
print(f"Processing: {filepath}")
|
||||
|
||||
result = await process_lrc_file(filepath)
|
||||
|
||||
# ✅ result is already a FileResult object
|
||||
results.append(result)
|
||||
|
||||
return {"results": results, "status": "completed"}
|
||||
Reference in New Issue
Block a user