Skip to content

Commit

Permalink
update notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
0thernet committed Jun 7, 2024
1 parent fb7420a commit 28d17c0
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 89 deletions.
49 changes: 27 additions & 22 deletions examples/notebooks/film-showings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
app = marimo.App(width="full")


@app.cell(hide_code=True)
@app.cell
def __(__file__):
import os
import sys
Expand Down Expand Up @@ -47,19 +47,19 @@ def __(__file__):

@app.cell
def __():
return


@app.cell
def __():
import requests, re
from bs4 import BeautifulSoup
from datetime import datetime, timedelta

today = datetime.today()
tomorrow = today + timedelta(days=1)
today_str = today.strftime("%Y-%m-%d")
tomorrow_str = tomorrow.strftime("%Y-%m-%d")
return datetime, timedelta, today, today_str, tomorrow, tomorrow_str


@app.cell
def __(today_str, tomorrow_str):
import requests, re
from bs4 import BeautifulSoup

response = requests.get(f"https://metrograph.com/calendar")
soup = BeautifulSoup(response.text, "html.parser")
Expand All @@ -78,14 +78,20 @@ def __(today_str, tomorrow_str):
return (
BeautifulSoup,
anchor,
datetime,
pattern,
pattern_re,
re,
requests,
response,
soup,
timedelta,
today,
today_el,
today_str,
tomorrow,
tomorrow_el,
tomorrow_str,
urls,
)

Expand Down Expand Up @@ -115,6 +121,7 @@ def __(
):
summaries = []
mds = []
import json


class Film(BaseModel):
Expand All @@ -124,36 +131,34 @@ class Film(BaseModel):
showtimes: list[str] = Field(..., description="List of showtimes for the film.")


