-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
167 lines (142 loc) · 4.67 KB
/
main.py
File metadata and controls
167 lines (142 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
EchoForge - Main Application
This is the main entry point for the EchoForge application.
It sets up the FastAPI server and registers all routes.
"""
import os
import logging
import argparse
from pathlib import Path
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from app.api.routes import router as api_router
from app.api.voice_browser import router as voice_browser_router
from app.ui.routes import router as ui_router
from app.core.voice_generator import VoiceGenerator
from app.core import config # Import config
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
handlers=[logging.StreamHandler()]
)
logger = logging.getLogger("echoforge")
# Create FastAPI application
app = FastAPI(
title="EchoForge",
description="Character Voice Generation Platform",
version="0.1.0"
)
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, you'd want to restrict this
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount static files
app.mount(
"/static",
StaticFiles(directory=Path(__file__).parent / "static"),
name="static"
)
# Include routers
app.include_router(api_router)
app.include_router(ui_router)
app.include_router(voice_browser_router)
@app.on_event("startup")
async def startup_event():
"""Perform startup initialization."""
logger.info("Starting EchoForge application")
# Create data directories
data_dir = Path(__file__).parent / "data"
voices_dir = data_dir / "voices" / "output"
voices_dir.mkdir(parents=True, exist_ok=True)
logger.info(f"Initialized data directories: {data_dir}")
# Initialize voice generator with Direct CSM
from app.api.voice_generator import voice_generator
try:
# Get device from environment or use auto
device = os.environ.get("ECHOFORGE_DEVICE", "auto")
logger.info(f"Initializing voice generator with device: {device}")
# Initialize the voice generator
voice_generator.initialize(device=device)
# Log whether we're using Direct CSM
if voice_generator.direct_csm is not None:
logger.info("Voice generator initialized with Direct CSM")
elif voice_generator.model is not None:
logger.info("Voice generator initialized with standard CSM model")
else:
logger.warning("Voice generator initialized but no model is loaded")
except Exception as e:
logger.error(f"Failed to initialize voice generator: {e}")
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="EchoForge TTS Server")
parser.add_argument(
"--host",
type=str,
default="0.0.0.0",
help="Host to bind the server to"
)
parser.add_argument(
"--port",
type=int,
default=config.PORT,
help="Port to bind the server to"
)
parser.add_argument(
"--model-path",
type=str,
default=None,
help="Path to the TTS model"
)
parser.add_argument(
"--device",
type=str,
default="auto",
help="Device to use for TTS (auto, cuda, cpu)"
)
parser.add_argument(
"--direct-csm",
action="store_true",
default=True,
help="Use Direct CSM implementation (default: True)"
)
parser.add_argument(
"--no-direct-csm",
action="store_false",
dest="direct_csm",
help="Disable Direct CSM implementation"
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable debug mode"
)
return parser.parse_args()
if __name__ == "__main__":
# Parse command line arguments
args = parse_args()
# Set environment variables for model settings
if args.model_path:
os.environ["ECHOFORGE_MODEL_PATH"] = args.model_path
os.environ["ECHOFORGE_DEVICE"] = args.device
# Set Direct CSM environment variable
os.environ["USE_DIRECT_CSM"] = str(args.direct_csm).lower()
# Configure logging level based on debug mode
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
logger.debug("Debug mode enabled")
# Start the server
logger.info(f"Starting server on {args.host}:{args.port} with Direct CSM {'enabled' if args.direct_csm else 'disabled'}")
uvicorn.run(
"main:app",
host=args.host,
port=args.port,
reload=args.debug,
log_level="debug" if args.debug else "info"
)