Closed aureliuszi closed 1 year ago
Good question @aureliuszi! Jupyter has its own async event loop, so you'll need to merge in the Hume streaming event loop.
Hope this example code helps:
import asyncio
from hume import HumeStreamClient, StreamSocket
from hume.config import FaceConfig
async def main():
client = HumeStreamClient("<your-api-key>")
configs = [FaceConfig(identify_faces=True)]
async with client.connect(configs) as socket:
socket: StreamSocket
result = await socket.send_file("<your-image-filepath>")
print(result)
# Jupyter has its own async event loop, so merge main into the Jupyter event loop
loop = asyncio.get_event_loop()
loop.create_task(main())
Wow, thank you so much for this! It is really appreciated!
I am trying to print the results of the analysis but I am still unable to. I tried to modify the code like so but to no avail:
async def main():
client = HumeStreamClient("api-key")
configs = [FaceConfig(identify_faces=True)]
async with client.connect(configs) as socket:
socket: StreamSocket
result = await socket.send_file("raw-social-media-data/bobo.mp4")
print(result)
**result.download("predictions-local.json")**
loop = asyncio.get_event_loop()
loop.create_task(main())
Hi @aureliuszi, calling await socket.send_file()
will return the results to you in a dictionary format. There's no need to do result.download()
. If you follow the code I sent above, these two lines are all you need to print out the results.
result = await socket.send_file("<your-image-filepath>")
print(result)
One thing to look out for, @aureliuszi, is that exceptions will not be raised from within an asyncio Task. You can use this example to make sure exceptions get printed:
import asyncio
from hume import HumeStreamClient, StreamSocket
from hume.config import FaceConfig
async def main():
client = HumeStreamClient("<your-api-key>")
configs = [FaceConfig(identify_faces=True)]
async with client.connect(configs) as socket:
socket: StreamSocket
try:
result = await socket.send_file("<your-image-filepath>")
except Exception as exc:
print(exc)
raise
print(result)
loop = asyncio.get_event_loop()
loop.create_task(main())
Since this has been open for a week I'll assume everything is resolved and close this issue
Hello friendly HumeAI team!
I am trying to run the following code in Jupyter Notebook:
But I get this error. Please help!
RuntimeError Traceback (most recent call last) Cell In[9], line 9 6 result = await socket.send_file("raw-social-media-data/bobo.mp4") 7 print(result) ----> 9 asyncio.run(main())