Skip to content

Instantly share code, notes, and snippets.

@ledmaster
Created May 12, 2023 17:10
Show Gist options
  • Save ledmaster/3d73591fe9e4f5b72efdae7aac8f04e5 to your computer and use it in GitHub Desktop.
Save ledmaster/3d73591fe9e4f5b72efdae7aac8f04e5 to your computer and use it in GitHub Desktop.
No more "Untitled12-final-final.ipynb"! A python script that ingests Jupyter Notebook cells and comes up with 10 relevant names to give it.
# No more "Untitled12-final-final.ipynb"!
# A python script that ingests Jupyter Notebook cells and comes up with 10 relevant names to give it.
import nbformat
import openai
import argparse
def extract_cell_contents(notebook_path):
with open(notebook_path, "r", encoding="utf-8") as f:
notebook = nbformat.read(f, nbformat.NO_CONVERT)
cell_contents = []
for cell in notebook.cells:
if cell.cell_type == "code":
cell_contents.append(cell.source)
elif cell.cell_type == "markdown":
cell_contents.append(cell.source)
return cell_contents
def generate_text(prompt):
response = openai.ChatCompletion.create(
model='gpt-4',
messages=[{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}],
temperature=0.8,
)
return response
if __name__ == "__main__":
# take notebook path as argument
parser = argparse.ArgumentParser()
parser.add_argument("nb_path", help="path to the notebook file")
args = parser.parse_args()
# Set up your OpenAI API credentials
openai.api_key = 'OPENAI-API_KEY'
# extract cell contents
contents = extract_cell_contents(args.nb_path)
cells = "\n".join(contents)
prompt = f"I need you to list 10 ideas of relevant names for the Jupyter Notebook that contains the cells I enclosed by three backticks. The name must be specific and descriptive without being verbose. ```{cells}```"
# generate text
generated_text = generate_text(prompt)
# print the generated text
print(generated_text['choices'][0]['message']['content'])
@ledmaster
Copy link
Author

Demo

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment