Skip to content
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

Bug fixes and Imagen 3 support for scaled version #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ BackgroundR automatically creates new backgrounds for your images based on a tex

## Updates

- **December 2024.** Support for Imagen 3.
- **August 2024** Initial release.

## Getting Started
Expand Down
64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"node": ">=12"
},
"dependencies": {
"@angular/platform-browser": "^19.0.5",
"@google/clasp": "^2.4.2",
"@types/google-apps-script": "^1.0.69",
"@types/jest": "^29.5.5",
Expand Down
8 changes: 7 additions & 1 deletion src/drive-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export const listFiles = (folder: string) => {
const files: GoogleAppsScript.Drive.File[] = [];
const filesIterator = DriveApp.getFolderById(folder).getFiles();
while (filesIterator.hasNext()) {
files.push(filesIterator.next());
const file = filesIterator.next();
console.log(
`Reading file: ${file.getName()} with mime type: ${file.getMimeType()}`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the logging message?

);
if (['image/jpeg', 'image/png'].includes(file.getMimeType())) {
files.push(file);
}
}
return files;
};
Expand Down
46 changes: 24 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ImageQueue {
folderId: string;
outputFolderId: string;
fileName: string;
file: string;
fileId: string;
prompt: string;
variationId: number;
}
Expand Down Expand Up @@ -90,16 +90,13 @@ const getImagesToProcess = () => {
}
const outputFolderId = ensureFolderExists(folderId);
listFiles(folderId).forEach((file: GoogleAppsScript.Drive.File) => {
const fileBlob = file.getBlob();
const bytes = fileBlob.getBytes();
const base64Data = Utilities.base64Encode(bytes);
let variationId = 1;
prompts.forEach(prompt => {
imageQueue.push({
folderId,
outputFolderId,
fileName: file.getName(),
file: base64Data,
fileId: file.getId(),
prompt,
variationId,
});
Expand All @@ -116,27 +113,31 @@ const getImageAssets = (folderId: string) => {
}
IMAGE_SHEET.getDataRange().offset(HEADER_ROWS, 0).clearContent();

listFiles(folderId).forEach((file: GoogleAppsScript.Drive.File) => {
const fileBlob = file.getBlob();
const bytes = fileBlob.getBytes();
const base64Data = Utilities.base64Encode(bytes);
const dataUrl = `data:image/png;base64,${base64Data}`;
const cellImage = SpreadsheetApp.newCellImage()
.setSourceUrl(dataUrl)
.build();
const row = [cellImage, file.getId()];
const sheetRow = IMAGE_SHEET.getLastRow() + 1;
IMAGE_SHEET.getRange(sheetRow, 1, 1, row.length).setValues([row]);
IMAGE_SHEET.setRowHeight(sheetRow, 256);
IMAGE_SHEET.setColumnWidth(1, 256);
});
listFiles(folderId)
.filter((file: GoogleAppsScript.Drive.File) => file.getSize() < 10000000)
.slice(0, 10) // Sample 10 images under 10 Mb.
.forEach((file: GoogleAppsScript.Drive.File) => {
const fileBlob = file.getBlob();
const bytes = fileBlob.getBytes();
const base64Data = Utilities.base64Encode(bytes);
const dataUrl = `data:${file.getMimeType()};base64,${base64Data}`;
const cellImage = SpreadsheetApp.newCellImage()
.setSourceUrl(dataUrl)
.build();
const row = [cellImage, file.getId()];
const sheetRow = IMAGE_SHEET.getLastRow() + 1;
IMAGE_SHEET.getRange(sheetRow, 1, 1, row.length).setValues([row]);
IMAGE_SHEET.setRowHeight(sheetRow, 256);
IMAGE_SHEET.setColumnWidth(1, 256);
});
};

const processImageAssets = (
backgroundDefinitions: BackgroundDefinition[],
projectId: string,
region: string,
modelId: string
modelId: string,
backgroundRemoval: boolean
) => {
if (!IMAGE_SHEET) {
throw `Sheet 'Images' not found`;
Expand All @@ -146,7 +147,6 @@ const processImageAssets = (
region,
modelId
);
// TODO: Don't clear content; continue from where it left off on last execution
IMAGE_SHEET.getRange('B:B')
.offset(HEADER_ROWS, 0)
.getValues()
Expand All @@ -172,7 +172,9 @@ const processImageAssets = (
const result = predict(
`${e.description}`,
base64Data,
imageGenerationEndpoint
imageGenerationEndpoint,
modelId,
backgroundRemoval
);
return SpreadsheetApp.newCellImage()
.setSourceUrl(
Expand Down
4 changes: 4 additions & 0 deletions src/ui/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ <h1 style="text-align: center">BackgroundR</h1>
<mat-form-field>
<mat-label>Image Generation Model</mat-label>
<mat-select [(ngModel)]="modelId" [disabled]="isLoading">
<mat-option value="imagen-3.0-capability-001">Imagen 3</mat-option>
<mat-option value="imagegeneration@006"
>imagegeneration&#64;006</mat-option
>
</mat-select>
</mat-form-field>
<mat-checkbox [(ngModel)]="backgroundRemoval"
>Remove existing background</mat-checkbox
>
<mat-form-field>
<mat-label>Drive Folder ID</mat-label>
<input
Expand Down
Loading