quivr/backend/modules/assistant/ito/audio_transcript.py
Stan Girard 1ffeb8f25d
feat: Add assistant module and remove ingestion module (#2420)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2024-04-10 04:28:22 -07:00

48 lines
1.4 KiB
Python

import os
from tempfile import NamedTemporaryFile
from logger import get_logger
from modules.assistant.ito.ito import ITO
from openai import OpenAI
logger = get_logger(__name__)
class AudioTranscriptAssistant(ITO):
def __init__(
self,
**kwargs,
):
super().__init__(
**kwargs,
)
async def process_assistant(self):
client = OpenAI()
logger.info(f"Processing audio file {self.uploadFile.filename}")
# Extract the original filename and create a temporary file with the same name
filename = os.path.basename(self.uploadFile.filename)
temp_file = NamedTemporaryFile(delete=False, suffix=filename)
# Write the uploaded file's data to the temporary file
data = await self.uploadFile.read()
temp_file.write(data)
temp_file.close()
# Open the temporary file and pass it to the OpenAI API
with open(temp_file.name, "rb") as file:
transcription = client.audio.transcriptions.create(
model="whisper-1", file=file, response_format="text"
)
logger.info(f"Transcription: {transcription}")
# Delete the temporary file
os.remove(temp_file.name)
return await self.create_and_upload_processed_file(
transcription, self.uploadFile.filename, "Audio Transcript"
)