mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-26 21:02:31 +03:00
f952d7a269
* feat(v2): loaders added * feature: Add scroll animations * feature: upload ui * feature: upload multiple files * fix: Same file name and size remove * feat(crawler): added * feat(parsers): v2 added more * feat(v2): audio now working * feat(v2): all loaders * feat(v2): explorer * chore: add links * feat(api): added status in return message * refactor(website): remove old code * feat(upload): return type for messages * feature: redirect to upload if ENV=local * fix(chat): fixed some issues * feature: respect response type * loading state * feature: Loading stat * feat(v2): added explore and chat pages * feature: modal settings * style: Chat UI * feature: scroll to bottom when chatting * feature: smooth scroll in chat * feature(anim): Slide chat in * feature: markdown chat * feat(explorer): list * feat(doc): added document item * feat(explore): added modal * Add clarification on Project API keys and web interface for migration scripts to Readme (#58) * fix(demo): changed link * add support to uploading zip file (#62) * Catch UnicodeEncodeError exception (#64) * feature: fixed chatbar * fix(loaders): missing argument * fix: layout * fix: One whole chatbox * fix: Scroll into view * fix(build): vercel issues * chore(streamlit): moved to own file * refactor(api): moved to backend folder * feat(docker): added docker compose * Fix a bug where langchain memories were not being cleaned (#71) * Update README.md (#70) * chore(streamlit): moved to own file * refactor(api): moved to backend folder * docs(readme): updated for new version * docs(readme): added old readme * docs(readme): update copy dot env file * docs(readme): cleanup --------- Co-authored-by: iMADi-ARCH <nandanaditya985@gmail.com> Co-authored-by: Matt LeBel <github@lebel.io> Co-authored-by: Evan Carlson <45178375+EvanCarlson@users.noreply.github.com> Co-authored-by: Mustafa Hasan Khan <65130881+mustafahasankhan@users.noreply.github.com> Co-authored-by: zhulixi <48713110+zlxxlz1026@users.noreply.github.com> Co-authored-by: Stanisław Tuszyński <stanislaw@tuszynski.me>
65 lines
2.8 KiB
Python
65 lines
2.8 KiB
Python
import os
|
|
import tempfile
|
|
from io import BytesIO
|
|
import time
|
|
import openai
|
|
import streamlit as st
|
|
from langchain.document_loaders import TextLoader
|
|
from langchain.embeddings.openai import OpenAIEmbeddings
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
from utils import compute_sha1_from_content
|
|
from langchain.schema import Document
|
|
from stats import add_usage
|
|
|
|
|
|
|
|
# Create a function to transcribe audio using Whisper
|
|
def _transcribe_audio(api_key, audio_file, stats_db):
|
|
openai.api_key = api_key
|
|
transcript = ""
|
|
|
|
with BytesIO(audio_file.read()) as audio_bytes:
|
|
# Get the extension of the uploaded file
|
|
file_extension = os.path.splitext(audio_file.name)[-1]
|
|
|
|
# Create a temporary file with the uploaded audio data and the correct extension
|
|
with tempfile.NamedTemporaryFile(delete=True, suffix=file_extension) as temp_audio_file:
|
|
temp_audio_file.write(audio_bytes.read())
|
|
temp_audio_file.seek(0) # Move the file pointer to the beginning of the file
|
|
|
|
# Transcribe the temporary audio file
|
|
if st.secrets.self_hosted == "false":
|
|
add_usage(stats_db, "embedding", "audio", metadata={"file_name": audio_file.name,"file_type": file_extension})
|
|
|
|
transcript = openai.Audio.translate("whisper-1", temp_audio_file)
|
|
|
|
return transcript
|
|
|
|
def process_audio(vector_store, file_name, stats_db):
|
|
if st.secrets.self_hosted == "false":
|
|
if file_name.size > 10000000:
|
|
st.error("File size is too large. Please upload a file smaller than 1MB.")
|
|
return
|
|
file_sha = ""
|
|
dateshort = time.strftime("%Y%m%d-%H%M%S")
|
|
file_meta_name = f"audiotranscript_{dateshort}.txt"
|
|
openai_api_key = st.secrets["openai_api_key"]
|
|
transcript = _transcribe_audio(openai_api_key, file_name, stats_db)
|
|
file_sha = compute_sha1_from_content(transcript.text.encode("utf-8"))
|
|
## file size computed from transcript
|
|
file_size = len(transcript.text.encode("utf-8"))
|
|
|
|
|
|
## Load chunk size and overlap from sidebar
|
|
chunk_size = st.session_state['chunk_size']
|
|
chunk_overlap = st.session_state['chunk_overlap']
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
|
texts = text_splitter.split_text(transcript.text)
|
|
|
|
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]
|
|
|
|
if st.secrets.self_hosted == "false":
|
|
add_usage(stats_db, "embedding", "audio", metadata={"file_name": file_meta_name,"file_type": ".txt", "chunk_size": chunk_size, "chunk_overlap": chunk_overlap})
|
|
vector_store.add_documents(docs_with_metadata)
|
|
return vector_store |