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

Add utils to generate boilerplate #38

Open
neelnanda-io opened this issue Dec 24, 2022 · 0 comments
Open

Add utils to generate boilerplate #38

neelnanda-io opened this issue Dec 24, 2022 · 0 comments

Comments

@neelnanda-io
Copy link
Collaborator

When developing a new feature, there's an annoying amount of boiler plate I need to write - creating mock data in a json format, creating a stories.tsx file, writing the Props and function definition, and writing the Python function. I think this can mostly be automated, and makes it lower friction to add features. Eg adding a generate_stub_files function to utils. Here's an attempt:

    # Split the string into a list of words
    words = s.split('_')

    # Capitalize the first letter of each word and join them
    camel = ''.join([word.capitalize() for word in words])

    # Return the first letter in lowercase
    return camel[0].lower() + camel[1:]

def snake_to_pascal(s: str) -> str:
    # Split the string into a list of words
    words = s.split('_')

    # Capitalize the first letter of each word and join them
    pascal = ''.join([word.capitalize() for word in words])
    return pascal
# %%
# Copy and paste to mock file
mock_data = {
    "prompt": prompt,
    "top_k_log_probs": top_log_probs.tolist(),
    "top_k_tokens": top_tokens,
    "correct_tokens": correct_tokens,
    "correct_token_rank": correct_token_rank,
    "correct_token_log_prob": correct_token_log_prob.tolist(),
}
vis_name = "LogProbVis"

mock_types = {
    "prompt": "string[]",
    "top_k_log_probs": "number[][]",
    "top_k_tokens": "string[][]",
    "correct_tokens": "string[]",
    "correct_token_rank": "number[]",
    "correct_token_log_prob": "number[]",
}

print(mock_data)

# %%
s = []
for name in mock_data:
    data = mock_data[name]
    typ = mock_types[name]
    if isinstance(data, torch.Tensor):
        data = data.tolist()
    print(f"export const {snake_to_camel('mock_'+name)}: {typ} = {data};")
    print()
# %%
newline = "\n"
template = f"""import {{ ComponentStory, ComponentMeta }} from "@storybook/react";
import React from "react";
import {{ {", ".join(map(lambda name: snake_to_camel("mock_" + name), mock_data.keys()))} }} from "./mocks/{vis_name[0].lower() + vis_name[1:]}";
import { {vis_name} } from "./{vis_name}";

export default {{
  component: {vis_name}
}} as ComponentMeta<typeof {vis_name}>;

const Template: ComponentStory<typeof {vis_name}> = (args) => (
  <{vis_name} {{...args}} />
);

export const SmallModelExample = Template.bind({{}});
SmallModelExample.args = {{
  {f",{newline}  ".join([f"{snake_to_camel(name)}: {snake_to_camel('mock_'+name)}" for name in mock_data])}
}};
"""
print(template)

# %%
func_defn = f"""
export function {vis_name}({{
  {f",{newline}  ".join(map(snake_to_camel, mock_data.keys()))}
}}: {vis_name}Props) {{
"""

interface = f"""
export interface {vis_name}Props {{
{''';
  /**
   */
   '''.join([f"{snake_to_camel(name)}: {mock_types[name]}" for name in mock_types])}
}}
"""
print(func_defn)
print()
print()
print()
print(interface)```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant