fix(lyric_translator): add readme and add async request

This commit is contained in:
bladeclara42
2025-10-16 08:51:26 +07:00
parent a2759b8169
commit c40cc5d2a2
2 changed files with 44 additions and 4 deletions

View File

@@ -1,8 +1,10 @@
import os import os
import re import re
import asyncio
from app.core.deepseek_client import chat_with_openai from app.core.deepseek_client import chat_with_openai
from app.models.lyric_romanji_translator import FileResult from app.models.lyric_romanji_translator import FileResult
semaphore = asyncio.Semaphore(5)
timestamp_pattern = re.compile(r"^\[\d{2}:\d{2}\.\d{2}\]") timestamp_pattern = re.compile(r"^\[\d{2}:\d{2}\.\d{2}\]")
def needs_romaji(lines, idx): 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) 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): async def translate_lyric_romanji(folder_path: str):
results = [] results = []
if not os.path.exists(folder_path): if not os.path.exists(folder_path):
return {"results": [], "status": f"error: folder not found {folder_path}"} return {"results": [], "status": f"error: folder not found {folder_path}"}
tasks = []
for root, _, files in os.walk(folder_path): for root, _, files in os.walk(folder_path):
for file in files: for file in files:
if file.endswith(".lrc"): if file.endswith(".lrc"):
filepath = os.path.join(root, file) 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 # Run them all concurrently
results.append(result) results = await asyncio.gather(*tasks)
return {"results": results, "status": "completed"} return {"results": results, "status": "completed"}

View File

@@ -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
```