Download src/apis/speech_processing_api.py from techconspartners/learnable-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/spaces/techconspartners/learnable-ai/resolve/main/src/apis/speech_processing_api.py
- Command line
-
hf download hf://spaces/techconspartners/learnable-ai/src/apis/speech_processing_api.py
-
curl -L -o speech_processing_api.py https://huggingface.co/spaces/techconspartners/learnable-ai/resolve/main/src/apis/speech_processing_api.py
1.32 kB
| """ | |
| Created By: ishwor subedi | |
| Date: 2024-08-13 | |
| """ | |
| from fastapi import HTTPException | |
| from fastapi.routing import APIRouter | |
| from src.models.models import SpeechToTextRequest, TextToSpeechRequest | |
| from src.pipeline.speech_processing_pipeline import SpeechTranscriptionPipeline | |
| from fastapi.responses import JSONResponse | |
| speech_transcription_router = APIRouter() | |
| speech_transcription_pipeline = SpeechTranscriptionPipeline() | |
| async def speech_to_text(request: SpeechToTextRequest): | |
| try: | |
| transcript = speech_transcription_pipeline.speech_to_text(request.audio, request.lang) | |
| json = {'transcript': transcript, 'status_code': 200} | |
| return json | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str) | |
| async def text_to_speech(request: TextToSpeechRequest): | |
| try: | |
| audio_bytes = speech_transcription_pipeline.text_to_speech(request.text, request.lang, request.tld) | |
| if not audio_bytes: | |
| raise ValueError("Audio generation failed.") | |
| return JSONResponse(content={"audio": audio_bytes, "status_code": 200}, status_code=200) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail="Internal Server Error") | |