fix(lyric_translator): add readme and add async request
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import os
|
||||
import re
|
||||
import asyncio
|
||||
from app.core.deepseek_client import chat_with_openai
|
||||
from app.models.lyric_romanji_translator import FileResult
|
||||
|
||||
semaphore = asyncio.Semaphore(5)
|
||||
timestamp_pattern = re.compile(r"^\[\d{2}:\d{2}\.\d{2}\]")
|
||||
|
||||
def needs_romaji(lines, idx):
|
||||
@@ -39,21 +41,28 @@ async def process_lrc_file(filepath: str) -> FileResult:
|
||||
|
||||
return FileResult(file=filepath, processed=added_lines > 0, added_lines=added_lines)
|
||||
|
||||
async def safe_process(filepath):
|
||||
async with semaphore:
|
||||
print(f"Processing: {filepath}")
|
||||
return await process_lrc_file(filepath)
|
||||
|
||||
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}"}
|
||||
|
||||
tasks = []
|
||||
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}")
|
||||
tasks.append(asyncio.create_task(safe_process(filepath)))
|
||||
|
||||
result = await process_lrc_file(filepath)
|
||||
if not tasks:
|
||||
return {"results": [], "status": "no .lrc files found"}
|
||||
|
||||
# ✅ result is already a FileResult object
|
||||
results.append(result)
|
||||
# Run them all concurrently
|
||||
results = await asyncio.gather(*tasks)
|
||||
|
||||
return {"results": results, "status": "completed"}
|
||||
Reference in New Issue
Block a user