Skip to content

Commit

Permalink
gen image chain
Browse files Browse the repository at this point in the history
  • Loading branch information
0thernet committed May 1, 2024
1 parent 6320903 commit 4eeb246
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 119 deletions.
119 changes: 0 additions & 119 deletions examples/notebooks/code-tool.py

This file was deleted.

100 changes: 100 additions & 0 deletions examples/notebooks/gen-image-chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import marimo

__generated_with = "0.3.12"
app = marimo.App(width="medium")


@app.cell
def __():
import os
import json
import base64
import marimo as mo
import substrate as sb

api_key = os.environ.get("SUBSTRATE_API_KEY")
api_key = api_key or "YOUR_API_KEY"
mo.md(f"`{api_key}`")
return api_key, base64, json, mo, os, sb


@app.cell
def __(api_key, sb):
substrate = sb.Substrate(
api_key=api_key,
backend="v1",
)
return substrate,


@app.cell
def __(mo):
prompt = mo.ui.text(
placeholder="prompt",
value="A bowl of fruit",
full_width=True,
).form()
prompt
return prompt,


@app.cell
def __(prompt, sb):
image = sb.GenerateImage(
{
"prompt": prompt.value,
}
)
return image,


@app.cell
def __(image, sb):
rmbg = sb.RemoveBackground({
"image_uri": image.future.image_uri
})
return rmbg,


@app.cell
def __(image, sb):
upscale = sb.UpscaleImage({
"image_uri": image.future.image_uri
})
return upscale,


@app.cell
def __(image, mo, rmbg, substrate, upscale):
res = substrate.run(image, rmbg, upscale)
viz = substrate.visualize(image, rmbg, upscale)
mo.md(f"[visualize]({viz})")
return res, viz


@app.cell
def __(image, mo, res):
image_out = res.get(image)
mo.image(src=image_out.image_uri)
return image_out,


@app.cell
def __(mo, res, rmbg):
rmbg_out = res.get(rmbg)
mo.image(src=rmbg_out.image_uri)
return rmbg_out,


@app.cell
def __(mo, res, upscale):
upscale_out = res.get(upscale)
mo.download(
data=upscale_out.image_uri,
filename="upscaled.jpeg",
)
return upscale_out,


if __name__ == "__main__":
app.run()
113 changes: 113 additions & 0 deletions examples/notebooks/run-code-chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import marimo

__generated_with = "0.3.12"
app = marimo.App(width="medium")


@app.cell
def __():
import os
import json
import base64
import marimo as mo
from substrate import Substrate, GenerateJSON, GenerateText, RunCode, sb

api_key = os.environ.get("SUBSTRATE_API_KEY")
api_key = api_key or "YOUR_API_KEY"
mo.md(f"`{api_key}`")
return (
GenerateJSON,
GenerateText,
RunCode,
Substrate,
api_key,
base64,
json,
mo,
os,
sb,
)


@app.cell
def __(Substrate, api_key, os):
user_id = os.environ.get("SUBSTRATE_USER_ID")
ray_api_key = os.environ.get("RAY_API_KEY")
base_url = "http://localhost:8000"
headers = {
"substrate-api-key": ray_api_key,
"x-user-id": user_id,
"x-proxy-path": "/compose",
}
substrate = Substrate(
api_key=api_key,
base_url=base_url,
backend="v1",
additional_headers=headers,
)
return base_url, headers, ray_api_key, substrate, user_id


@app.cell
def __(mo):
question = mo.ui.text(
placeholder="Question",
value="What is the 88th fibonacci number?",
full_width=True,
).form()
question
return question,


@app.cell
def __(GenerateText, RunCode, question):
prompt = f"""
{question.value}
Think step by step and run Python code to check your work or solve problems before returning an answer. Print the output. The Python runtime does not have network or filesystem access, but does include the entire standard library. Read input from stdin and write output to stdout. Remember to print the output in your code. Wrap the code in your response inside a <code></code> tag.
"""
text = GenerateText(
{
"prompt": prompt,
"node": "Llama3Instruct70B",
# "node": "Mistral7BInstruct",
}
)
get_code = RunCode(
{
"code": """import re
import sys
s = sys.argv[1]
match = re.search(r'<code>(.*?)</code>', s, re.DOTALL)
print(match.group(1) if match else "")
""",
"args": [text.future.text],
}
)
run_code = RunCode(
{
"code": get_code.future.output
}
)
return get_code, prompt, run_code, text


@app.cell
def __(get_code, run_code, substrate, text):
res = substrate.run(text, get_code, run_code)
viz = substrate.visualize(text, get_code, run_code)
print(viz)
# res = substrate.run(text)
return res, viz


@app.cell
def __(json, mo, res, run_code):
print(json.dumps(res.json, indent=2))
mo.md(res.get(run_code).output)
return


if __name__ == "__main__":
app.run()

0 comments on commit 4eeb246

Please sign in to comment.