Skip to content

Commit f774fe4

Browse files
committed
add try catch and robust integration
1 parent f60aa3a commit f774fe4

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

scrapegraphai/graphs/screenshot_scraper_graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def _create_graph(self) -> BaseGraph:
4242
"""
4343
fetch_screen_node = FetchScreenNode(
4444
input="url",
45-
output=["imgs"],
45+
output=["screenshots"],
4646
node_config={
4747
"link": self.source
4848
}
4949
)
5050
generate_answer_from_image_node = GenerateAnswerFromImageNode(
5151
input="imgs",
52-
output=["answer"],
52+
output=["screenshots"],
5353
node_config={
5454
"config": self.config
5555
}

scrapegraphai/nodes/generate_answer_from_image_node.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
GenerateAnswerFromImageNode Module
3+
"""
14
import base64
25
import asyncio
36
from typing import List, Optional
@@ -21,7 +24,9 @@ def __init__(
2124
super().__init__(node_name, "node", input, output, 2, node_config)
2225

2326
async def process_image(self, session, api_key, image_data, user_prompt):
24-
# Convert image data to base64
27+
"""
28+
async process image
29+
"""
2530
base64_image = base64.b64encode(image_data).decode('utf-8')
2631

2732
headers = {
@@ -96,4 +101,15 @@ def execute(self, state: dict) -> dict:
96101
"""
97102
Wrapper to run the asynchronous execute_async function in a synchronous context.
98103
"""
99-
return asyncio.run(self.execute_async(state))
104+
try:
105+
eventloop = asyncio.get_event_loop()
106+
except RuntimeError:
107+
eventloop = None
108+
109+
if eventloop and eventloop.is_running():
110+
task = eventloop.create_task(self.execute_async(state))
111+
state = eventloop.run_until_complete(asyncio.gather(task))[0]
112+
else:
113+
state = asyncio.run(self.execute_async(state))
114+
115+
return state

0 commit comments

Comments
 (0)