Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

fix(examples): bound image telephone downloads (#879)

Rolly Calmac96b5f78f68a
diff --git a/examples/image-telephone/application.py b/examples/image-telephone/application.py
index fa5aaf77..35ab2984 100644
--- a/examples/image-telephone/application.py
+++ b/examples/image-telephone/application.py
@@ -42,6 +42,8 @@ from burr.core import Action, ApplicationBuilder, State, default, expr
from burr.core.action import action
from burr.lifecycle import PostRunStepHook
+IMAGE_DOWNLOAD_TIMEOUT_SECONDS = 30
+
# import hamilton modules that define the DAGs for image captioning and image generation
caption_images = dataflows.import_module("caption_images", "elijahbenizzy")
generate_images = dataflows.import_module("generate_images", "elijahbenizzy")
@@ -67,7 +69,11 @@ class ImageSaverHook(PostRunStepHook):
image_url = state["current_image_location"]
image_name = "image_" + str(state["__SEQUENCE_ID"]) + ".png"
with open(os.path.join(self.path, image_name), "wb") as f:
- f.write(requests.get(image_url).content)
+ response = requests.get(
+ image_url, timeout=IMAGE_DOWNLOAD_TIMEOUT_SECONDS
+ )
+ response.raise_for_status()
+ f.write(response.content)
print(f"Saved image to {self.path}/{image_name}")
diff --git a/examples/integrations/hamilton/image-telephone/application.py b/examples/integrations/hamilton/image-telephone/application.py
index 4f7f5fa1..44b44b5a 100644
--- a/examples/integrations/hamilton/image-telephone/application.py
+++ b/examples/integrations/hamilton/image-telephone/application.py
@@ -33,6 +33,8 @@ from burr.core.action import action
from burr.integrations import hamilton
from burr.lifecycle import PostRunStepHook
+IMAGE_DOWNLOAD_TIMEOUT_SECONDS = 30
+
caption_images = dataflows.import_module("caption_images", "elijahbenizzy")
generate_images = dataflows.import_module("generate_images", "elijahbenizzy")
@@ -53,7 +55,11 @@ class ImageSaverHook(PostRunStepHook):
image_url = state["current_image_location"]
image_name = "image_" + str(state["__SEQUENCE_ID"]) + ".png"
with open(os.path.join(self.path, image_name), "wb") as f:
- f.write(requests.get(image_url).content)
+ response = requests.get(
+ image_url, timeout=IMAGE_DOWNLOAD_TIMEOUT_SECONDS
+ )
+ response.raise_for_status()
+ f.write(response.content)
print(f"Saved image to {self.path}/{image_name}")