Skip to content

Commit

Permalink
Merge branch 'main' into add-any-to-any
Browse files Browse the repository at this point in the history
  • Loading branch information
merveenoyan authored Sep 4, 2024
2 parents 3617fee + a9047d5 commit 3fed699
Show file tree
Hide file tree
Showing 63 changed files with 676 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/agents-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
pnpm --filter doc-internal run fix-cdn-versions
git add ../..
git commit -m "🔖 @hugginface/agents $BUMPED_VERSION"
git commit -m "🔖 @huggingface/agents $BUMPED_VERSION"
git tag "agents-v$BUMPED_VERSION"
- run: pnpm --filter agents... build && pnpm publish --no-git-checks .
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gguf-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
BUMPED_VERSION=$(node -p "require('semver').inc('$PACKAGE_VERSION', '${{ github.event.inputs.newversion }}')")
# Update package.json with the new version
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
git commit . -m "🔖 @hugginface/gguf $BUMPED_VERSION"
git commit . -m "🔖 @huggingface/gguf $BUMPED_VERSION"
git tag "gguf-v$BUMPED_VERSION"
- run: pnpm publish --no-git-checks .
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/hub-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
pnpm --filter doc-internal run fix-cdn-versions
git add ../..
git commit -m "🔖 @hugginface/hub $BUMPED_VERSION"
git commit -m "🔖 @huggingface/hub $BUMPED_VERSION"
git tag "hub-v$BUMPED_VERSION"
- name: Make sure that the latest version of @huggingface/tasks is consistent with the local version
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/inference-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
pnpm --filter doc-internal run fix-cdn-versions
git add ../..
git commit -m "🔖 @hugginface/inference $BUMPED_VERSION"
git commit -m "🔖 @huggingface/inference $BUMPED_VERSION"
git tag "inference-v$BUMPED_VERSION"
- name: Make sure that the latest version of @huggingface/tasks is consistent with the local version
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jinja-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
BUMPED_VERSION=$(node -p "require('semver').inc('$PACKAGE_VERSION', '${{ github.event.inputs.newversion }}')")
# Update package.json with the new version
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
git commit . -m "🔖 @hugginface/jinja $BUMPED_VERSION"
git commit . -m "🔖 @huggingface/jinja $BUMPED_VERSION"
git tag "jinja-v$BUMPED_VERSION"
- run: pnpm publish --no-git-checks .
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/languages-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
BUMPED_VERSION=$(node -p "require('semver').inc('$PACKAGE_VERSION', '${{ github.event.inputs.newversion }}')")
# Update package.json with the new version
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
git commit . -m "🔖 @hugginface/languages $BUMPED_VERSION"
git commit . -m "🔖 @huggingface/languages $BUMPED_VERSION"
git tag "languages-v$BUMPED_VERSION"
- run: pnpm publish --no-git-checks .
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/space-header-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
BUMPED_VERSION=$(node -p "require('semver').inc('$PACKAGE_VERSION', '${{ github.event.inputs.newversion }}')")
# Update package.json with the new version
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
git commit . -m "🔖 @hugginface/space-header $BUMPED_VERSION"
git commit . -m "🔖 @huggingface/space-header $BUMPED_VERSION"
git tag "space-header-v$BUMPED_VERSION"
- run: pnpm publish --no-git-checks .
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tasks-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
BUMPED_VERSION=$(node -p "require('semver').inc('$PACKAGE_VERSION', '${{ github.event.inputs.newversion }}')")
# Update package.json with the new version
node -e "const fs = require('fs'); const package = JSON.parse(fs.readFileSync('./package.json')); package.version = '$BUMPED_VERSION'; fs.writeFileSync('./package.json', JSON.stringify(package, null, '\t') + '\n');"
git commit . -m "🔖 @hugginface/tasks $BUMPED_VERSION"
git commit . -m "🔖 @huggingface/tasks $BUMPED_VERSION"
git tag "tasks-v$BUMPED_VERSION"
- run: pnpm publish --no-git-checks .
env:
Expand Down
2 changes: 1 addition & 1 deletion packages/doc-internal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This package generates `.md` files inside the [docs](../../docs) folder using [typedoc](https://typedoc.org/) and [typedoc-plugin-markdown](https://github.com/tgreyuk/typedoc-plugin-markdown).

The `.md` files are generated when releasing packages. They are then published to [hugginface.co](https://huggingface.co/docs/huggingface.js/index) through the [doc-builder](https://github.com/huggingface/doc-builder)'s github action.
The `.md` files are generated when releasing packages. They are then published to [huggingface.co](https://huggingface.co/docs/huggingface.js/index) through the [doc-builder](https://github.com/huggingface/doc-builder)'s github action.

We run a few scripts in between, [fix-md-links](./fix-md-links.ts) and [update-toc](./update-toc.ts) to preprocess the files for `doc-builder`.

Expand Down
7 changes: 7 additions & 0 deletions packages/jinja/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ export class BooleanLiteral extends Literal<boolean> {
override type = "BooleanLiteral";
}

/**
* Represents null (none) in the template.
*/
export class NullLiteral extends Literal<null> {
override type = "NullLiteral";
}

/**
* Represents an array literal in the template.
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/jinja/src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const TOKEN_TYPES = Object.freeze({

NumericLiteral: "NumericLiteral", // e.g., 123
BooleanLiteral: "BooleanLiteral", // true or false
NullLiteral: "NullLiteral", // none
StringLiteral: "StringLiteral", // 'string'
Identifier: "Identifier", // Variables, functions, etc.
Equals: "Equals", // =
Expand Down Expand Up @@ -73,13 +74,15 @@ const KEYWORDS = Object.freeze({
// Literals
true: TOKEN_TYPES.BooleanLiteral,
false: TOKEN_TYPES.BooleanLiteral,
none: TOKEN_TYPES.NullLiteral,

// NOTE: According to the Jinja docs: The special constants true, false, and none are indeed lowercase.
// Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false),
// all three can now also be written in title case (True, False, and None). However, for consistency, (all Jinja identifiers are lowercase)
// you should use the lowercase versions.
True: TOKEN_TYPES.BooleanLiteral,
False: TOKEN_TYPES.BooleanLiteral,
None: TOKEN_TYPES.NullLiteral,
});

/**
Expand Down Expand Up @@ -271,6 +274,7 @@ export function tokenize(source: string, options: PreprocessOptions = {}): Token
case TOKEN_TYPES.Identifier:
case TOKEN_TYPES.NumericLiteral:
case TOKEN_TYPES.BooleanLiteral:
case TOKEN_TYPES.NullLiteral:
case TOKEN_TYPES.StringLiteral:
case TOKEN_TYPES.CloseParen:
case TOKEN_TYPES.CloseSquareBracket:
Expand Down
6 changes: 6 additions & 0 deletions packages/jinja/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
NumericLiteral,
StringLiteral,
BooleanLiteral,
NullLiteral,
ArrayLiteral,
ObjectLiteral,
BinaryExpression,
Expand Down Expand Up @@ -486,6 +487,8 @@ export function parse(tokens: Token[]): Program {
if (filter instanceof BooleanLiteral) {
// Special case: treat boolean literals as identifiers
filter = new Identifier(filter.value.toString());
} else if (filter instanceof NullLiteral) {
filter = new Identifier("none");
}
if (!(filter instanceof Identifier)) {
throw new SyntaxError(`Expected identifier for the test`);
Expand Down Expand Up @@ -527,6 +530,9 @@ export function parse(tokens: Token[]): Program {
case TOKEN_TYPES.BooleanLiteral:
++current;
return new BooleanLiteral(token.value.toLowerCase() === "true");
case TOKEN_TYPES.NullLiteral:
++current;
return new NullLiteral(null);
case TOKEN_TYPES.Identifier:
++current;
return new Identifier(token.value);
Expand Down
16 changes: 16 additions & 0 deletions packages/jinja/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
NumericLiteral,
StringLiteral,
BooleanLiteral,
NullLiteral,
ArrayLiteral,
Statement,
Program,
Expand Down Expand Up @@ -104,6 +105,18 @@ export class StringValue extends RuntimeValue<string> {
}),
],
["length", new NumericValue(this.value.length)],
[
"rstrip",
new FunctionValue(() => {
return new StringValue(this.value.trimEnd());
}),
],
[
"lstrip",
new FunctionValue(() => {
return new StringValue(this.value.trimStart());
}),
],
]);
}

Expand Down Expand Up @@ -257,6 +270,7 @@ export class Environment {
],
["false", (operand) => operand.type === "BooleanValue" && !(operand as BooleanValue).value],
["true", (operand) => operand.type === "BooleanValue" && (operand as BooleanValue).value],
["none", (operand) => operand.type === "NullValue"],
["string", (operand) => operand.type === "StringValue"],
["number", (operand) => operand.type === "NumericValue"],
["integer", (operand) => operand.type === "NumericValue" && Number.isInteger((operand as NumericValue).value)],
Expand Down Expand Up @@ -1039,6 +1053,8 @@ export class Interpreter {
return new StringValue((statement as StringLiteral).value);
case "BooleanLiteral":
return new BooleanValue((statement as BooleanLiteral).value);
case "NullLiteral":
return new NullValue((statement as NullLiteral).value);
case "ArrayLiteral":
return new ArrayValue((statement as ArrayLiteral).value.map((x) => this.evaluate(x, environment)));
case "TupleLiteral":
Expand Down
82 changes: 81 additions & 1 deletion packages/jinja/test/templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ const TEST_STRINGS = {
UNDEFINED_VARIABLES: `{{ undefined_variable }}`,
UNDEFINED_ACCESS: `{{ object.undefined_attribute }}`,

// Null
NULL_VARIABLE: `{% if not null_val is defined %}{% set null_val = none %}{% endif %}{% if null_val is not none %}{{ 'fail' }}{% else %}{{ 'pass' }}{% endif %}`,

// Ternary operator
TERNARY_OPERATOR: `|{{ 'a' if true else 'b' }}|{{ 'a' if false else 'b' }}|{{ 'a' if 1 + 1 == 2 else 'b' }}|{{ 'a' if 1 + 1 == 3 or 1 * 2 == 3 else 'b' }}|`,

Expand All @@ -143,6 +146,11 @@ const TEST_STRINGS = {
MACROS: `{% macro hello(name) %}{{ 'Hello ' + name }}{% endmacro %}|{{ hello('Bob') }}|{{ hello('Alice') }}|`,
MACROS_1: `{% macro hello(name, suffix='.') %}{{ 'Hello ' + name + suffix }}{% endmacro %}|{{ hello('A') }}|{{ hello('B', '!') }}|{{ hello('C', suffix='?') }}|`,
MACROS_2: `{% macro fn(x, y=2, z=3) %}{{ x + ',' + y + ',' + z }}{% endmacro %}|{{ fn(1) }}|{{ fn(1, 0) }}|{{ fn(1, 0, -1) }}|{{ fn(1, y=0, z=-1) }}|{{ fn(1, z=0) }}|`,

//rstrip
RSTRIP: `{{ " test it ".rstrip() }}`,
//lstrip
LSTRIP: `{{ " test it ".lstrip() }}`,
};

const TEST_PARSED = {
Expand Down Expand Up @@ -2210,7 +2218,7 @@ const TEST_PARSED = {
{ value: "unknown", type: "StringLiteral" },
{ value: ")", type: "CloseParen" },
{ value: "is", type: "Is" },
{ value: "none", type: "Identifier" },
{ value: "none", type: "NullLiteral" },
{ value: "}}", type: "CloseExpression" },
{ value: "|", type: "Text" },
{ value: "{{", type: "OpenExpression" },
Expand Down Expand Up @@ -2355,6 +2363,45 @@ const TEST_PARSED = {
{ value: "}}", type: "CloseExpression" },
],

// Null
NULL_VARIABLE: [
{ value: "{%", type: "OpenStatement" },
{ value: "if", type: "If" },
{ value: "not", type: "UnaryOperator" },
{ value: "null_val", type: "Identifier" },
{ value: "is", type: "Is" },
{ value: "defined", type: "Identifier" },
{ value: "%}", type: "CloseStatement" },
{ value: "{%", type: "OpenStatement" },
{ value: "set", type: "Set" },
{ value: "null_val", type: "Identifier" },
{ value: "=", type: "Equals" },
{ value: "none", type: "NullLiteral" },
{ value: "%}", type: "CloseStatement" },
{ value: "{%", type: "OpenStatement" },
{ value: "endif", type: "EndIf" },
{ value: "%}", type: "CloseStatement" },
{ value: "{%", type: "OpenStatement" },
{ value: "if", type: "If" },
{ value: "null_val", type: "Identifier" },
{ value: "is", type: "Is" },
{ value: "not", type: "UnaryOperator" },
{ value: "none", type: "NullLiteral" },
{ value: "%}", type: "CloseStatement" },
{ value: "{{", type: "OpenExpression" },
{ value: "fail", type: "StringLiteral" },
{ value: "}}", type: "CloseExpression" },
{ value: "{%", type: "OpenStatement" },
{ value: "else", type: "Else" },
{ value: "%}", type: "CloseStatement" },
{ value: "{{", type: "OpenExpression" },
{ value: "pass", type: "StringLiteral" },
{ value: "}}", type: "CloseExpression" },
{ value: "{%", type: "OpenStatement" },
{ value: "endif", type: "EndIf" },
{ value: "%}", type: "CloseStatement" },
],

// Ternary operator
TERNARY_OPERATOR: [
{ value: "|", type: "Text" },
Expand Down Expand Up @@ -2674,6 +2721,25 @@ const TEST_PARSED = {
{ value: "}}", type: "CloseExpression" },
{ value: "|", type: "Text" },
],

RSTRIP: [
{ value: "{{", type: "OpenExpression" },
{ value: " test it ", type: "StringLiteral" },
{ value: ".", type: "Dot" },
{ value: "rstrip", type: "Identifier" },
{ value: "(", type: "OpenParen" },
{ value: ")", type: "CloseParen" },
{ value: "}}", type: "CloseExpression" },
],
LSTRIP: [
{ value: "{{", type: "OpenExpression" },
{ value: " test it ", type: "StringLiteral" },
{ value: ".", type: "Dot" },
{ value: "lstrip", type: "Identifier" },
{ value: "(", type: "OpenParen" },
{ value: ")", type: "CloseParen" },
{ value: "}}", type: "CloseExpression" },
],
};

const TEST_CONTEXT = {
Expand Down Expand Up @@ -2894,6 +2960,9 @@ const TEST_CONTEXT = {
UNDEFINED_VARIABLES: {},
UNDEFINED_ACCESS: { object: {} },

// Null
NULL_VARIABLE: { a: null },

// Ternary operator
TERNARY_OPERATOR: {},

Expand All @@ -2915,6 +2984,10 @@ const TEST_CONTEXT = {
MACROS: {},
MACROS_1: {},
MACROS_2: {},

//STRIP
RSTRIP: {},
LSTRIP: {},
};

const EXPECTED_OUTPUTS = {
Expand Down Expand Up @@ -3037,6 +3110,9 @@ const EXPECTED_OUTPUTS = {
UNDEFINED_VARIABLES: ``,
UNDEFINED_ACCESS: ``,

// Null
NULL_VARIABLE: `pass`,

// Ternary operator
TERNARY_OPERATOR: `|a|b|a|b|`,

Expand All @@ -3056,6 +3132,10 @@ const EXPECTED_OUTPUTS = {
MACROS: `|Hello Bob|Hello Alice|`,
MACROS_1: `|Hello A.|Hello B!|Hello C?|`,
MACROS_2: `|1,2,3|1,0,3|1,0,-1|1,0,-1|1,2,0|`,

// RSTRIP/LSTRIP
RSTRIP: ` test it`,
LSTRIP: `test it `,
};

describe("Templates", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/space-header/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@huggingface/space-header",
"version": "1.0.3",
"version": "1.0.4",
"packageManager": "[email protected]",
"description": "Use the Space mini_header outside Hugging Face",
"repository": "https://github.com/huggingface/huggingface.js.git",
Expand Down
6 changes: 4 additions & 2 deletions packages/space-header/src/header/components/content/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const Avatar = (username: string): HTMLImageElement => {
export const Avatar = (username: string, type: "user" | "org" = "user"): HTMLImageElement => {
const route = type === "user" ? "users" : "organizations";

const element = document.createElement("img");
element.src = `https://huggingface.co/api/users/${username}/avatar`;
element.src = `https://huggingface.co/api/${route}/${username}/avatar`;

element.style.width = "0.875rem";
element.style.height = "0.875rem";
Expand Down
4 changes: 3 additions & 1 deletion packages/space-header/src/header/components/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const Content = (space: Space): HTMLDivElement => {
content.style.paddingRight = "12px";
content.style.height = "40px";

content.appendChild(Avatar(space.author));
if (space.type !== "unknown") {
content.appendChild(Avatar(space.author, space.type));
}
content.appendChild(Username(space.author));
content.appendChild(Separation());
content.appendChild(Namespace(space.id));
Expand Down
6 changes: 5 additions & 1 deletion packages/space-header/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { Options, Space, Header } from "./type";
import { inject_fonts } from "./inject_fonts";

import { create } from "./header/create";
import { get_space } from "./get_space";
import { check_avatar } from "./utils/check_avatar";
import { get_space } from "./utils/get_space";
import { inject } from "./inject";

async function main(initialSpace: string | Space, options?: Options) {
Expand All @@ -27,6 +28,9 @@ async function main(initialSpace: string | Space, options?: Options) {
space = initialSpace;
}

const [user, org] = await Promise.all([check_avatar(space.author, "user"), check_avatar(space.author, "org")]);
space.type = user ? "user" : org ? "org" : "unknown";

const mini_header_element = create(space as Space);
inject(mini_header_element, options);

Expand Down
Loading

0 comments on commit 3fed699

Please sign in to comment.