add try catch and robust integration

This commit is contained in:
Marco Vinciguerra 2024-08-19 14:24:27 +02:00
parent f60aa3acde
commit f774fe40e5
2 changed files with 20 additions and 4 deletions

View File

@ -42,14 +42,14 @@ class ScreenshotScraperGraph(AbstractGraph):
"""
fetch_screen_node = FetchScreenNode(
input="url",
output=["imgs"],
output=["screenshots"],
node_config={
"link": self.source
}
)
generate_answer_from_image_node = GenerateAnswerFromImageNode(
input="imgs",
output=["answer"],
output=["screenshots"],
node_config={
"config": self.config
}

View File

@ -1,3 +1,6 @@
"""
GenerateAnswerFromImageNode Module
"""
import base64
import asyncio
from typing import List, Optional
@ -21,7 +24,9 @@ class GenerateAnswerFromImageNode(BaseNode):
super().__init__(node_name, "node", input, output, 2, node_config)
async def process_image(self, session, api_key, image_data, user_prompt):
# Convert image data to base64
"""
async process image
"""
base64_image = base64.b64encode(image_data).decode('utf-8')
headers = {
@ -96,4 +101,15 @@ class GenerateAnswerFromImageNode(BaseNode):
"""
Wrapper to run the asynchronous execute_async function in a synchronous context.
"""
return asyncio.run(self.execute_async(state))
try:
eventloop = asyncio.get_event_loop()
except RuntimeError:
eventloop = None
if eventloop and eventloop.is_running():
task = eventloop.create_task(self.execute_async(state))
state = eventloop.run_until_complete(asyncio.gather(task))[0]
else:
state = asyncio.run(self.execute_async(state))
return state