-
Notifications
You must be signed in to change notification settings - Fork 53
Implements Pillow image generation example #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| image_bytes = buffer.getvalue() | ||
|
|
||
| # Create and return response with appropriate headers | ||
| return Response( | ||
| to_js(image_bytes).buffer, | ||
| headers={ | ||
| "Content-Type": content_type, | ||
| "Cache-Control": "public, max-age=3600", | ||
| }, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One copy instead of two:
| image_bytes = buffer.getvalue() | |
| # Create and return response with appropriate headers | |
| return Response( | |
| to_js(image_bytes).buffer, | |
| headers={ | |
| "Content-Type": content_type, | |
| "Cache-Control": "public, max-age=3600", | |
| }, | |
| ) | |
| image_bytes = create_proxy(buffer.getvalue()) | |
| jsbuffer = image_bytes.getBuffer() | |
| try: | |
| # Create and return response with appropriate headers | |
| return Response( | |
| jsbuffer.data, | |
| headers={ | |
| "Content-Type": content_type, | |
| "Cache-Control": "public, max-age=3600", | |
| }, | |
| ) | |
| finally: | |
| image_bytes.destroy() | |
| jsbuffer.release() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is presumably that the Response constructor is somehow messing up array buffer views. We should figure out where that is messed up and fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @ryanking13 this might be a good issue for you to investigate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it looks like it doesn't take Uint8Array type as a Response. The error log that I get in the console is
TypeError: Unsupported type in Response: Uint8Array
Raw ArrayBuffer seems to work fine, but it doesn't seem to handle ArrayBufferView.
I was able to make it work with the following workaround but it involves copying in slice anyways.
image_bytes = create_proxy(buffer.getvalue())
jsbuffer = image_bytes.getBuffer()
# ...
return Response(
jsbuffer.data.slice(0, jsbuffer.data.byteLength).buffer,
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we need a fix to JavaScript Response:
const a = new TextEncoder().encode("hello there!!")
const b = a.subarray(3)
const r = new Response(b)
console.log(await r.text()); // 'lo there!!'There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay nevermind that clearly works.
12-image-gen/src/entry.py
Outdated
| Return an HTML page showing available endpoints and examples. | ||
| """ | ||
| html = """ | ||
| <!DOCTYPE html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we move this into it's own .html file and do
endpoints = Path(__file__).parent / "endpoints.html"
return Response(endpoints.read_text(), headers={"Content-Type": "text/html"})There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this is a great example too that we should put in our docs somewhere
08c0160 to
eb78296
Compare
1188ea0 to
2a53cd0
Compare
d81e4e9 to
873157c
Compare
db3ed55 to
935df31
Compare
for more information, see https://pre-commit.ci
hoodmane
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be good to improve this when we figure out what's going on with the Reponse constructor but this is good for now.
| image_bytes = buffer.getvalue() | ||
|
|
||
| # Create and return response with appropriate headers | ||
| return Response( | ||
| to_js(image_bytes).buffer, | ||
| headers={ | ||
| "Content-Type": content_type, | ||
| "Cache-Control": "public, max-age=3600", | ||
| }, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay nevermind that clearly works.

Deployed URL: https://python-image-gen.runtime-playground.workers.dev
Mostly generated using GenAI.
CC @ryanking13