Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
merwan committed Oct 12, 2022
2 parents dbb5d99 + 66b7447 commit e89ebb7
Show file tree
Hide file tree
Showing 25 changed files with 332 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist
build
.venv
*.local
.env
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Uh!live Python SDK
# Uh!ive Python SDK

The Uh!live Python SDK provides convenient access to the Uh!live API from
applications written in the Python language.
Expand Down
4 changes: 2 additions & 2 deletions examples/conversation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

To run the scripts, you'll need to set some environment variables:
```
export UHLIVE_API_TOKEN=a-valid-token
export UHLIVE_API_URL=wss://api.uh.live/
export UHLIVE_API_CLIENT=a-valid-client-identifier
export UHLIVE_API_SECRET=secret-pass-code
```

And then:
Expand Down
17 changes: 11 additions & 6 deletions examples/conversation/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import os
from time import time

import requests
import websocket as ws # type: ignore
from websocket import WebSocketTimeoutException # type: ignore

from uhlive.auth import build_authentication_request
from uhlive.stream.conversation import Conversation, Ok, build_conversation_url

parser = argparse.ArgumentParser(
Expand All @@ -16,14 +18,17 @@
parser.add_argument("conversation_id", help="Conversation ID")
args = parser.parse_args()

uhlive_url = os.environ["UHLIVE_API_URL"]
uhlive_token = os.environ["UHLIVE_API_TOKEN"]
uhlive_id = os.environ["UHLIVE_API_ID"]
uhlive_client = os.environ["UHLIVE_API_CLIENT"]
uhlive_secret = os.environ["UHLIVE_API_SECRET"]

url = build_conversation_url(uhlive_url, uhlive_token)
socket = ws.create_connection(url, timeout=5)
auth_url, auth_params = build_authentication_request(uhlive_client, uhlive_secret)
login = requests.post(auth_url, data=auth_params)
login.raise_for_status()
uhlive_token = login.json()["access_token"]

client = Conversation(uhlive_id, args.conversation_id, "observer")
url = build_conversation_url(uhlive_token)
socket = ws.create_connection(url, timeout=10)
client = Conversation(uhlive_client, args.conversation_id, "observer")

socket.send(client.join(readonly=True))

Expand Down
25 changes: 18 additions & 7 deletions examples/conversation/stream_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import time
from threading import Thread

import requests
import websocket as ws # type: ignore
from websocket import WebSocketTimeoutException # type: ignore

from uhlive.auth import build_authentication_request
from uhlive.stream.conversation import Conversation, Ok, build_conversation_url


Expand Down Expand Up @@ -48,16 +50,23 @@ def run(self):
action="store_false",
)
parser.add_argument("--without_rescoring", dest="rescoring", action="store_false")
parser.add_argument("--user", dest="user_id", default="")
parser.add_argument("--password", dest="user_pwd", default="")
args = parser.parse_args()

uhlive_url = os.environ["UHLIVE_API_URL"]
uhlive_token = os.environ["UHLIVE_API_TOKEN"]
uhlive_id = os.environ["UHLIVE_API_ID"]
uhlive_client = os.environ["UHLIVE_API_CLIENT"]
uhlive_secret = os.environ["UHLIVE_API_SECRET"]

url = build_conversation_url(uhlive_url, uhlive_token)
socket = ws.create_connection(url, timeout=10)
auth_url, auth_params = build_authentication_request(
uhlive_client, uhlive_secret, args.user_id, args.user_pwd
)
login = requests.post(auth_url, data=auth_params)
login.raise_for_status()
uhlive_token = login.json()["access_token"]

client = Conversation(uhlive_id, args.conversation_id, "Alice")
url = build_conversation_url(uhlive_token)
socket = ws.create_connection(url, timeout=10)
client = Conversation(uhlive_client, args.conversation_id, "Alice")

socket.send(
client.join(
Expand All @@ -69,8 +78,10 @@ def run(self):
audio_codec=args.codec,
)
)
join = time.time()
# check we didn't get an error on join
client.receive(socket.recv())
data = socket.recv()
print("join resp =", client.receive(data), "in", time.time() - join, "seconds")


sender = AudioSender(socket, client, args.audio_file, args.codec)
Expand Down
36 changes: 26 additions & 10 deletions examples/conversation/stream_file_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from aiohttp import ClientSession # type: ignore

from uhlive.auth import build_authentication_request
from uhlive.stream.conversation import Conversation, Ok, build_conversation_url


Expand All @@ -20,12 +21,18 @@ async def stream_file(audio_path, socket, client, codec):
await socket.send_str(client.leave())


