From c40cc5d2a2327447b49d4595a10f9429fe65e18d Mon Sep 17 00:00:00 2001 From: bladeclara42 <71927457+bladeclara42@users.noreply.github.com> Date: Thu, 16 Oct 2025 08:51:26 +0700 Subject: [PATCH] fix(lyric_translator): add readme and add async request --- app/services/lyric_romanji_translator.py | 17 ++++++++++--- readme.md | 31 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/services/lyric_romanji_translator.py b/app/services/lyric_romanji_translator.py index 16ec157..b92ccc8 100644 --- a/app/services/lyric_romanji_translator.py +++ b/app/services/lyric_romanji_translator.py @@ -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"} \ No newline at end of file diff --git a/readme.md b/readme.md index e69de29..efa9633 100644 --- a/readme.md +++ b/readme.md @@ -0,0 +1,31 @@ + +# Project Title + +A brief description of what this project does and who it's for + + +## Run Locally + +Clone the project + +```bash + git clone https://link-to-project +``` + +Go to the project directory + +```bash + cd my-project +``` + +Install dependencies + +```bash + pip install -r requirements.txt +``` + +Start the server + +```bash + uvicorn app.main:app --reload +```