forked from alexiusstrauss/AudioTopic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit. Add backend and frontend files
- Loading branch information
1 parent
a506c39
commit 234bd5c
Showing
20 changed files
with
9,466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
flask = "*" | ||
fastapi = "*" | ||
ipython = "*" | ||
python-dotenv = "*" | ||
black = "*" | ||
uvicorn = "*" | ||
langchain = "*" | ||
gtts = "*" | ||
transformers = "*" | ||
tensorflow = "*" | ||
openai = "*" | ||
tiktoken = "*" | ||
chromadb = "*" | ||
python-multipart = "*" | ||
speechrecognition = "*" | ||
pydub = "*" | ||
|
||
[dev-packages] | ||
|
||
[requires] | ||
python_version = "3.10" |
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from fastapi import FastAPI, File, UploadFile | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
from .exceptions import FileFormatException | ||
from .models import UploadResponse | ||
from .services import DeepDive | ||
|
||
app = FastAPI() | ||
|
||
# Configuração do middleware CORS | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
|
||
@app.get("/healthcheck") | ||
async def healthcheck(): | ||
return {"status": "UP"} | ||
|
||
|
||
@app.post("/process-audio/", response_model=UploadResponse) | ||
async def create_upload_file( | ||
audio_file: UploadFile = File(..., description="Envie um arquivo .mp3 ou .wav") | ||
): | ||
service = DeepDive() | ||
result = service.upload_audio(audio_file) | ||
|
||
if not result: | ||
raise FileFormatException() | ||
|
||
result = service.speech_to_text(result) | ||
|
||
return UploadResponse(**result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from fastapi import HTTPException | ||
|
||
|
||
class FileFormatException(HTTPException): | ||
""" | ||
Exceção para indicar formato de arquivo inválido em uploads. | ||
Usada quando um arquivo enviado não está no formato '.mp3' ou '.wav'. | ||
Args: | ||
detail (str): Mensagem personalizada para a exceção. | ||
""" | ||
|
||
def __init__(self, detail: str = "Formatos aceitos: '.mp3' ou '.wav'."): | ||
super().__init__(status_code=400, detail=detail) | ||
|
||
|
||
class RecognizeException(HTTPException): | ||
""" | ||
Exceção quando a lib que faz a coversão de audio pra texto apresentar erro | ||
Args: | ||
detail (str): Mensagem personalizada para a exceção. | ||
""" | ||
|
||
def __init__(self, detail: str = "Erro ao ler o conteudo do aúdio"): | ||
super().__init__(status_code=500, detail=detail) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class UploadResponse(BaseModel): | ||
file_id: str | ||
original_context: str = None | ||
summary_context: str = None | ||
mp3_summary_url: str = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from uuid import uuid4 | ||
|
||
import speech_recognition as sr | ||
from fastapi import UploadFile | ||
from pydub import AudioSegment as Asegment | ||
|
||
from .exceptions import RecognizeException | ||
|
||
|
||
class DeepDive: | ||
""" | ||
Service responsavel por upload, conversao e validação do audio | ||
""" | ||
|
||
def upload_audio(self, file: UploadFile): | ||
""" | ||
Function upload audio mp3 or wav | ||
""" | ||
file_extension = file.filename.split(".")[-1] | ||
if file_extension not in ["mp3", "wav"]: | ||
return False | ||
|
||
file_id = str(uuid4()) | ||
file_location = f"audio_files/{file_id}.{file_extension}" | ||
|
||
with open(file_location, "wb+") as file_object: | ||
file_object.write(file.file.read()) | ||
|
||
return {"file_id": file_id, "file_location": file_location} | ||
|
||
def speech_to_text(self, upload_audio: dict) -> dict: | ||
""" | ||
Function convert audio to text | ||
""" | ||
recognizer = sr.Recognizer() | ||
audio_file_path = upload_audio["file_location"] | ||
audio_file_path = self.__convert_mp3_to_wav(audio_file_path) | ||
|
||
with sr.AudioFile(audio_file_path) as source: | ||
audio_data = recognizer.record(source) | ||
try: | ||
text = recognizer.recognize_google( | ||
audio_data=audio_data, language="pt-BR" | ||
) | ||
upload_audio["original_context"] = text | ||
except (sr.UnknownValueError, sr.RequestError) as exc: | ||
raise RecognizeException() from exc | ||
|
||
return upload_audio | ||
|
||
def __convert_mp3_to_wav(self, mp3_file_path): | ||
wav_file_path = mp3_file_path.replace(".mp3", ".wav") | ||
try: | ||
Asegment.from_mp3(mp3_file_path).export(wav_file_path, format="wav") | ||
except Exception as exc: | ||
raise RecognizeException() from exc | ||
return wav_file_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Nuxt dev/build outputs | ||
.output | ||
.data | ||
.nuxt | ||
.nitro | ||
.cache | ||
dist | ||
|
||
# Node dependencies | ||
node_modules | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
||
# Misc | ||
.DS_Store | ||
.fleet | ||
.idea | ||
|
||
# Local env files | ||
.env | ||
.env.* | ||
!.env.example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
shamefully-hoist=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Nuxt 3 Minimal Starter | ||
|
||
Look at the [Nuxt 3 documentation](https://nuxt.com/docs/getting-started/introduction) to learn more. | ||
|
||
## Setup | ||
|
||
Make sure to install the dependencies: | ||
|
||
```bash | ||
# npm | ||
npm install | ||
|
||
# pnpm | ||
pnpm install | ||
|
||
# yarn | ||
yarn install | ||
|
||
# bun | ||
bun install | ||
``` | ||
|
||
## Development Server | ||
|
||
Start the development server on `http://localhost:3000`: | ||
|
||
```bash | ||
# npm | ||
npm run dev | ||
|
||
# pnpm | ||
pnpm run dev | ||
|
||
# yarn | ||
yarn dev | ||
|
||
# bun | ||
bun run dev | ||
``` | ||
|
||
## Production | ||
|
||
Build the application for production: | ||
|
||
```bash | ||
# npm | ||
npm run build | ||
|
||
# pnpm | ||
pnpm run build | ||
|
||
# yarn | ||
yarn build | ||
|
||
# bun | ||
bun run build | ||
``` | ||
|
||
Locally preview production build: | ||
|
||
```bash | ||
# npm | ||
npm run preview | ||
|
||
# pnpm | ||
pnpm run preview | ||
|
||
# yarn | ||
yarn preview | ||
|
||
# bun | ||
bun run preview | ||
``` | ||
|
||
Check out the [deployment documentation](https://nuxt.com/docs/getting-started/deployment) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<template> | ||
<div> | ||
<audio-recorder @audio-uploaded="handleAudioUploaded" /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import AudioRecorder from '~/components/AudioRecorder.vue'; | ||
export default { | ||
components: { | ||
AudioRecorder | ||
}, | ||
methods: { | ||
handleAudioUploaded(responseData) { | ||
this.$router.push({ | ||
name: 'summary-context', | ||
params: { | ||
fileData: responseData | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
</script> |
Oops, something went wrong.