Spaces:
Sleeping
Sleeping
initial commit
Browse files- .gitignore +174 -0
- notebook_enhancer.py +122 -0
- requirements.txt +29 -0
.gitignore
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# UV
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
#uv.lock
|
102 |
+
|
103 |
+
# poetry
|
104 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
105 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
106 |
+
# commonly ignored for libraries.
|
107 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
108 |
+
#poetry.lock
|
109 |
+
|
110 |
+
# pdm
|
111 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
112 |
+
#pdm.lock
|
113 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
114 |
+
# in version control.
|
115 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
116 |
+
.pdm.toml
|
117 |
+
.pdm-python
|
118 |
+
.pdm-build/
|
119 |
+
|
120 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
121 |
+
__pypackages__/
|
122 |
+
|
123 |
+
# Celery stuff
|
124 |
+
celerybeat-schedule
|
125 |
+
celerybeat.pid
|
126 |
+
|
127 |
+
# SageMath parsed files
|
128 |
+
*.sage.py
|
129 |
+
|
130 |
+
# Environments
|
131 |
+
.env
|
132 |
+
.venv
|
133 |
+
env/
|
134 |
+
venv/
|
135 |
+
ENV/
|
136 |
+
env.bak/
|
137 |
+
venv.bak/
|
138 |
+
|
139 |
+
# Spyder project settings
|
140 |
+
.spyderproject
|
141 |
+
.spyproject
|
142 |
+
|
143 |
+
# Rope project settings
|
144 |
+
.ropeproject
|
145 |
+
|
146 |
+
# mkdocs documentation
|
147 |
+
/site
|
148 |
+
|
149 |
+
# mypy
|
150 |
+
.mypy_cache/
|
151 |
+
.dmypy.json
|
152 |
+
dmypy.json
|
153 |
+
|
154 |
+
# Pyre type checker
|
155 |
+
.pyre/
|
156 |
+
|
157 |
+
# pytype static type analyzer
|
158 |
+
.pytype/
|
159 |
+
|
160 |
+
# Cython debug symbols
|
161 |
+
cython_debug/
|
162 |
+
|
163 |
+
# PyCharm
|
164 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
165 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
166 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
167 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
168 |
+
#.idea/
|
169 |
+
|
170 |
+
# Ruff stuff:
|
171 |
+
.ruff_cache/
|
172 |
+
|
173 |
+
# PyPI configuration file
|
174 |
+
.pypirc
|
notebook_enhancer.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import nbformat
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
|
6 |
+
class NotebookEnhancer:
|
7 |
+
def __init__(self):
|
8 |
+
# Initialize Hugging Face models
|
9 |
+
self.title_generator = pipeline(
|
10 |
+
"summarization", model="facebook/bart-large-cnn"
|
11 |
+
)
|
12 |
+
self.summary_generator = pipeline(
|
13 |
+
"summarization", model="sshleifer/distilbart-cnn-12-6"
|
14 |
+
)
|
15 |
+
|
16 |
+
def generate_title(self, code):
|
17 |
+
"""Generate a concise title for a code cell"""
|
18 |
+
# Limit input length to match model constraints
|
19 |
+
max_length = 1024
|
20 |
+
truncated_code = code[:max_length] if len(code) > max_length else code
|
21 |
+
|
22 |
+
result = self.title_generator(
|
23 |
+
truncated_code, max_length=10, min_length=3, do_sample=False
|
24 |
+
)
|
25 |
+
title = result[0]["summary_text"].strip()
|
26 |
+
# Format as a markdown title
|
27 |
+
return f"## {title.capitalize()}"
|
28 |
+
|
29 |
+
def generate_summary(self, code):
|
30 |
+
"""Generate a detailed summary for a code cell"""
|
31 |
+
# Limit input length to match model constraints
|
32 |
+
max_length = 1024
|
33 |
+
truncated_code = code[:max_length] if len(code) > max_length else code
|
34 |
+
|
35 |
+
result = self.summary_generator(
|
36 |
+
truncated_code, max_length=100, min_length=30, do_sample=True
|
37 |
+
)
|
38 |
+
return result[0]["summary_text"].strip()
|
39 |
+
|
40 |
+
def enhance_notebook(self, notebook_content):
|
41 |
+
"""Add title and summary markdown cells before each code cell"""
|
42 |
+
# Load the notebook
|
43 |
+
notebook = nbformat.reads(notebook_content, as_version=4)
|
44 |
+
|
45 |
+
# Create a new notebook
|
46 |
+
enhanced_notebook = nbformat.v4.new_notebook()
|
47 |
+
enhanced_notebook.metadata = notebook.metadata
|
48 |
+
|
49 |
+
# Process each cell
|
50 |
+
i = 0
|
51 |
+
while i < len(notebook.cells):
|
52 |
+
cell = notebook.cells[i]
|
53 |
+
|
54 |
+
# For code cells, add title and summary markdown cells
|
55 |
+
if cell.cell_type == "code" and cell.source.strip():
|
56 |
+
# Generate title
|
57 |
+
title = self.generate_title(cell.source)
|
58 |
+
title_cell = nbformat.v4.new_markdown_cell(title)
|
59 |
+
enhanced_notebook.cells.append(title_cell)
|
60 |
+
|
61 |
+
# Generate summary
|
62 |
+
summary = self.generate_summary(cell.source)
|
63 |
+
summary_cell = nbformat.v4.new_markdown_cell(summary)
|
64 |
+
enhanced_notebook.cells.append(summary_cell)
|
65 |
+
|
66 |
+
# Add the original cell
|
67 |
+
enhanced_notebook.cells.append(cell)
|
68 |
+
i += 1
|
69 |
+
|
70 |
+
# Convert back to string
|
71 |
+
return nbformat.writes(enhanced_notebook)
|
72 |
+
|
73 |
+
|
74 |
+
def process_notebook(file):
|
75 |
+
"""Process an uploaded notebook file"""
|
76 |
+
enhancer = NotebookEnhancer()
|
77 |
+
|
78 |
+
# Read uploaded file
|
79 |
+
notebook_content = file.decode("utf-8")
|
80 |
+
|
81 |
+
# Process the notebook
|
82 |
+
enhanced_notebook = enhancer.enhance_notebook(notebook_content)
|
83 |
+
|
84 |
+
# Save to temp file
|
85 |
+
output_path = "enhanced_notebook.ipynb"
|
86 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
87 |
+
f.write(enhanced_notebook)
|
88 |
+
|
89 |
+
return output_path
|
90 |
+
|
91 |
+
|
92 |
+
def build_gradio_interface():
|
93 |
+
"""Create and launch the Gradio interface"""
|
94 |
+
with gr.Blocks(title="Notebook Enhancer") as demo:
|
95 |
+
gr.Markdown("# Jupyter Notebook Enhancer")
|
96 |
+
gr.Markdown(
|
97 |
+
"""
|
98 |
+
Upload a Jupyter notebook to enhance it with automatically generated titles and summaries for each code cell.
|
99 |
+
|
100 |
+
This tool uses Hugging Face models to:
|
101 |
+
1. Generate concise titles for code cells
|
102 |
+
2. Create explanatory summaries of what the code does
|
103 |
+
"""
|
104 |
+
)
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
with gr.Column():
|
108 |
+
file_input = gr.File(label="Upload Jupyter Notebook (.ipynb)")
|
109 |
+
process_btn = gr.Button("Enhance Notebook")
|
110 |
+
|
111 |
+
with gr.Column():
|
112 |
+
output = gr.File(label="Enhanced Notebook")
|
113 |
+
|
114 |
+
process_btn.click(fn=process_notebook, inputs=file_input, outputs=output)
|
115 |
+
|
116 |
+
return demo
|
117 |
+
|
118 |
+
|
119 |
+
# This will be the entry point when running the script
|
120 |
+
if __name__ == "__main__":
|
121 |
+
demo = build_gradio_interface()
|
122 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Core dependencies
|
2 |
+
nbformat>=5.1.3
|
3 |
+
gradio>=3.32.0
|
4 |
+
transformers>=4.26.0
|
5 |
+
torch>=1.13.1
|
6 |
+
accelerate>=0.16.0
|
7 |
+
|
8 |
+
# NLP model dependencies
|
9 |
+
sentencepiece>=0.1.97
|
10 |
+
protobuf>=3.20.0
|
11 |
+
|
12 |
+
# Notebook dependencies
|
13 |
+
jupyterlab>=3.5.0
|
14 |
+
ipykernel>=6.21.0
|
15 |
+
|
16 |
+
# Visualization (for example notebooks)
|
17 |
+
matplotlib>=3.6.0
|
18 |
+
seaborn>=0.12.0
|
19 |
+
pandas>=1.5.0
|
20 |
+
numpy>=1.23.0
|
21 |
+
|
22 |
+
# ML components (for example notebooks)
|
23 |
+
scikit-learn>=1.2.0
|
24 |
+
|
25 |
+
# Development tools
|
26 |
+
pytest>=7.2.0
|
27 |
+
black>=23.1.0
|
28 |
+
flake8>=6.0.0
|
29 |
+
isort>=5.12.0
|