I have a gui program build in python with pywebview. When clicking on a button a prompt is executed via the replicate api. Everything works well when the button is pressed for the first time. After the function fetching the replicate api is finished I press the button a second time and I get the following error:
…
UsersbaschAppDataLocalProgramsPythonPython310libasynciobase_events.py”, line 515, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Here are some of the functions involved:
the function when triggered with the button:
...
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
response = asyncio.run(config.execute_prompt(gui, prompt_text, {parameter.name: parameter.get_value() for parameter in config.parameters.values()}, append_prediction_part))
...
config.execute_prompt:
async def execute_prompt(self, gui, prompt, parameters, update_func):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
self.gui = gui
result = await predict(self.model, prompt, parameters, update_func, self.set_prediction)
return result
predict function:
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_KEY
input = {}
prompt = deepcopy(prompt)
for key, value in parameters.items():
if value is not None:
input[key] = value
if "pre_prompt" in input:
prompt = input["pre_prompt"] + "nn" + prompt
del input["pre_prompt"]
from pprint import pprint
input["prompt"] = prompt
pprint(input)
#print(input, model, prompt, parameters)
prediction = await replicate.models.predictions.async_create(
model,
input=input
)
set_prediction(prediction)
resp = {
"status": prediction.status
}
previous_output = ""
while resp["status"] != "succeeded" and resp["status"] != "failed":
resp = await prediction._client._async_request(
"GET",
prediction.urls["get"]
)
# _json_to_prediction(resp._client, resp.json())
# print(resp.decod.json())
resp = loads(resp.content.decode("utf-8"))
pprint(resp)
if "output" in resp and resp["output"] is not None:
output = "".join(resp["output"])
update(output[len(previous_output):])
previous_output = output
return resp
The predict function sends the prompt to the replicate API and then retrieves the output of the llm as it progresses, sending the new parts to the update function, which will write to a div in the pywebview api. The last response is the finished output and will be returned
I am using python 3.10.4, Windows 11 with all recent updates, and replicate api 0.26.0
Am I using asyncio right? What can I do to resolve the issue?
Kind regards
Bastian
I was trying to write an asynchronous request to replicate, which should be triggered by a button. The first button click works as expected, the second one leads to the exception.