2022-06-30 20:45:15 +00:00
|
|
|
import logging
|
|
|
|
|
2023-08-28 15:28:58 +00:00
|
|
|
from lightning.app import LightningApp, LightningFlow, LightningWork
|
2022-06-30 20:45:15 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-08-28 15:28:58 +00:00
|
|
|
class PickleChecker(LightningWork):
|
2022-06-30 20:45:15 +00:00
|
|
|
def run(self, pickle_image: bytes):
|
|
|
|
parsed = self.parse_image(pickle_image)
|
|
|
|
if parsed == b"it is a pickle":
|
|
|
|
return True
|
2023-05-05 09:34:40 +00:00
|
|
|
if parsed == b"it is not a pickle":
|
2022-06-30 20:45:15 +00:00
|
|
|
return False
|
2023-05-05 09:34:40 +00:00
|
|
|
raise Exception("Couldn't parse the image")
|
2022-06-30 20:45:15 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_image(image_str: bytes):
|
|
|
|
return image_str
|
|
|
|
|
|
|
|
|
2023-08-28 15:28:58 +00:00
|
|
|
class Slack(LightningFlow):
|
2022-06-30 20:45:15 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def send_message(message):
|
|
|
|
logger.info(f"Sending message: {message}")
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-08-28 15:28:58 +00:00
|
|
|
class RootComponent(LightningFlow):
|
2022-06-30 20:45:15 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.pickle_checker = PickleChecker()
|
|
|
|
self.slack = Slack()
|
|
|
|
self.counter = 3
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self.counter > 0:
|
|
|
|
logger.info(f"Running the app {self.counter}")
|
|
|
|
image_str = b"it is not a pickle"
|
|
|
|
if self.pickle_checker.run(image_str):
|
|
|
|
self.slack.send_message("It's a pickle!")
|
|
|
|
else:
|
|
|
|
self.slack.send_message("It's not a pickle!")
|
|
|
|
self.counter -= 1
|
|
|
|
else:
|
2023-01-17 11:06:50 +00:00
|
|
|
self.stop("Pickle or Not End")
|
2022-06-30 20:45:15 +00:00
|
|
|
|
|
|
|
|
2023-08-28 15:28:58 +00:00
|
|
|
app = LightningApp(RootComponent())
|