Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 006fba9

Browse files
authored
Make ICortex work on Google Colab, doc improvements (#29)
* Improve history format * Wip * Improved docs, config init * Updated dependencies to work with Python 3.7 (used by google colab) * Undid output capturing for now
1 parent 71ef381 commit 006fba9

15 files changed

Lines changed: 350 additions & 287 deletions

File tree

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@
1515
</p>
1616
<hr />
1717

18-
ICortex is a [Jupyter kernel](https://jupyter-client.readthedocs.io/en/latest/kernels.html) that lets you develop **soft programs**:
18+
tl;dr in goes English, out comes Python:
1919

20-
- sets of instructions (i.e. prompts) [written in natural language](https://en.wikipedia.org/wiki/Natural-language_programming) (such as English)
21-
- processed by language models that generate Python code
22-
- to perform useful work in various contexts
23-
- more flexibly than regular software.
20+
https://user-images.githubusercontent.com/2453968/199964302-0dbe1d7d-81c9-4244-a9f2-9d959775e471.mp4
2421

25-
To put it simply—in goes English, out comes Python:
22+
ICortex enables you to develop **soft programs**:
2623

27-
https://user-images.githubusercontent.com/2453968/199964302-0dbe1d7d-81c9-4244-a9f2-9d959775e471.mp4
24+
> *Soft program:* a set of instructions (i.e. prompts) [written in natural language](https://en.wikipedia.org/wiki/Natural-language_programming) (e.g. English), processed by a language model that generates code at a lower layer of abstraction (e.g. Python), to perform work more flexibly than regular software.
25+
26+
In other words, ICortex is a **natural language programming** (NLP) framework that enables you to write code in English, and then run it in Python. It aims to make programming more accessible to non-programmers.
2827

29-
ICortex is ...
28+
ICortex is designed to be …
3029

31-
- a drop-in replacement for the IPython kernel. Prompts can be executed with the [magic commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html) `%prompt` or `%p` for short.
32-
- interactive—install missing packages directly, decide whether to execute the generated code or not, and so on, directly in the Jupyter Notebook cell.
33-
- open source and fully extensible—if you think we are missing a model or an API, you can request it by creating an issue, or implement it yourself by subclassing `ServiceBase` under [`icortex/services`](icortex/services).
30+
- a drop-in replacement for the [IPython kernel](https://ipython.org/). Prompts are executed via [magic commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html) such as `%prompt`.
31+
- interactive—automatically install missing packages, decide whether to execute the generated code or not, and so on, directly in the Jupyter Notebook cell.
32+
- open source and fully extensible—ICortex introduces a [domain-specific language](https://en.wikipedia.org/wiki/Domain-specific_language) for orchestrating various code generation services. If you think we are missing a model or an API, you can request it by creating an issue, or implement it yourself by subclassing `ServiceBase` under [`icortex/services`](icortex/services).
3433

35-
It is similar to [Github Copilot](https://github.com/features/copilot) but with certain differences that make it stand out:
34+
ICortex is similar to [Github Copilot](https://github.com/features/copilot) but with certain differences that make it stand out:
3635

3736
| Feature | GitHub Copilot | ICortex |
3837
|---|:---:|:---:|
@@ -48,6 +47,8 @@ ICortex is currently in alpha, so expect breaking changes. We are giving free cr
4847

4948
## Installation
5049

50+
### Locally
51+
5152
Install directly from PyPI:
5253

5354
```sh
@@ -56,6 +57,15 @@ pip install icortex
5657
python -m icortex.kernel.install
5758
```
5859

60+
### On Google Colab
61+
62+
[Google Colab](https://colab.research.google.com/) is a restricted computing environment that does not allow installing new Jupyter kernels. However, you can still use ICortex by running the following code in a Colab notebook:
63+
64+
```
65+
!pip install icortex
66+
import icortex.init
67+
```
68+
5969
## Quickstart
6070

6171
[Click here to visit the docs and get started using ICortex](https://icortex.readthedocs.io/en/latest/quickstart.html).

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"sphinx.ext.coverage",
2626
"sphinx.ext.napoleon",
2727
"sphinx_copybutton",
28+
"sphinx.ext.autosectionlabel",
2829
]
2930

3031
templates_path = ["_templates"]

docs/source/index.rst

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,27 @@
66
ICortex
77
=======
88

9+
tl;dr in goes English, out comes Python:
910

10-
ICortex is a `Jupyter
11-
kernel <https://jupyter-client.readthedocs.io/en/latest/kernels.html>`__
12-
that lets you develop **soft programs**:
11+
.. video:: https://user-images.githubusercontent.com/2453968/199964302-0dbe1d7d-81c9-4244-a9f2-9d959775e471.mp4
12+
:width: 100%
1313

14-
- sets of instructions (i.e. prompts) `written in natural
15-
language <https://en.wikipedia.org/wiki/Natural-language_programming>`__
16-
(such as English)
17-
- processed by language models that generate Python code
18-
- to perform useful work in various contexts
19-
- more flexibly than regular software.
14+
ICortex enables you to develop **soft programs**:
2015

21-
To put it simply—in goes English, out comes Python:
16+
*Soft program:* a set of instructions (i.e. prompts) `written in natural language <https://en.wikipedia.org/wiki/Natural-language_programming>`__ (e.g. English), processed by a language model that generates code at a lower layer of abstraction (e.g. Python), to perform work more flexibly than regular software.
2217

23-
.. video:: https://user-images.githubusercontent.com/2453968/199964302-0dbe1d7d-81c9-4244-a9f2-9d959775e471.mp4
24-
:width: 100%
18+
In other words, ICortex is a **natural language programming** (NLP) framework that enables you to write code in English, and then run it in Python. It aims to make programming more accessible to non-programmers.
2519

26-
ICortex is …
20+
ICortex is designed to be
2721

28-
- a drop-in replacement for the IPython kernel. Prompts can be executed
29-
with the `magic
22+
- a drop-in replacement for the `IPython kernel <https://ipython.org/>`__. Prompts can be executed
23+
via `magic
3024
commands <https://ipython.readthedocs.io/en/stable/interactive/magics.html>`__
31-
``%prompt`` or ``%p`` for short.
32-
- interactive—install missing packages directly, decide whether to
25+
such as ``%prompt``.
26+
- interactive—automatically install missing packages, decide whether to
3327
execute the generated code or not, and so on, directly in the Jupyter
3428
Notebook cell.
35-
- open source and fully extensible—if you think we are missing a model
36-
or an API, you can request it by creating an issue, or implement it
37-
yourself by subclassing ``ServiceBase`` under
38-
`icortex/services <https://github.com/textcortex/icortex/tree/main/icortex/services>`__.
29+
- open source and fully extensible—ICortex introduces a `domain-specific language <https://en.wikipedia.org/wiki/Domain-specific_language>`__ for orchestrating various code generation services. If you think we are missing a model or an API, you can request it by creating an issue, or implement it yourself by subclassing `ServiceBase` under `icortex/services <https://github.com/textcortex/icortex/tree/main/icortex/services>`__.
3930

4031
Get started
4132
-----------

docs/source/quickstart.rst

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ Quickstart
55
Installation
66
------------
77

8-
Install ICortex from PyPI, along with JupyterLab:
8+
Locally
9+
~~~~~~~
10+
11+
Install ICortex on your local machine, along with JupyterLab:
912

1013
.. code:: sh
1114
@@ -18,15 +21,37 @@ The second line is needed to install the kernel spec into Jupyter, otherwise, yo
1821
1922
jupyter kernelspec list
2023
21-
ICortex should be visible in the list of available kernels.
24+
ICortex needs to be visible in the list of available kernels.
25+
26+
On Google Colab
27+
~~~~~~~~~~~~~~~
28+
29+
`Google Colab <https://colab.research.google.com/>`__ is a restricted computing environment that does not allow installing new Jupyter kernels. However, you can still use ICortex by running the following code in a Colab notebook:
30+
31+
.. code:: text
32+
33+
!pip install icortex
34+
import icortex.init
2235
2336
Using ICortex
2437
-------------
2538

39+
Create a TextCortex account
40+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
41+
42+
.. tip::
43+
**This does not require you to enter any payment information.** Your account will receive free credits to try out the service. If you already have an account, you can skip this step.
44+
45+
46+
You need a TextCortex account to connect to TextCortex's code generation API. If you are planning to use a different code generation service, you can skip to :ref:`Start JupyterLab`.
47+
48+
|signup_link|
49+
50+
Once you have an account, proceed to the next section.
51+
2652
Start JupyterLab
2753
~~~~~~~~~~~~~~~~
2854

29-
3055
Create a directory for your new project, and start JupyterLab:
3156

3257
::
@@ -35,7 +60,7 @@ Create a directory for your new project, and start JupyterLab:
3560
cd new-icortex-project/
3661
jupyter lab
3762

38-
Once JupyterLab is up and running, create a new notebook that uses ICortex.
63+
Once JupyterLab is up and running, create a new notebook with ICortex kernel. If you are using Google Colab, you can skip this step.
3964

4065
.. important::
4166
If you don't see ICortex in the list of available kernels, you may have skipped kernel installation above—run ``python -m icortex.kernel.install`` and restart JupyterLab. If you still don't see ICortex there, `create a new installation issue on GitHub <https://github.com/textcortex/icortex/issues/new>`__.
@@ -60,11 +85,11 @@ After running ``%icortex init`` you should see the following message:
6085
::
6186

6287
Which code generation service would you like to use?
63-
Variables: textcortex, huggingface, openai
64-
Default [textcortex]
88+
Available services: textcortex, huggingface, openai
89+
Default: textcortex (Press enter to select the default option)
6590

6691

67-
ICortex supports different code generation services such as the TextCortex API, OpenAI Codex API, local HuggingFace transformers, and so on. We recommend that you start with TextCortex. It is already selected by default—**press Enter once** to move on to the next step.
92+
ICortex supports different code generation services. We recommend that you start with TextCortex. It is selected by default—**press Enter once** to move on to the next step.
6893

6994
In the next step, the dialog will ask whether to use the default parameters for TextCortex's code generation service:
7095

@@ -80,18 +105,6 @@ In the next step, the dialog will ask whether to use the default parameters for
80105
https://app.textcortex.com/user/dashboard/settings/api-key)
81106
82107
83-
Do not type anything yet, and proceed to the next section.
84-
85-
Create a TextCortex account
86-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
87-
88-
You need a TextCortex account to connect to TextCortex's code generation API.
89-
90-
|signup_link|.
91-
92-
.. note::
93-
This does not require you to enter any payment information and your account will receive free credits to try out the service. If you already have an account, you can skip this step.
94-
95108
Next, |api_key_link|.
96109

97110
Copy your API key from the dashboard, go back to the Jupyter notebook where you initially ran ``%icortex init``, and paste it in the dialog where it was asked for. Press Enter to continue.
@@ -192,7 +205,7 @@ ICortex printed the code generated by the API and is now asking whether it shoul
192205
193206
.. |signup_link| raw:: html
194207

195-
<a href="https://app.textcortex.com/user/signup" target="_blank">Click here sign up on the website</a>
208+
<blockquote><div style="text-align: center"><p><a href="https://app.textcortex.com/user/signup?registration_source=icortex" target="_blank">Click here sign up at TextCortex</a></p></div></blockquote>
196209

197210
.. |api_key_link| raw:: html
198211

icortex/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from icortex.kernel import ICortexKernel, print_service_help, get_icortex_kernel
1+
from icortex.kernel import ICortexKernel, print_service_help, get_icortex
22

33
import icortex.services
4-
import importlib.metadata
4+
import importlib_metadata
55

6-
__version__ = importlib.metadata.version("icortex")
6+
__version__ = importlib_metadata.version("icortex")

icortex/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ def main(argv=None, prog=None, kernel=None):
184184
elif args.command == "help":
185185
parser.print_help()
186186
elif args.command == "shell" or args.command is None:
187-
from icortex.kernel import get_icortex_kernel
187+
from icortex.kernel import get_icortex
188188
from icortex.kernel.app import ZMQTerminalICortexApp
189189

190-
kernel = get_icortex_kernel()
190+
kernel = get_icortex()
191191
if kernel is None:
192192
ZMQTerminalICortexApp.launch_instance()
193193
else:

icortex/config.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def set_service_var(self, var_name: str, var_value) -> bool:
5050
success = self.write_config()
5151
if success:
5252
print(f"Set variable {var_name} to {cast_value}.")
53-
# kernel = get_icortex_kernel()
53+
# kernel = get_icortex()
5454
if self.kernel is not None:
5555
self.set_service()
5656
return True
@@ -78,7 +78,7 @@ def set_service_config(self, service_name: str, hard_init=False) -> bool:
7878
success = self.write_config()
7979
if success:
8080
print(f"Set service to {service_name} successfully.")
81-
# kernel = get_icortex_kernel()
81+
# kernel = get_icortex()
8282
if self.kernel is not None:
8383
self.set_service()
8484
return True
@@ -87,7 +87,7 @@ def set_service_config(self, service_name: str, hard_init=False) -> bool:
8787

8888
def set_service(self):
8989
# TODO: pass the --config flag from icortex somehow
90-
# kernel = get_icortex_kernel()
90+
# kernel = get_icortex()
9191
if self.kernel is None:
9292
return False
9393

@@ -113,11 +113,12 @@ def set_service(self):
113113
def ask_which_service(self) -> str:
114114
sorted_services = get_available_services()
115115
service_name = prompt_input(
116-
"Which code generation service would you like to use?\nVariables: "
116+
"Which code generation service would you like to use?\nAvailable services: "
117117
+ ", ".join(sorted_services)
118-
+ "\nDefault",
118+
+ "\nDefault:",
119119
type=str,
120120
default=DEFAULT_SERVICE,
121+
press_enter=True,
121122
)
122123
return service_name
123124

icortex/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import typing as t
2-
import importlib.metadata
2+
import importlib_metadata
33
from copy import deepcopy
44
import platform
55
from icortex.defaults import DEFAULT_HISTORY_VAR
66

7-
icortex_version = importlib.metadata.version("icortex")
7+
icortex_version = importlib_metadata.version("icortex")
88

99
INITIAL_HISTORY_VAL = {
1010
"metadata": {

icortex/helper.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ def yes_no_input(message: str, default=True) -> bool:
4747
return (user_input == "" and default) or user_input.strip().lower() == "y"
4848

4949

50-
def prompt_input(message: str, type=str, default=None):
50+
def prompt_input(message: str, type=str, default=None, press_enter=False):
5151
if default:
52-
message += f" [{default}]"
52+
message += f" {default}"
53+
if press_enter:
54+
message += " (Press enter to select the default option)"
5355

5456
print(message)
5557
user_input = input()

icortex/init.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
from icortex.kernel import ICortexShell
3+
import __main__
4+
5+
# import ipdb; ipdb.set_trace()
6+
# If the kernel is IPython, load the magics.
7+
if "get_icortex" in __main__.__dict__:
8+
logging.warning("ICortex is already initialized, skipping.")
9+
elif "get_ipython" in __main__.__dict__ and "get_icortex" not in __main__.__dict__:
10+
# from icortex.magics import load_ipython_extension
11+
# ipython = globals()["get_ipython"]()
12+
ICortexShell._init_icortex_shell(__main__.get_ipython())
13+
# load_ipython_extension(get_ipython())
14+
else:
15+
raise Exception("IPython is not available, cannot initialize ICortex.")

0 commit comments

Comments
 (0)