mirror of
https://github.com/open-webui/open-webui
synced 2025-05-19 20:57:54 +00:00
refac: audio file handling
This commit is contained in:
parent
46ac6f2b29
commit
78a8ef8e66
@ -16,6 +16,7 @@ from open_webui.models.files import (
|
|||||||
Files,
|
Files,
|
||||||
)
|
)
|
||||||
from open_webui.routers.retrieval import ProcessFileForm, process_file
|
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.storage.provider import Storage
|
||||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@ -67,7 +68,22 @@ def upload_file(
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
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)
|
file_item = Files.get_file_by_id(id=id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(e)
|
log.exception(e)
|
||||||
|
@ -913,7 +913,12 @@ def process_file(
|
|||||||
# Update the content in the file
|
# Update the content in the file
|
||||||
# Usage: /files/{file_id}/data/content/update
|
# 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 = [
|
docs = [
|
||||||
Document(
|
Document(
|
||||||
|
@ -157,22 +157,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
files = [...files, fileItem];
|
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 {
|
try {
|
||||||
// During the file upload, file content is automatically extracted.
|
// During the file upload, file content is automatically extracted.
|
||||||
|
@ -174,22 +174,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
files = [...files, fileItem];
|
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 {
|
try {
|
||||||
// During the file upload, file content is automatically extracted.
|
// During the file upload, file content is automatically extracted.
|
||||||
|
@ -133,20 +133,6 @@
|
|||||||
|
|
||||||
knowledge.files = [...(knowledge.files ?? []), fileItem];
|
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 {
|
try {
|
||||||
const uploadedFile = await uploadFile(localStorage.token, file).catch((e) => {
|
const uploadedFile = await uploadFile(localStorage.token, file).catch((e) => {
|
||||||
toast.error(`${e}`);
|
toast.error(`${e}`);
|
||||||
|
Loading…
Reference in New Issue
Block a user