Get Top Reddit Posts
This example illustrates a few capabilities of Beam:
- Building apps using FastAPI
- Using the hot-reloading feature to develop locally
- Deploying apps to a persistent REST endpoint
Import Beam modules
You’ll start by importing a Beam App
and Runtime
App
is the namespace for a project. You’ll give it a unique name as an identifier.- Inside the
App
is aRuntime
. TheRuntime
is a definition of the hardware your container will run on.
from beam import App, Runtime, Image
beam_app = App(
"scrape-reddit",
runtime=Runtime(
cpu=1,
memory="1Gi",
image=Image(python_packages=["fastapi", "httpx"]),
),
)
This app requires pydantic
to be installed on your local machine. You can install it by running pip install pydantic
.
Running a function to retrieve Reddit posts
We’re going to deploy a function that retrieves top posts from a given subreddit.
from beam import App, Runtime, Image
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
import requests
beam_app = App(
"scrape-reddit",
runtime=Runtime(
cpu=1,
memory="1Gi",
image=Image(python_packages=["fastapi", "httpx"]),
),
)
# Define a FastAPI app
app = FastAPI()
# Pydantic model for a Reddit post
class RedditPost(BaseModel):
title: str
url: str
upvotes: int
# Function to get top posts from Reddit
def get_top_posts(subreddit: str, limit: int = 5): # -> List[RedditPost]:
headers = {"User-Agent": "FastAPI Reddit Bot/0.1"}
params = {"limit": limit}
response = requests.get(
f"https://www.reddit.com/r/{subreddit}/top/.json",
headers=headers,
params=params,
)
if response.status_code != 200:
raise HTTPException(status_code=404, detail="Subreddit not found")
top_posts = []
for post in response.json()["data"]["children"]:
data = post["data"]
top_posts.append(
RedditPost(title=data["title"], url=data["url"], upvotes=data["ups"])
)
return top_posts
# Endpoint to get top posts from a specific subreddit
@app.get("/r/{subreddit}/top", response_model=List[RedditPost])
def read_top_posts(subreddit: str, limit: Optional[int] = 1):
return get_top_posts(subreddit, limit)
# Entrypoint to the app. When deployed, this HTTP endpoint will be publicly exposed to the internet.
@beam_app.asgi(authorized=False)
def handler():
return app
Developing your app on Beam
Beam includes a live-reloading feature that allows you to run your code on the same environment you’ll be running in production.
By default, Beam will sync all the files in your working directory to the remote container. This allows you to use the files you have locally while developing. If you want to prevent some files from getting uploaded, you can create a .beamignore
.
In your shell, run beam serve app.py
. This will:
- Spin up a container
- Run the container on a cloud machine
- Print a cURL request to invoke the API
- Stream the logs to your shell
You should keep this terminal window open while developing.
(.venv) user@MacBook demo % beam serve app.py
i Using cached image.
✓ App initialized.
i Uploading files...
✓ Container scheduled, logs will appear below.
⠴ Starting container... 5s (Estimated: 3m20s)
================= Call the API =================
curl -X GET https://eh1dq-655a376a6d7360000745eaef.apps.beam.cloud
============= Logs Streamed Below ==============
INFO: | GET - /r/machinelearning/top
INFO: | GET - /favicon.ico
Now, head back to your IDE, and change a line of code. Hit save.
If you look closely at the shell running beam serve
, you’ll notice the server reloading with your code changes.
You’ll use this workflow anytime you’re developing an app on Beam. Trust us — it makes the development process uniquely fast and painless.
Call the API
Open a browser window and paste the URL printed when you ran beam serve
.
You can customize this URL with the name of a subreddit. In this example, we’ll use r/machinelearning
.
https://eh1dq-655a35a06d73600009707a99.apps.beam.cloud/r/machinelearning/top
This GET
request returns a top post from the subreddit:
[{"title":"[D] Skill Creep in ML/DL Roles - is the field getting not just more competitive, but more difficult?","url":"https://www.reddit.com/r/MachineLearning/comments/17yp1l5/d_skill_creep_in_mldl_roles_is_the_field_getting/","upvotes":78}]
Deployment
Now it’s time to deploy the app to create a persistent endpoint. In your shell, run this command to deploy your app:
beam deploy app.py
Unlike a temporary beam serve
session, this will create a persistent deployment and will remain active until you stop it.
Further Reading
Was this page helpful?