Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get_usage_request when Response Not Available #473

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion deepgram/clients/manage/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import logging
from typing import Dict, Union, Optional
import json

import httpx

from ....utils import verboselogs
from ....options import DeepgramClientOptions
from ...common import AbstractAsyncRestClient
from ...common import AbstractAsyncRestClient, DeepgramError

from .response import (
Message,
Expand Down Expand Up @@ -1000,6 +1001,14 @@ async def get_usage_request(
url, timeout=timeout, addons=addons, headers=headers, **kwargs
)
self._logger.info("result: %s", result)

# convert str to JSON to check response field
json_result = json.loads(result)
if json_result.get("response") is None:
raise DeepgramError(
"Response is not available yet. Please try again later."
)

res = UsageRequest.from_json(result)
self._logger.verbose("result: %s", res)
self._logger.notice("get_usage_request succeeded")
Expand Down
11 changes: 10 additions & 1 deletion deepgram/clients/manage/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import logging
from typing import Dict, Union, Optional
import json

import httpx

from ....utils import verboselogs
from ....options import DeepgramClientOptions
from ...common import AbstractSyncRestClient
from ...common import AbstractSyncRestClient, DeepgramError

from .response import (
Message,
Expand Down Expand Up @@ -1003,6 +1004,14 @@ def get_usage_request(
url, timeout=timeout, addons=addons, headers=headers, **kwargs
)
self._logger.info("json: %s", result)

# convert str to JSON to check response field
json_result = json.loads(result)
if json_result.get("response") is None:
raise DeepgramError(
"Response is not available yet. Please try again later."
)

res = UsageRequest.from_json(result)
self._logger.verbose("result: %s", res)
self._logger.notice("get_usage_request succeeded")
Expand Down
83 changes: 83 additions & 0 deletions tests/edge_cases/usage_to_fast/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

import os
import sys
from dotenv import load_dotenv
import logging
from deepgram.utils import verboselogs

import httpx

from deepgram import (
DeepgramClient,
DeepgramClientOptions,
PrerecordedOptions,
FileSource,
)

load_dotenv()

AUDIO_FILE = "sample.mp3"


def main():
try:
# Create a Deepgram client using the API key
# config: DeepgramClientOptions = DeepgramClientOptions(verbose=verboselogs.SPAM)
config: DeepgramClientOptions = DeepgramClientOptions()
deepgram: DeepgramClient = DeepgramClient("", config)
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved

# get projects
projectResp = deepgram.manage.v("1").get_projects()
if projectResp is None:
print(f"ListProjects failed.")
sys.exit(1)

myId = None
myName = None
for project in projectResp.projects:
myId = project.project_id
myName = project.name
print(f"ListProjects() - ID: {myId}, Name: {myName}")
break

with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()

payload: FileSource = {
"buffer": buffer_data,
}

options: PrerecordedOptions = PrerecordedOptions(
callback="http://example.com",
model="nova-2",
smart_format=True,
utterances=True,
punctuate=True,
diarize=True,
)

response = deepgram.listen.rest.v("1").transcribe_file(
payload, options, timeout=httpx.Timeout(300.0, connect=10.0)
)
request_id = (
response.request_id
) # without callback: response.metadata.request_id
print(f"request_id: {request_id}")
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved

# get request
getResp = deepgram.manage.v("1").get_usage_request(myId, request_id)
if getResp is None:
print("No request found")
else:
print(f"GetUsageRequest() - getResp: {getResp}")
print("\n\n\n")

except Exception as e:
print(f"Exception: {e}")
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
main()
Loading