async def main(uhlive_url, uhlive_token, uhlive_id, cmdline_args):
async def main(uhlive_client, uhlive_secret, cmdline_args):
async with ClientSession() as session:
async with session.ws_connect(
build_conversation_url(uhlive_url, uhlive_token)
) as socket:
client = Conversation(uhlive_id, cmdline_args.conversation_id, "Alice")
auth_url, auth_params = build_authentication_request(
uhlive_client, uhlive_secret, args.user_id, args.user_pwd
)
async with session.post(auth_url, data=auth_params) as login:
login.raise_for_status()
body = await login.json()
uhlive_token = body["access_token"]

async with session.ws_connect(build_conversation_url(uhlive_token)) as socket:
client = Conversation(uhlive_client, cmdline_args.conversation_id, "Alice")
# shortcut
await socket.send_str(
client.join(
Expand All @@ -37,9 +44,16 @@ async def main(uhlive_url, uhlive_token, uhlive_id, cmdline_args):
audio_codec=cmdline_args.codec,
)
)
join = time.time()
# check we didn't get an error on join
msg = await socket.receive()
client.receive(msg.data)
print(
"join resp =",
client.receive(msg.data),
"in",
time.time() - join,
"seconds",
)

streamer = asyncio.create_task(
stream_file(cmdline_args.audio_file, socket, client, cmdline_args.codec)
Expand Down Expand Up @@ -76,9 +90,11 @@ async def main(uhlive_url, uhlive_token, uhlive_id, cmdline_args):
action="store_false",
)
parser.add_argument("--without_rescoring", dest="rescoring", action="store_false")
parser.add_argument("--user", dest="user_id", default="")
parser.add_argument("--password", dest="user_pwd", default="")

args = parser.parse_args()

uhlive_url = os.environ["UHLIVE_API_URL"]
uhlive_token = os.environ["UHLIVE_API_TOKEN"]
uhlive_id = os.environ["UHLIVE_API_ID"]
asyncio.run(main(uhlive_url, uhlive_token, uhlive_id, args))
uhlive_client = os.environ["UHLIVE_API_CLIENT"]
uhlive_secret = os.environ["UHLIVE_API_SECRET"]
asyncio.run(main(uhlive_client, uhlive_secret, args))
21 changes: 15 additions & 6 deletions examples/conversation/stream_microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import os
import time

import requests
import sounddevice as sd # type: ignore
import websocket as ws # type: ignore
from websocket import WebSocketTimeoutException # type: ignore

from uhlive.auth import build_authentication_request
from uhlive.stream.conversation import Conversation, Ok, build_conversation_url

# Audio recording parameters
Expand Down Expand Up @@ -36,16 +38,23 @@ def callback(indata, frame_count, time_info, status):
action="store_false",
)
parser.add_argument("--without_rescoring", dest="rescoring", action="store_false")
parser.add_argument("--user", dest="user_id", default="")
parser.add_argument("--password", dest="user_pwd", default="")
args = parser.parse_args()

uhlive_url = os.environ["UHLIVE_API_URL"]
uhlive_token = os.environ["UHLIVE_API_TOKEN"]
uhlive_id = os.environ["UHLIVE_API_ID"]
uhlive_client = os.environ["UHLIVE_API_CLIENT"]
uhlive_secret = os.environ["UHLIVE_API_SECRET"]

url = build_conversation_url(uhlive_url, uhlive_token)
socket = ws.create_connection(url, timeout=10)
auth_url, auth_params = build_authentication_request(
uhlive_client, uhlive_secret, args.user_id, args.user_pwd
)
login = requests.post(auth_url, data=auth_params)
login.raise_for_status()
uhlive_token = login.json()["access_token"]

client = Conversation(uhlive_id, args.conversation_id, "Alice")
url = build_conversation_url(uhlive_token)
socket = ws.create_connection(url, timeout=10)
client = Conversation(uhlive_client, args.conversation_id, "Alice")

