123456789101112131415161718192021222324252627282930313233343536373839 |
- import tempfile
- from typing import List, Any
- from fastapi import Request, APIRouter, UploadFile, File
- from fastapi.responses import FileResponse, PlainTextResponse
- from fastapi.exceptions import HTTPException
- from fastapi.encoders import jsonable_encoder
- from urllib.parse import urlparse, urljoin
- from pathlib import Path
- from icecream import ic
- from google.oauth2 import service_account
- from google.cloud import speech
- client_file = Path(__file__).parent.parent/'keys/pure-lodge-426406-e4-af94156a748a.json'
- credentials = service_account.Credentials.from_service_account_file(client_file)
- client = speech.SpeechClient(credentials=credentials)
- router = APIRouter()
- @router.post('/gcp')
- def gcp(language_code: str=None, file: UploadFile = File()):
- extension = file.filename.split(".")[-1]
- if extension not in ("mp3", "wav", "webm"):
- return HTTPException(status_code=400, detail="Audio must be mp3, wav, webm or webm format!")
- content = file.file.read()
- audio = speech.RecognitionAudio(content=content)
- if extension=='mp3': encoding=speech.RecognitionConfig.AudioEncoding.MP3
- elif extension=='wav': encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16
- elif extension=='webm': encoding=speech.RecognitionConfig.AudioEncoding.WEBM_OPUS
- else: return HTTPException(status_code=400, detail="no such encoding.")
- config = speech.RecognitionConfig(
- encoding=encoding,
- sample_rate_hertz=48000,
- language_code=language_code
- )
- response = client.recognize(config=config, audio=audio)
- results = [results.alternatives[0].transcript for results in response.results]
- ic(results)
-
- return results
|