for url in urls[:5]:
for url in urls[:1]:
md = RunPython(
input={
"url": url,
},
code="""import httpx
code="""import requests
from bs4 import BeautifulSoup
client = httpx.AsyncClient()
from markdownify import markdownify
url = SB_IN['url']
print(url)
res = await client.get(url)
await client.aclose()
soup = BeautifulSoup(res.text, 'html.parser')
text = ' '.join([l.strip() for l in soup.text.splitlines() if len(l.strip()) > 0])
print(text)
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
SB_OUT['markdown'] = markdownify(str(soup))
""",
pip_install=["httpx", "beautifulsoup4"],
pip_install=["requests", "beautifulsoup4", "markdownify"],
)

mds.append(md)

json = GenerateJSON(
summary = GenerateJSON(
prompt=sb.concat(
"Summarize the following markdown about a film playing at Metrograph Theater. Do not include Now Playing in the title.\n ",
md.future.stdout,
"Summarize the following markdown about a film playing at local theater, generating JSON following the schema below\n ",
md.future.output["markdown"],
),
json_schema=Film.model_json_schema(),
node="Llama3Instruct8B",
)
summaries.append(json)
summaries.append(summary)

markdown = GenerateText(
prompt=sb.concat(
Expand All @@ -163,7 +168,7 @@ class Film(BaseModel):
node="Llama3Instruct70B",
)
res = substrate.run(*summaries, *mds, markdown)
return Film, json, markdown, md, mds, res, summaries, url
return Film, json, markdown, md, mds, res, summaries, summary, url


@app.cell
Expand Down
26 changes: 12 additions & 14 deletions examples/notebooks/image-game.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __(mo):
full_width=True,
).form()
prompt
return (prompt,)
return prompt,


@app.cell
Expand Down Expand Up @@ -107,7 +107,7 @@ def __(mo):
.form()
)
guesses
return (guesses,)
return guesses,


@app.cell
Expand All @@ -134,7 +134,7 @@ def __(collection_name, guesses, sb):
def __(embed1, embed2, guess1image, guess2image, mo, substrate):
guess_res = substrate.run(guess1image, guess2image, embed1, embed2)
mo.accordion({"response": mo.tree(guess_res.json)})
return (guess_res,)
return guess_res,


@app.cell
Expand All @@ -149,18 +149,16 @@ def __(embed1, embed2, guess_res, mo):
def __(collection_name, guess1image, guess2image, guess_res, sb):
# query with the embeddings of the two image guesses
query = sb.QueryVectorStore(
{
"model": "clip",
"collection_name": collection_name,
"query_image_uris": [
guess_res.get(guess1image).image_uri,
guess_res.get(guess2image).image_uri,
],
"top_k": 100,
"ef_search": 64,
}
model="clip",
collection_name=collection_name,
query_image_uris=[
guess_res.get(guess1image).image_uri,
guess_res.get(guess2image).image_uri,
],
top_k=100,
ef_search=64,
)
return (query,)
return query,


@app.cell
Expand Down
82 changes: 44 additions & 38 deletions examples/notebooks/interior-design.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def __(__file__):
def __(api_key):
from substrate import (
Substrate,
RemoveBackground,
StableDiffusionXLControlNet,
UpscaleImage,
StableDiffusionXLInpaint,
GenerativeEditImage,
GenerateTextVision,
GenerateText,
sb,
Expand All @@ -38,10 +38,10 @@ def __(api_key):
return (
GenerateText,
GenerateTextVision,
RemoveBackground,
GenerativeEditImage,
StableDiffusionXLControlNet,
StableDiffusionXLInpaint,
Substrate,
UpscaleImage,
sb,
substrate,
)
Expand All @@ -51,26 +51,25 @@ def __(api_key):
def __(
GenerateText,
GenerateTextVision,
StableDiffusionXLControlNet,
StableDiffusionXLInpaint,
sb,
substrate,
):
num_frames = 8
original_image = "https://media.substrate.run/office.jpg"
times = ["5am in tokyo", "10am in paris", "6pm in london", "10pm in berlin"]
styles = ["sunlit onsen style tokyo office", "80s disco style berlin office at night"]
images = [
StableDiffusionXLControlNet(
image_uri=original_image,
control_method="depth",
# conditioning_scale=1,
prompt=sb.concat("cozy office at ", t),
num_images=num_frames,
StableDiffusionXLInpaint(
image_uri="https://media.substrate.run/office.jpg",
# control_method="depth",
strength=0.75,
prompt=s,
num_images=1,
)
for t in times
for s in styles
]
captions = [
descriptions = [
GenerateTextVision(
prompt="Describe the interesting interior decor touches in this image",
# image_uris=[i.future.image_uri],
image_uris=[i.future.outputs[0].image_uri],
)
for i in images
Expand All @@ -79,21 +78,19 @@ def __(
GenerateText(
prompt=sb.concat(
"Summarize the 2 most interesting details in one sentence, be concise: ",
c.future.text,
d.future.text,
),
)
for c in captions
for d in descriptions
]
res = substrate.run(*images, *captions, *summaries)
return (
captions,
images,
num_frames,
original_image,
res,
summaries,
times,
)
res = substrate.run(*images, *descriptions, *summaries)
return descriptions, images, res, styles, summaries


@app.cell
def __(mo, res):
mo.tree(res.json)
return


@app.cell
Expand All @@ -104,17 +101,17 @@ def __(mo, original_image):

@app.cell
def __(images, mo, res, summaries):
mo.hstack(
mo.vstack(
[
mo.vstack(
[
mo.image(res.get(images[0]).outputs[0].image_uri),
mo.image(res.get(images[0]).image_uri),
mo.md(res.get(summaries[0]).text),
]
),
mo.vstack(
[
mo.image(res.get(images[1]).outputs[0].image_uri),
mo.image(res.get(images[1]).image_uri),
mo.md(res.get(summaries[1]).text),
]
),
Expand All @@ -125,33 +122,42 @@ def __(images, mo, res, summaries):

@app.cell
def __(images, mo, res, summaries):
mo.hstack(
mo.vstack(
[
mo.vstack(
[
mo.image(res.get(images[2]).outputs[0].image_uri),
mo.md(res.get(summaries[2]).text),
mo.image(res.get(images[0]).outputs[0].image_uri),
mo.md(res.get(summaries[0]).text),
]
),
mo.vstack(
[
mo.image(res.get(images[3]).outputs[0].image_uri),
mo.md(res.get(summaries[3]).text),
mo.image(res.get(images[1]).outputs[0].image_uri),
mo.md(res.get(summaries[1]).text),
]
),
]
)
return


@app.cell
def __(images, mo, res):
mo.hstack(
[
mo.download(res.get(images[0]).image_uri),
mo.download(res.get(images[1]).image_uri),
]
)
return


@app.cell
def __(images, mo, res):
mo.hstack(
[
mo.download(res.get(images[0]).outputs[0].image_uri),
mo.download(res.get(images[1]).outputs[0].image_uri),
mo.download(res.get(images[2]).outputs[0].image_uri),
mo.download(res.get(images[3]).outputs[0].image_uri),
]
)
return
Expand Down
27 changes: 12 additions & 15 deletions examples/notebooks/logo-controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,40 @@ def __(api_key):
UpscaleImage,
)

substrate = Substrate(api_key=api_key)
s = Substrate(api_key=api_key)
return (
RemoveBackground,
StableDiffusionXLControlNet,
Substrate,
UpscaleImage,
substrate,
s,
)


@app.cell
def __(StableDiffusionXLControlNet, substrate):
num_images = 4
def __(RemoveBackground, StableDiffusionXLControlNet, s):
num_images = 2
mask = RemoveBackground(image_uri="https://media.substrate.run/logo-sq.png", return_mask=True)
controlnet = StableDiffusionXLControlNet(
image_uri="https://media.substrate.run/logo-mask.png",
image_uri=mask.future.image_uri,
control_method="illusion",
conditioning_scale=1,
prompt="birds-eye view of the ocean, turbulent choppy waves",
conditioning_scale=1.0,
prompt="street view futuristic solarpunk city of atlantis",
num_images=num_images,
)
res = substrate.run(controlnet)
return controlnet, num_images, res
res = s.run(mask, controlnet)
return controlnet, mask, num_images, res


@app.cell
def __(controlnet, mo, num_images, res):
mo.hstack([
mo.image(res.get(controlnet).outputs[i].image_uri) for i in list(range(num_images))
])
mo.hstack([mo.image(res.get(controlnet).outputs[i].image_uri) for i in list(range(num_images))])
return


@app.cell
def __(controlnet, mo, num_images, res):
mo.hstack([
mo.download(res.get(controlnet).outputs[i].image_uri) for i in list(range(num_images))
])
mo.hstack([mo.download(res.get(controlnet).outputs[i].image_uri) for i in list(range(num_images))])
return


Expand Down
Loading

0 comments on commit 28d17c0

Please sign in to comment.