Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz committed Nov 13, 2024
1 parent 157276a commit 1230273
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions examples/read_hn.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# /// script
# dependencies = ["controlflow"]
# ///

import os
import sys
from pathlib import Path
from typing import Annotated, TypedDict

import httpx
from prefect.artifacts import create_markdown_artifact
from prefect.blocks.system import Secret
from prefect.docker import DockerImage
from prefect.runner.storage import GitCredentials, GitRepository
from pydantic import AnyHttpUrl, Field
Expand All @@ -26,52 +32,68 @@ def analyze_article(id: str) -> HNArticleSummary:
return f"here is the article content: {content}" # type: ignore


@cf.task()
def summarize_article_briefs(
briefs: list[HNArticleSummary],
) -> Annotated[str, Field(description="markdown summary")]:
"""Summarize a list of article briefs"""
return f"here are the article briefs: {briefs}" # type: ignore


@cf.flow(retries=2)
def analyze_articles(n: int = 5) -> list[HNArticleSummary]:
def analyze_hn_articles(n: int = 5):
top_article_ids = httpx.get(
"https://hacker-news.firebaseio.com/v0/topstories.json"
).json()[:n]
return analyze_article.map(top_article_ids).result()
briefs = analyze_article.map(top_article_ids).result()
create_markdown_artifact(
key="hn-article-exec-summary",
markdown=summarize_article_briefs(briefs),
)


if __name__ == "__main__":
EVERY_12_HOURS_CRON = "0 */12 * * *"
if len(sys.argv) > 1 and sys.argv[1] == "serve":
analyze_articles.serve(
analyze_hn_articles.serve(
parameters={"n": 5},
cron="*/5 * * * *",
cron=EVERY_12_HOURS_CRON,
)
elif len(sys.argv) > 1 and sys.argv[1] == "local_deploy":
analyze_articles.from_source(
analyze_hn_articles.from_source(
source=str((p := Path(__file__)).parent.resolve()),
entrypoint=f"{p.name}:analyze_articles",
).deploy(name="local-deployment", work_pool_name="local-process-pool")
entrypoint=f"{p.name}:analyze_hn_articles",
).deploy(
name="local-deployment",
work_pool_name="local",
cron=EVERY_12_HOURS_CRON,
)
elif len(sys.argv) > 1 and sys.argv[1] == "docker_deploy":
repo = GitRepository(
url="https://github.com/PrefectHQ/controlflow.git",
branch="example-deploy",
branch="main",
credentials=None, # replace with `dict(username="", access_token="")` for private repos
)
analyze_articles.from_source(
analyze_hn_articles.from_source(
source=repo,
entrypoint="examples/read_hn.py:analyze_articles",
).deploy(
name="docker-deployment",
# image=DockerImage( # uncomment and replace with your own image
# name="zzstoatzz/cf-test-deploy",
# image=DockerImage( # uncomment and replace with your own image if desired
# name="zzstoatzz/cf-read-hn",
# tag="latest",
# dockerfile=str(
# Path(__file__).parent.resolve() / "prefect-deploy.Dockerfile"
# ),
# dockerfile=str(Path(__file__).parent.resolve() / "read-hn.Dockerfile"),
# ),
work_pool_name="docker-work", # uv pip install _U prefect-docker prefect worker start --pool docker-work --type docker
parameters={"topics": ["roses", "violets", "sugar", "spice"]},
work_pool_name="docker-work", # uv pip install -U prefect-docker prefect worker start --pool docker-work --type docker
cron=EVERY_12_HOURS_CRON,
parameters={"n": 5},
job_variables={
"env": {"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY")},
"image": "zzstoatzz/cf-test-deploy:latest", # publicly available image on dockerhub
"image": "zzstoatzz/cf-read-hn:latest", # publicly available image on dockerhub
},
build=False,
push=False,
)
else:
print(f"just running the code\n\n\n\n\n\n")
analyze_articles(5)
analyze_hn_articles(5)

0 comments on commit 1230273

Please sign in to comment.