Skip to content

Commit

Permalink
Attempt to server build frontend from python package
Browse files Browse the repository at this point in the history
  • Loading branch information
beveradb committed Jan 28, 2025
1 parent 3663aff commit c936e7a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
47 changes: 18 additions & 29 deletions lyrics_transcriber/review/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import atexit
import urllib.parse
from fastapi.staticfiles import StaticFiles

logger = logging.getLogger(__name__)

Expand All @@ -29,25 +30,21 @@


def start_vite_server():
"""Start the Vite development server."""
global vite_process

# Get the path to the lyrics-analyzer directory relative to this file
"""Get path to the built frontend assets."""
global vite_process # We'll keep this for backwards compatibility
# Get the path to the built frontend assets
current_dir = os.path.dirname(os.path.abspath(__file__))
vite_dir = os.path.abspath(os.path.join(current_dir, "../../lyrics-analyzer"))

logger.info(f"Starting Vite dev server in {vite_dir}")

# Start the Vite dev server
vite_process = subprocess.Popen(["npm", "run", "dev"], cwd=vite_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

# Register cleanup function to kill Vite server on exit
atexit.register(lambda: vite_process.terminate() if vite_process else None)

# Wait a bit for the server to start
time.sleep(2) # Adjust this if needed

return vite_process
frontend_dir = os.path.abspath(os.path.join(current_dir, "../../lyrics-analyzer/dist"))

if not os.path.exists(frontend_dir):
raise FileNotFoundError(f"Frontend assets not found at {frontend_dir}. Ensure the package was built correctly.")

# Mount the static files
app.mount("/", StaticFiles(directory=frontend_dir, html=True), name="frontend")

logger.info(f"Mounted frontend assets from {frontend_dir}")
return None # No process to return since we're serving static files


@app.get("/api/correction-data")
Expand Down Expand Up @@ -109,9 +106,9 @@ def start_review_server(correction_result: CorrectionResult) -> CorrectionResult

logger.info("Starting review server...")

# Start Vite dev server
vite_proc = start_vite_server()
logger.info("Vite dev server started")
# Start Vite dev server (now just mounts static files)
start_vite_server()
logger.info("Frontend assets mounted")

# Start FastAPI server in a separate thread
server_thread = Thread(target=uvicorn.run, args=(app,), kwargs={"host": "127.0.0.1", "port": 8000, "log_level": "info"}, daemon=True)
Expand All @@ -132,13 +129,5 @@ def start_review_server(correction_result: CorrectionResult) -> CorrectionResult
# logger.error("Review timed out after 10 minutes")
# raise TimeoutError("Review did not complete within the expected time frame.")

# Clean up Vite server
if vite_proc:
vite_proc.terminate()
try:
vite_proc.wait(timeout=5)
except subprocess.TimeoutExpired:
vite_proc.kill()

logger.info("Review completed, returning results")
return current_review
10 changes: 8 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[tool.poetry]
name = "lyrics-transcriber"
version = "0.34.1"
version = "0.34.2"
description = "Automatically create synchronised lyrics files in ASS and MidiCo LRC formats with word-level timestamps, using Whisper and lyrics from Genius and Spotify"
authors = ["Andrew Beveridge <[email protected]>"]
license = "MIT"
readme = "README.md"
packages = [{ include = "lyrics_transcriber" }]
packages = [
{ include = "lyrics_transcriber" },
{ include = "lyrics-analyzer", from = "." },
]
homepage = "https://github.com/karaokenerds/python-lyrics-transcriber"
repository = "https://github.com/karaokenerds/python-lyrics-transcriber"
documentation = "https://github.com/karaokenerds/python-lyrics-transcriber/blob/main/README.md"
Expand Down Expand Up @@ -53,3 +56,6 @@ build-backend = "poetry.core.masonry.api"
testpaths = ["tests"]
python_files = ["test_*.py"]
filterwarnings = ["ignore:'audioop' is deprecated:DeprecationWarning"]

[tool.poetry.package-data]
"lyrics-analyzer" = ["dist/**/*"]

0 comments on commit c936e7a

Please sign in to comment.