Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
- DockerfileAdded+34
- app.pyAdded+100
12345678910111213141516171819202122232425262728293031323334FROM python:3.10-slim
WORKDIR /app
# Install system dependencies required for building and audio processingRUN apt-get update && apt-get install -y \ git bash wget build-essential libsndfile1 \ && rm -rf /var/lib/apt/lists/*
# Force install PyTorch CPU version to save massive amounts of build time and spaceRUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
# Clone and install the custom NeMo fork required by IndicConformerASRRUN git clone https://github.com/AI4Bharat/NeMo.git \ && cd NeMo \ && git checkout nemo-v2 \ && bash reinstall.sh \ && cd ..
# Install FastAPI, Piper TTS, and patch the known dependency issuesRUN pip install --no-cache-dir "numpy<2.0" huggingface_hub==0.23.2 fastapi uvicorn python-multipart "piper-tts==1.2.0"
# Fetch the Piper Nepali Chitwan model directly during the Docker buildRUN wget -q -O chitwan.onnx "https://huggingface.co/rhasspy/piper-voices/resolve/main/ne/ne_NP/chitwan/medium/ne_NP-chitwan-medium.onnx?download=true"RUN wget -q -O chitwan.onnx.json "https://huggingface.co/rhasspy/piper-voices/resolve/main/ne/ne_NP/chitwan/medium/ne_NP-chitwan-medium.onnx.json?download=true"
# Copy the FastAPI app into the containerCOPY app.py /app/app.py
# Hugging Face Spaces route traffic through port 7860EXPOSE 7860
# Start the asynchronous Uvicorn serverCMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import osimport waveimport asyncioimport torchfrom fastapi import FastAPI, Depends, HTTPException, status, UploadFile, Filefrom fastapi.security import HTTPBasic, HTTPBasicCredentialsfrom fastapi.responses import FileResponseimport nemo.collections.asr as nemo_asrfrom piper.voice import PiperVoice
app = FastAPI(title="ASR & TTS API")security = HTTPBasic()
# Basic Authentication ConfigurationUSERNAME = os.environ.get("API_USERNAME", "admin")PASSWORD = os.environ.get("API_PASSWORD", "secret")
def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)): if not (credentials.username == USERNAME and credentials.password == PASSWORD): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", headers={"WWW-Authenticate": "Basic"}, ) return credentials
# Global references for the modelsasr_model = Nonetts_voice = None
@app.on_event("startup")async def load_models(): global asr_model, tts_voice # 1. Load and Quantize NeMo ASR # Ensure you have uploaded your downloaded NeMo model to the Space with this filename nemo_path = "model.nemo" if os.path.exists(nemo_path): device = torch.device('cpu') model = nemo_asr.models.EncDecCTCModel.restore_from(restore_path=nemo_path, map_location=device) model.freeze() # Apply CPU Dynamic Quantization for memory reduction and speed model = torch.quantization.quantize_dynamic( model, {torch.nn.Linear}, dtype=torch.qint8 ) model.cur_decoder = 'ctc' asr_model = model print("ASR Model loaded and dynamically quantized.") else: print("WARNING: model.nemo not found. Please upload it.")
# 2. Load Piper TTS (Nepali Chitwan) tts_model_path = "chitwan.onnx" if os.path.exists(tts_model_path): tts_voice = PiperVoice.load(tts_model_path) print("TTS Model loaded.")
@app.post("/asr")async def transcribe(file: UploadFile = File(...), _: str = Depends(verify_credentials)): if not asr_model: raise HTTPException(status_code=503, detail="ASR model not loaded") # Save the uploaded audio temporarily audio_path = f"/tmp/{file.filename}" with open(audio_path, "wb") as f: f.write(await file.read()) # Run the CPU-bound ASR transcription in a thread pool loop = asyncio.get_event_loop() transcription = await loop.run_in_executor( None, # Note: If using the AI4Bharat multilingual model, add `language_id='ne'` below lambda: asr_model.transcribe(paths2audio_files=[audio_path], batch_size=1)[0] ) os.remove(audio_path) return {"text": transcription}
@app.post("/tts")async def synthesize(text: str, _: str = Depends(verify_credentials)): if not tts_voice: raise HTTPException(status_code=503, detail="TTS model not loaded") output_path = "/tmp/output.wav" # Run the CPU-bound TTS synthesis in a thread pool loop = asyncio.get_event_loop() def generate_audio(): with wave.open(output_path, "wb") as wav_file: wav_file.setnchannels(1) wav_file.setsampwidth(2) wav_file.setframerate(tts_voice.config.sample_rate) tts_voice.synthesize(text, wav_file)
await loop.run_in_executor(None, generate_audio) return FileResponse(output_path, media_type="audio/wav")DockerfileAdded+34
app.pyAdded+100