diff --git a/backend/open_webui/routers/files.py b/backend/open_webui/routers/files.py index bccf74b24..95b7f6461 100644 --- a/backend/open_webui/routers/files.py +++ b/backend/open_webui/routers/files.py @@ -16,6 +16,7 @@ from open_webui.models.files import ( Files, ) from open_webui.routers.retrieval import ProcessFileForm, process_file +from open_webui.routers.audio import transcribe from open_webui.storage.provider import Storage from open_webui.utils.auth import get_admin_user, get_verified_user from pydantic import BaseModel @@ -67,7 +68,22 @@ def upload_file( ) try: - process_file(request, ProcessFileForm(file_id=id), user=user) + if file.content_type in [ + "audio/mpeg", + "audio/wav", + "audio/ogg", + "audio/x-m4a", + ]: + file_path = Storage.get_file(file_path) + result = transcribe(request, file_path) + process_file( + request, + ProcessFileForm(file_id=id, content=result.get("text", "")), + user=user, + ) + else: + process_file(request, ProcessFileForm(file_id=id), user=user) + file_item = Files.get_file_by_id(id=id) except Exception as e: log.exception(e) diff --git a/backend/open_webui/routers/retrieval.py b/backend/open_webui/routers/retrieval.py index 9279f9fa3..446754db4 100644 --- a/backend/open_webui/routers/retrieval.py +++ b/backend/open_webui/routers/retrieval.py @@ -913,7 +913,12 @@ def process_file( # Update the content in the file # Usage: /files/{file_id}/data/content/update - VECTOR_DB_CLIENT.delete_collection(collection_name=f"file-{file.id}") + try: + # /files/{file_id}/data/content/update + VECTOR_DB_CLIENT.delete_collection(collection_name=f"file-{file.id}") + except: + # Audio file upload pipeline + pass docs = [ Document( diff --git a/src/lib/components/channel/MessageInput.svelte b/src/lib/components/channel/MessageInput.svelte index 496993a0c..9ee433e30 100644 --- a/src/lib/components/channel/MessageInput.svelte +++ b/src/lib/components/channel/MessageInput.svelte @@ -157,22 +157,6 @@ } files = [...files, fileItem]; - // Check if the file is an audio file and transcribe/convert it to text file - if (['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/x-m4a'].includes(file['type'])) { - const res = await transcribeAudio(localStorage.token, file).catch((error) => { - toast.error(`${error}`); - return null; - }); - - if (res) { - console.log(res); - const blob = new Blob([res.text], { type: 'text/plain' }); - file = blobToFile(blob, `${file.name}.txt`); - - fileItem.name = file.name; - fileItem.size = file.size; - } - } try { // During the file upload, file content is automatically extracted. diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index df07cb493..f065c7997 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -174,22 +174,6 @@ } files = [...files, fileItem]; - // Check if the file is an audio file and transcribe/convert it to text file - if (['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/x-m4a'].includes(file['type'])) { - const res = await transcribeAudio(localStorage.token, file).catch((error) => { - toast.error(`${error}`); - return null; - }); - - if (res) { - console.log(res); - const blob = new Blob([res.text], { type: 'text/plain' }); - file = blobToFile(blob, `${file.name}.txt`); - - fileItem.name = file.name; - fileItem.size = file.size; - } - } try { // During the file upload, file content is automatically extracted. diff --git a/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte b/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte index e897492e1..173d04a61 100644 --- a/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte +++ b/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte @@ -133,20 +133,6 @@ knowledge.files = [...(knowledge.files ?? []), fileItem]; - // Check if the file is an audio file and transcribe/convert it to text file - if (['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/x-m4a'].includes(file['type'])) { - const res = await transcribeAudio(localStorage.token, file).catch((error) => { - toast.error(`${error}`); - return null; - }); - - if (res) { - console.log(res); - const blob = new Blob([res.text], { type: 'text/plain' }); - file = blobToFile(blob, `${file.name}.txt`); - } - } - try { const uploadedFile = await uploadFile(localStorage.token, file).catch((e) => { toast.error(`${e}`);