Skip to content

Commit

Permalink
Media upload datasource! (#419)
Browse files Browse the repository at this point in the history
* basic changes to allow files box

* basic imports, yay!

* video_scene_timelines to work on video imports!

* add is_compatible_with checks to processors that cannot run on new media top_datasets

* more is_compatible fixes

* necessary function for checking media_types

* enable more processors on media datasets

* consolidate user_input file type

* detect mimetype from filename

best I can do without downloading all the files first.

* handle zip archives; allow log and metadata files

* do not count metadata or log files in num_files

* move machine learning processors so they can be imported elsewhere

* audio_to_text datasource

* When validating zip file uploads, send list of file attributes instead of the first 128K of the zip file

* Check type of files in zip when uploading media

* Skip useless files when uploading media as zip

* check multiple zip types in JS

* js !=== python

* fix media_type for loose file imports; fix extension for audio_to_text preset; fix merge for some processors w/ media_type

---------

Co-authored-by: Stijn Peeters <[email protected]>
  • Loading branch information
dale-wahl and stijn-uva authored Jun 25, 2024
1 parent 4ce689b commit 25f4ed6
Show file tree
Hide file tree
Showing 50 changed files with 599 additions and 54 deletions.
36 changes: 35 additions & 1 deletion LICENSE-3DPARTY
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,38 @@ Incorporates the Graphology graph manipulation library
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.

-------------------------------------------------------------------------------
Incorporates the zip.js library
- at /webtool/static/js/zip.min.js
- from https://github.com/gildas-lormeau/zip.js

BSD 3-Clause License

Copyright (c) 2023, Gildas Lormeau

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 changes: 12 additions & 0 deletions common/lib/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,18 @@ def get_extension(self):

return False

def get_media_type(self):
"""
Gets the media type of the dataset file.
:return str: media type, e.g., "text"
"""
if hasattr(self, "media_type"):
return self.media_type
else:
# Default to text
return self.parameters.get("media_type", "text")

def get_result_url(self):
"""
Gets the 4CAT frontend URL of a dataset file.
Expand Down
17 changes: 17 additions & 0 deletions common/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ def timify_long(number):

return time_str + last_str

def andify(items):
"""
Format a list of items for use in text
Returns a comma-separated list, the last item preceded by "and"
:param items: Iterable list
:return str: Formatted string
"""
if len(items) == 0:
return ""
elif len(items) == 1:
return str(items[1])

result = f" and {items.pop()}"
return ", ".join([str(item) for item in items]) + result


def get_yt_compatible_ids(yt_ids):
"""
Expand Down
6 changes: 6 additions & 0 deletions datasources/audio_to_text/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Use default data source init function
from common.lib.helpers import init_datasource

# Internal identifier for this data source
DATASOURCE = "upload-audio-to-text"
NAME = "Upload Audio to Text"
53 changes: 53 additions & 0 deletions datasources/audio_to_text/audio_to_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Audio Upload to Text
This data source acts similar to a Preset, but because it needs SearchMedia's validate_query and after_create methods
to run, chaining that processor does not work (Presets essentially only run the process and after_process methods
of their processors and skip those two datasource only methods).
"""

from datasources.media_import.import_media import SearchMedia
from processors.machine_learning.whisper_speech_to_text import AudioToText


class AudioUploadToText(SearchMedia):
type = "upload-audio-to-text-search" # job ID
category = "Search" # category
title = "Convert speech to text" # title displayed in UI
description = "Upload your own audio and use OpenAI's Whisper model to create transcripts" # description displayed in UI

@classmethod
def is_compatible_with(cls, module=None, user=None):
#TODO: False here does not appear to actually remove the datasource from the "Create dataset" page so technically
# this method is not necessary; if we can adjust that behavior, it ought to function as intended

# Ensure the Whisper model is available
return AudioToText.is_compatible_with(module=module, user=user)

@classmethod
def get_options(cls, parent_dataset=None, user=None):
# We need both sets of options for this datasource
media_options = SearchMedia.get_options(parent_dataset=parent_dataset, user=user)
whisper_options = AudioToText.get_options(parent_dataset=parent_dataset, user=user)
media_options.update(whisper_options)

#TODO: there are some odd formatting issues if we use those derived options
# The intro help text is not displayed correct (does not wrap)
# Advanced Settings uses []() links which do not work on the "Create dataset" page, so we adjust

media_options["intro"]["help"] = ("Upload audio files here to convert speech to text. "
"4CAT will use OpenAI's Whisper model to create transcripts."
"\n\nFor information on using advanced settings: [Command Line Arguments (CLI)](https://github.com/openai/whisper/blob/248b6cb124225dd263bb9bd32d060b6517e067f8/whisper/transcribe.py#LL374C3-L374C3)")
media_options["advanced"]["help"] = "Advanced Settings"

return media_options

@staticmethod
def validate_query(query, request, user):
# We need SearchMedia's validate_query to upload the media
media_query = SearchMedia.validate_query(query, request, user)

# Here's the real trick: act like a preset and add another processor to the pipeline
media_query["next"] = [{"type": "audio-to-text",
"parameters": query.copy()}]
return media_query
6 changes: 6 additions & 0 deletions datasources/media_import/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Use default data source init function
from common.lib.helpers import init_datasource

# Internal identifier for this data source
DATASOURCE = "media-import"
NAME = "Import/upload Media files"
Loading

0 comments on commit 25f4ed6

Please sign in to comment.