Hi,
I’m trying to test some script execution in Knime, but as I’m not too familiar with python and Python Script node I don’t know how to get the json output from the console into a table for Knime.
I’m testing Deepgram’s API (you can get an API key online). For path I’m using the path to a local file (wav).
import knime.scripting.io as knio
import pandas as pd
from deepgram import Deepgram
import asyncio, json
# Your Deepgram API Key
DEEPGRAM_API_KEY = 'API KEY' #should be replaced
# Location of the file you want to transcribe. Should include filename and extension.
# Example of a local file: ../../Audio/life-moves-pretty-fast.wav
# Example of a remote file: https://static.deepgram.com/examples/interview_speech-analytics.wav
FILE = knio.flow_variables['path']
# Mimetype for the file you want to transcribe
# Include this line only if transcribing a local file
# Example: audio/wav
MIMETYPE = 'audio/wav'
async def main():
# Initialize the Deepgram SDK
deepgram = Deepgram(DEEPGRAM_API_KEY)
# Check whether requested file is local or remote, and prepare source
if FILE.startswith('http'):
# file is remote
# Set the source
source = {
'url': FILE
}
else:
# file is local
# Open the audio file
audio = open(FILE, 'rb')
# Set the source
source = {
'buffer': audio,
'mimetype': MIMETYPE
}
# Send the audio to Deepgram and get the response
response = await asyncio.create_task(
deepgram.transcription.prerecorded(
source,
{
'punctuate': True
}
)
)
# Write the response to the console
print(json.dumps(response, indent=4))
import sys
try:
# If running in a Jupyter notebook, Jupyter is already running an event loop, so run main with this line instead:
#await main()
asyncio.run(main())
except Exception as e:
exception_type, exception_object, exception_traceback = sys.exc_info()
line_number = exception_traceback.tb_lineno
print(f'line {line_number}: {exception_type} - {e}')
The console gives an expected output in Json format, but I get the error KnimeUserError: Output table ‘0’ must be of type knime.api.Table or knime.api.BatchOutputTable, but got <class ‘py4j.java_gateway.JavaObject’>
As I understand, I need to assign knio.output_tables[0] the output I want to get to be able to work further with the response, but I don’t know how to do that correctly and everything I try from searching for this issue gives me errors.
Please advise,
Thank you.