Skip to content

Commit

Permalink
Initial commit. Add backend and frontend files
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiusstrauss committed Nov 26, 2023
1 parent a506c39 commit 234bd5c
Show file tree
Hide file tree
Showing 20 changed files with 9,466 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore → backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

audio_files/
27 changes: 27 additions & 0 deletions backend/Pipfile
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"
2,843 changes: 2,843 additions & 0 deletions backend/Pipfile.lock

Large diffs are not rendered by default.

Empty file added backend/src/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions backend/src/api.py
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)
26 changes: 26 additions & 0 deletions backend/src/exceptions.py
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)
8 changes: 8 additions & 0 deletions backend/src/models.py
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
57 changes: 57 additions & 0 deletions backend/src/services.py
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
24 changes: 24 additions & 0 deletions frontend/src/.gitignore
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
1 change: 1 addition & 0 deletions frontend/src/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shamefully-hoist=true
75 changes: 75 additions & 0 deletions frontend/src/README.md
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.
25 changes: 25 additions & 0 deletions frontend/src/app.vue
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>
Loading

0 comments on commit 234bd5c

Please sign in to comment.