Skip to content

Commit 78a8ef8

Browse files
committed
refac: audio file handling
1 parent 46ac6f2 commit 78a8ef8

File tree

5 files changed

+23
-48
lines changed

5 files changed

+23
-48
lines changed

backend/open_webui/routers/files.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Files,
1717
)
1818
from open_webui.routers.retrieval import ProcessFileForm, process_file
19+
from open_webui.routers.audio import transcribe
1920
from open_webui.storage.provider import Storage
2021
from open_webui.utils.auth import get_admin_user, get_verified_user
2122
from pydantic import BaseModel
@@ -67,7 +68,22 @@ def upload_file(
6768
)
6869

6970
try:
70-
process_file(request, ProcessFileForm(file_id=id), user=user)
71+
if file.content_type in [
72+
"audio/mpeg",
73+
"audio/wav",
74+
"audio/ogg",
75+
"audio/x-m4a",
76+
]:
77+
file_path = Storage.get_file(file_path)
78+
result = transcribe(request, file_path)
79+
process_file(
80+
request,
81+
ProcessFileForm(file_id=id, content=result.get("text", "")),
82+
user=user,
83+
)
84+
else:
85+
process_file(request, ProcessFileForm(file_id=id), user=user)
86+
7187
file_item = Files.get_file_by_id(id=id)
7288
except Exception as e:
7389
log.exception(e)

backend/open_webui/routers/retrieval.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,12 @@ def process_file(
913913
# Update the content in the file
914914
# Usage: /files/{file_id}/data/content/update
915915

916-
VECTOR_DB_CLIENT.delete_collection(collection_name=f"file-{file.id}")
916+
try:
917+
# /files/{file_id}/data/content/update
918+
VECTOR_DB_CLIENT.delete_collection(collection_name=f"file-{file.id}")
919+
except:
920+
# Audio file upload pipeline
921+
pass
917922

918923
docs = [
919924
Document(

src/lib/components/channel/MessageInput.svelte

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,6 @@
157157
}
158158
159159
files = [...files, fileItem];
160-
// Check if the file is an audio file and transcribe/convert it to text file
161-
if (['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/x-m4a'].includes(file['type'])) {
162-
const res = await transcribeAudio(localStorage.token, file).catch((error) => {
163-
toast.error(`${error}`);
164-
return null;
165-
});
166-
167-
if (res) {
168-
console.log(res);
169-
const blob = new Blob([res.text], { type: 'text/plain' });
170-
file = blobToFile(blob, `${file.name}.txt`);
171-
172-
fileItem.name = file.name;
173-
fileItem.size = file.size;
174-
}
175-
}
176160
177161
try {
178162
// During the file upload, file content is automatically extracted.

src/lib/components/chat/MessageInput.svelte

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,6 @@
174174
}
175175
176176
files = [...files, fileItem];
177-
// Check if the file is an audio file and transcribe/convert it to text file
178-
if (['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/x-m4a'].includes(file['type'])) {
179-
const res = await transcribeAudio(localStorage.token, file).catch((error) => {
180-
toast.error(`${error}`);
181-
return null;
182-
});
183-
184-
if (res) {
185-
console.log(res);
186-
const blob = new Blob([res.text], { type: 'text/plain' });
187-
file = blobToFile(blob, `${file.name}.txt`);
188-
189-
fileItem.name = file.name;
190-
fileItem.size = file.size;
191-
}
192-
}
193177
194178
try {
195179
// During the file upload, file content is automatically extracted.

src/lib/components/workspace/Knowledge/KnowledgeBase.svelte

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,6 @@
133133
134134
knowledge.files = [...(knowledge.files ?? []), fileItem];
135135
136-
// Check if the file is an audio file and transcribe/convert it to text file
137-
if (['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/x-m4a'].includes(file['type'])) {
138-
const res = await transcribeAudio(localStorage.token, file).catch((error) => {
139-
toast.error(`${error}`);
140-
return null;
141-
});
142-
143-
if (res) {
144-
console.log(res);
145-
const blob = new Blob([res.text], { type: 'text/plain' });
146-
file = blobToFile(blob, `${file.name}.txt`);
147-
}
148-
}
149-
150136
try {
151137
const uploadedFile = await uploadFile(localStorage.token, file).catch((e) => {
152138
toast.error(`${e}`);

0 commit comments

Comments
 (0)