|
@@ -11,13 +11,13 @@ from dashscope.audio.tts import ResultCallback, SpeechSynthesizer, SpeechSynthes
|
|
|
|
|
|
import requests
|
|
|
|
|
|
-from ..schemas.llm import TextToSpeechResponse
|
|
|
+from ..schemas.speech import TextToSpeechResponse
|
|
|
from ..core.config import get_settings
|
|
|
|
|
|
settings = get_settings()
|
|
|
# Configure DashScope API key from env/.env
|
|
|
dashscope.api_key = settings.dashscope_api_key or ""
|
|
|
-UPLOAD_PATH = settings.upload_path or 'https://api.piaoquantv.com/ad/file/upload'
|
|
|
+UPLOAD_PATH = settings.upload_path
|
|
|
|
|
|
|
|
|
def _safe_filename(name: str) -> str:
|
|
@@ -26,10 +26,10 @@ def _safe_filename(name: str) -> str:
|
|
|
|
|
|
|
|
|
class SpeechProvider:
|
|
|
- def text_to_speech(self, pitch: float, rate: float, filename: str, text: str, *, model: Optional[str] = None, format: Optional[str] = None) -> TextToSpeechResponse:
|
|
|
- # Resolve output path under app/audio and ensure directory exists
|
|
|
- app_dir = Path(__file__).resolve().parents[1] # .../app
|
|
|
- audio_dir = app_dir / "audio"
|
|
|
+ def text_to_speech(self, volume: int, pitch: float, rate: float, filename: str, text: str, *, model: Optional[str] = None, format: Optional[str] = None) -> TextToSpeechResponse:
|
|
|
+ # Resolve output path under project-root/temp and ensure directory exists
|
|
|
+ project_root = Path(__file__).resolve().parents[2] # repo root
|
|
|
+ audio_dir = project_root / "temp"
|
|
|
audio_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
# determine desired output format (default mp3 for smaller size)
|
|
@@ -55,6 +55,7 @@ class SpeechProvider:
|
|
|
|
|
|
SpeechSynthesizer.call(
|
|
|
model=(model or 'sambert-zhifei-v1'),
|
|
|
+ volume=volume,
|
|
|
text=text,
|
|
|
pitch=pitch,
|
|
|
rate=rate,
|
|
@@ -68,6 +69,11 @@ class SpeechProvider:
|
|
|
# After synthesis completes, upload the file to OSS
|
|
|
try:
|
|
|
url = _upload_file(UPLOAD_PATH, out_path)
|
|
|
+ # Upload succeeded; remove local audio file to save space
|
|
|
+ try:
|
|
|
+ Path(out_path).unlink(missing_ok=True)
|
|
|
+ except Exception as del_err:
|
|
|
+ print(f"[warn] Failed to delete local audio {out_path}: {del_err}")
|
|
|
return TextToSpeechResponse(audio_url=url)
|
|
|
except Exception as e:
|
|
|
# If upload fails, fall back to local path to avoid breaking
|