2023-05-21 02:20:55 +03:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import time
|
2023-06-01 17:01:27 +03:00
|
|
|
|
2023-05-21 02:20:55 +03:00
|
|
|
import openai
|
2023-06-01 17:01:27 +03:00
|
|
|
from langchain.schema import Document
|
2023-05-21 02:20:55 +03:00
|
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
2023-08-21 13:25:16 +03:00
|
|
|
|
|
|
|
from models import File, get_documents_vector_store
|
2023-06-04 00:12:42 +03:00
|
|
|
from utils.file import compute_sha1_from_content
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-05-22 09:39:55 +03:00
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
async def process_audio(
|
|
|
|
file: File,
|
|
|
|
enable_summarization: bool,
|
|
|
|
user,
|
|
|
|
user_openai_api_key,
|
|
|
|
):
|
2023-06-28 10:47:59 +03:00
|
|
|
temp_filename = None
|
2023-05-21 02:20:55 +03:00
|
|
|
file_sha = ""
|
|
|
|
dateshort = time.strftime("%Y%m%d-%H%M%S")
|
|
|
|
file_meta_name = f"audiotranscript_{dateshort}.txt"
|
2023-08-03 21:24:42 +03:00
|
|
|
documents_vector_store = get_documents_vector_store()
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
# use this for whisper
|
2023-07-10 15:27:49 +03:00
|
|
|
os.environ.get("OPENAI_API_KEY")
|
2023-06-11 00:59:16 +03:00
|
|
|
if user_openai_api_key:
|
2023-07-10 15:27:49 +03:00
|
|
|
pass
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-06-28 10:47:59 +03:00
|
|
|
try:
|
2023-06-28 20:39:27 +03:00
|
|
|
upload_file = file.file
|
2023-07-10 15:27:49 +03:00
|
|
|
with tempfile.NamedTemporaryFile(
|
|
|
|
delete=False,
|
|
|
|
suffix=upload_file.filename, # pyright: ignore reportPrivateUsage=none
|
|
|
|
) as tmp_file:
|
|
|
|
await upload_file.seek(0) # pyright: ignore reportPrivateUsage=none
|
|
|
|
content = (
|
|
|
|
await upload_file.read() # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
2023-06-28 10:47:59 +03:00
|
|
|
tmp_file.write(content)
|
|
|
|
tmp_file.flush()
|
|
|
|
tmp_file.close()
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-06-28 10:47:59 +03:00
|
|
|
temp_filename = tmp_file.name
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-06-28 10:47:59 +03:00
|
|
|
with open(tmp_file.name, "rb") as audio_file:
|
|
|
|
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
file_sha = compute_sha1_from_content(
|
|
|
|
transcript.text.encode("utf-8") # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
|
|
|
file_size = len(
|
|
|
|
transcript.text.encode("utf-8") # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-06-28 10:47:59 +03:00
|
|
|
chunk_size = 500
|
|
|
|
chunk_overlap = 0
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-06-28 10:47:59 +03:00
|
|
|
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
|
2023-07-10 15:27:49 +03:00
|
|
|
chunk_size=chunk_size, chunk_overlap=chunk_overlap
|
|
|
|
)
|
|
|
|
texts = text_splitter.split_text(
|
|
|
|
transcript.text.encode("utf-8") # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
docs_with_metadata = [
|
|
|
|
Document(
|
|
|
|
page_content=text,
|
|
|
|
metadata={
|
|
|
|
"file_sha1": file_sha,
|
|
|
|
"file_size": file_size,
|
|
|
|
"file_name": file_meta_name,
|
|
|
|
"chunk_size": chunk_size,
|
|
|
|
"chunk_overlap": chunk_overlap,
|
|
|
|
"date": dateshort,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
for text in texts
|
|
|
|
]
|
2023-06-28 10:47:59 +03:00
|
|
|
|
2023-08-03 21:24:42 +03:00
|
|
|
documents_vector_store.add_documents(docs_with_metadata)
|
2023-06-28 10:47:59 +03:00
|
|
|
|
|
|
|
finally:
|
|
|
|
if temp_filename and os.path.exists(temp_filename):
|
2023-07-10 15:27:49 +03:00
|
|
|
os.remove(temp_filename)
|