mirror of
https://github.com/VinciGit00/Scrapegraph-ai.git
synced 2026-07-01 21:00:48 +08:00
add try catch and robust integration
This commit is contained in:
parent
f60aa3acde
commit
f774fe40e5
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user