Skip to content

Commit

Permalink
chore: set transcribe_model_selection
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamSilveiraF committed Oct 25, 2023
1 parent 80d3b90 commit 360badd
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ __pycache__/
# C extensions
*.so

#audios
uploaded_audio/

# Distribution / packaging
.Python
build/
Expand Down Expand Up @@ -144,3 +147,6 @@ venv.bak/
.Trashes
ehthumbs.db
Thumbs.db

#gcp
gcp.json
Empty file added api/middlewares/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions api/middlewares/cors_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from fastapi.middleware.cors import CORSMiddleware

def setup_cors(app):
origins = [
"http://localhost:3000",
]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
16 changes: 11 additions & 5 deletions api/routes/audio.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import APIRouter, UploadFile, File, HTTPException
from fastapi.responses import JSONResponse
import shutil
from pathlib import Path
from api.services.transcribe_audio import transcribe_model_selection

import os

router = APIRouter()
Expand All @@ -15,12 +15,18 @@ def read_root():
@router.post("/upload/")
async def upload_audio(file: UploadFile = File(...)):
try:
data = await file.read()

Path(AUDIO_PATH).mkdir(parents=True, exist_ok=True)
path_to_audio = os.path.join(AUDIO_PATH, file.filename)

with open(path_to_audio, 'wb') as f:
f.write(data)

audio_text = transcribe_model_selection(path_to_audio)

with open(os.path(AUDIO_PATH, file.filename), "wb") as buffer:
shutil.copyfileobj(file.file, buffer)

return {"filename": file.filename, "message": "File uploaded successfully!"}
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail="File upload failed.")

26 changes: 15 additions & 11 deletions api/services/transcribe_audio.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from google.cloud import speech
from google.cloud.speech import enums, types

def transcribe_audio(file_path):
def transcribe_model_selection(speech_file: str) -> speech.RecognizeResponse:

client = speech.SpeechClient()

with open(file_path, "rb") as audio_file:
with open(speech_file, "rb") as audio_file:
content = audio_file.read()

audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncondig.LINEAR16,
# TODO: Adjuste sample rate
audio = speech.RecognitionAudio(content=content)

config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED,
sample_rate_hertz=16000,
language_code="en-US"
language_code="en-US",
)

response = client.recognize(config=config, audio=audio)

for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print("-" * 20)
print(f"First alternative of result {i}")
print(f"Transcript: {alternative.transcript}")

if response.results:
return response.results[0].alternatives[0].transcript
return ""
return response
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from fastapi import FastAPI
from api.routes import audio
from api.middlewares import cors_middleware

app = FastAPI()

cors_middleware.setup_cors(app)

app.include_router(audio.router)
15 changes: 15 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
annotated-types==0.6.0
anyio==3.7.1
cachetools==5.3.2
certifi==2023.7.22
charset-normalizer==3.3.1
click==8.1.7
contourpy==1.1.1
cycler==0.12.0
Expand All @@ -9,6 +11,12 @@ email-validator==2.1.0.post1
exceptiongroup==1.1.3
fastapi==0.104.0
fonttools==4.43.0
google-api-core==2.12.0
google-auth==2.23.3
google-cloud-speech==2.21.1
googleapis-common-protos==1.61.0
grpcio==1.59.0
grpcio-status==1.59.0
h11==0.14.0
httpcore==0.18.0
httptools==0.6.1
Expand All @@ -24,6 +32,10 @@ numpy==1.26.0
orjson==3.9.9
packaging==23.2
Pillow==10.0.1
proto-plus==1.22.3
protobuf==4.24.4
pyasn1==0.5.0
pyasn1-modules==0.3.0
pydantic==2.4.2
pydantic-extra-types==2.1.0
pydantic-settings==2.0.3
Expand All @@ -33,12 +45,15 @@ python-dateutil==2.8.2
python-dotenv==1.0.0
python-multipart==0.0.6
PyYAML==6.0.1
requests==2.31.0
rsa==4.9
six==1.16.0
sniffio==1.3.0
starlette==0.27.0
sympy==1.12
typing_extensions==4.8.0
ujson==5.8.0
urllib3==2.0.7
uvicorn==0.23.2
uvloop==0.19.0
watchfiles==0.21.0
Expand Down

0 comments on commit 360badd

Please sign in to comment.