socket.send(
client.join(
Expand Down
27 changes: 18 additions & 9 deletions examples/conversation/stream_microphone_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sounddevice as sd # type: ignore
from aiohttp import ClientSession # type: ignore

from uhlive.auth import build_authentication_request
from uhlive.stream.conversation import Conversation, Ok, build_conversation_url


Expand Down Expand Up @@ -36,12 +37,18 @@ async def stream_mic(socket, client):
pass


async def main(uhlive_url, uhlive_token, uhlive_id, cmdline_args):
async def main(uhlive_client, uhlive_secret, cmdline_args):
async with ClientSession() as session:
async with session.ws_connect(
build_conversation_url(uhlive_url, uhlive_token)
) as socket:
client = Conversation(uhlive_id, cmdline_args.conversation_id, "Alice")
auth_url, auth_params = build_authentication_request(
uhlive_client, uhlive_secret, args.user_id, args.user_pwd
)
async with session.post(auth_url, data=auth_params) as login:
login.raise_for_status()
body = await login.json()
uhlive_token = body["access_token"]

async with session.ws_connect(build_conversation_url(uhlive_token)) as socket:
client = Conversation(uhlive_client, cmdline_args.conversation_id, "Alice")
# shortcut
await socket.send_str(
client.join(
Expand Down Expand Up @@ -89,9 +96,11 @@ async def main(uhlive_url, uhlive_token, uhlive_id, cmdline_args):
action="store_false",
)
parser.add_argument("--without_rescoring", dest="rescoring", action="store_false")
parser.add_argument("--user", dest="user_id", default="")
parser.add_argument("--password", dest="user_pwd", default="")

args = parser.parse_args()

uhlive_url = os.environ["UHLIVE_API_URL"]
uhlive_token = os.environ["UHLIVE_API_TOKEN"]
uhlive_id = os.environ["UHLIVE_API_ID"]
asyncio.run(main(uhlive_url, uhlive_token, uhlive_id, args))
uhlive_client = os.environ["UHLIVE_API_CLIENT"]
uhlive_secret = os.environ["UHLIVE_API_SECRET"]
asyncio.run(main(uhlive_client, uhlive_secret, args))
4 changes: 2 additions & 2 deletions examples/recognition/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Example scripts

To run the examples, you need to set these environment variables:
* `UHLIVE_API_URL`: the URL of the API you use, e.g. `wss://api.uh.live/bots`
* `UHLIVE_API_TOKEN`: your access token for the API
export UHLIVE_API_CLIENT=a-valid-client-identifier
export UHLIVE_API_SECRET=secret-pass-code

Note that you must use the right token for the right entrypoint URL.
17 changes: 13 additions & 4 deletions examples/recognition/async_bot_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sounddevice as sd # type: ignore
from aiohttp import ClientSession # type: ignore

from uhlive.auth import build_authentication_request
from uhlive.stream.recognition import Closed
from uhlive.stream.recognition import CompletionCause as CC
from uhlive.stream.recognition import (
Expand All @@ -21,6 +22,7 @@
RecognitionInProgress,
Recognizer,
StartOfInput,
build_connection_request,
)


Expand Down Expand Up @@ -147,12 +149,19 @@ async def confirm(self, text: str) -> bool:
)
return res.value

async def run(self, uhlive_url: str, uhlive_token: str):
async def run(self, uhlive_client: str, uhlive_secret: str):
async with ClientSession() as session:
self.session = session
async with session.ws_connect(
uhlive_url, headers={"Authorization": f"bearer {uhlive_token}"}
) as socket:
auth_url, auth_params = build_authentication_request(
uhlive_client, uhlive_secret
)
async with session.post(auth_url, data=auth_params) as login:
login.raise_for_status()
body = await login.json()
uhlive_token = body["access_token"]

url, headers = build_connection_request(uhlive_token)
async with session.ws_connect(url, headers=headers) as socket:
self.socket = socket
await socket.send_str(self.client.open("deskbot"))
await self.expect(Opened)
Expand Down
21 changes: 15 additions & 6 deletions examples/recognition/basic_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from aiohttp import ClientSession # type: ignore

from uhlive.auth import build_authentication_request
from uhlive.stream.recognition import Closed
from uhlive.stream.recognition import CompletionCause as CC
from uhlive.stream.recognition import (
Expand All @@ -13,6 +14,7 @@
RecognitionInProgress,
Recognizer,
StartOfInput,
build_connection_request,
)


Expand Down Expand Up @@ -40,9 +42,16 @@ async def stream(socket, client, audio_files):

async def main(uhlive_url: str, uhlive_token: str):
async with ClientSession() as session:
async with session.ws_connect(
uhlive_url, headers={"Authorization": f"bearer {uhlive_token}"}
) as socket:
auth_url, auth_params = build_authentication_request(
uhlive_client, uhlive_secret
)
async with session.post(auth_url, data=auth_params) as login:
login.raise_for_status()
body = await login.json()
uhlive_token = body["access_token"]

url, headers = build_connection_request(uhlive_token)
async with session.ws_connect(url, headers=headers) as socket:
client = Recognizer()

# Shortcuts
Expand Down Expand Up @@ -134,6 +143,6 @@ async def expect(*event_classes):


if __name__ == "__main__":
uhlive_url = os.environ["UHLIVE_API_URL"]
uhlive_token = os.environ["UHLIVE_API_TOKEN"]
asyncio.run(main(uhlive_url, uhlive_token))
uhlive_client = os.environ["UHLIVE_API_CLIENT"]
uhlive_secret = os.environ["UHLIVE_API_SECRET"]
asyncio.run(main(uhlive_client, uhlive_secret))
Loading

0 comments on commit e89ebb7

Please sign in to comment.