acheong08 / Bard

Python SDK/API for reverse engineered Google Bard
MIT License
1.42k stars 178 forks source link

RuntimeError: There is no current event loop in thread #62

Closed hemangsrr closed 1 year ago

hemangsrr commented 1 year ago

I am getting the following error when calling Bard

RuntimeError: There is no current event loop in thread 'Thread-77 (process_request_thread)'.

Traceback (most recent call last):
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\flask\app.py", line 2213, in __call__
    return self.wsgi_app(environ, start_response)
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\flask_socketio\__init__.py", line 43, in __call__
    return super(_SocketIOMiddleware, self).__call__(environ,
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\engineio\middleware.py", line 74, in __call__
    return self.wsgi_app(environ, start_response)
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\flask\app.py", line 2193, in wsgi_app
    response = self.handle_exception(e)
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\flask\app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\flask\app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\flask\app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\flask\app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "D:\Hemang\GoogleBard\GoogleBard\WebApp.py", line 201, in SearchText
    final_content = BardResponse(sorted_data)
  File "D:\Hemang\GoogleBard\GoogleBard\WebApp.py", line 113, in BardResponse
    bard = Chatbot(token)
  File "D:\Hemang\GoogleBard\GoogleBard\EntSearch\lib\site-packages\Bard.py", line 62, in __init__
    self.loop = asyncio.get_event_loop()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\asyncio\events.py", line 656, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-77 (process_request_thread)'.
def BardResponse(sorted_data):
    global bard
    token = <My-Token>
    bard = Chatbot(token)    
    bard_data = []
    for data in sorted_data:
        print("Data passed to Bard --------------------------")
        print(data["sentence"])
        Answer = bard.ask('Summarize this: ' + data["sentence"])
        response = Answer["content"]
        bard_data.append({'response': response})
    return bard_data

This was working until a couple days ago, and then I'm getting this error

hemangsrr commented 1 year ago

For now I managed to fix it with the following changes to my code

async def BardResponse(sorted_data):
    global bard
    token = <My-Token>
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    future_bard = loop.run_in_executor(None, create_chatbot, token)
    # Wait for the future to complete and get the Chatbot instance
    bard = await future_bard   
    bard_data = []
    print(sorted_data)
    for data in sorted_data:
        print("Data passed to Bard --------------------------")
        print(data["sentence"])
        Answer = bard.ask('Summarize this: ' + data["sentence"])
        response = Answer["content"]
        bard_data.append({'response': response})
    return bard_data

and changed the function where this was being called to async as well and added the following to the call await BardResponse(sorted_data)

to make this work with flask, i also had to reinstall Flask with the 'async' extra

I'm leaving this here as reference in case anyone else comes across a similar issue

acheong08 commented 1 year ago

from Bard import AsyncChatbot

async def BardResponse(sorted_data):

    token = <My-Token>
    bard = await AsyncChatbot(token)
    bard_data = []
    print(sorted_data)
    for data in sorted_data:
        print("Data passed to Bard --------------------------")
        print(data["sentence"])
        Answer = await bard.ask('Summarize this: ' + data["sentence"])
        response = Answer["content"]
        bard_data.append({'response': response})
    return bard_data
hemangsrr commented 1 year ago

That will definitely make my code cleaner and avoid unnecessary async loop creation. I'll try it out