Featuring Notebook (Colab/Jupyter) Compatibility#42
Conversation
p-e-w
left a comment
There was a problem hiding this comment.
Thanks, this is looking much better now!
I'm surprised that Rich, which is used by tens of thousands of Python projects, should have broken notebook detection. Can you describe where Rich's function failed in your tests?
src/heretic/utils.py
Outdated
| if is_notebook(): | ||
| print() | ||
| if password: | ||
| return getpass.getpass(message) |
There was a problem hiding this comment.
Please move password handling into prompt_password. There is no need to clutter this function with unrelated functionality.
src/heretic/utils.py
Outdated
| # For text input, we might need unsafe_ask if requested | ||
| kwargs = {"default": default} | ||
| if qmark: | ||
| kwargs["qmark"] = qmark |
There was a problem hiding this comment.
You can just set the qmark default to "?" in the arguments (which is what Questionary uses) and save yourself this kwargs trick. Just pass the parameters regularly then.
| return result if result else default | ||
| else: | ||
| return questionary.path( | ||
| message, default=default, only_directories=only_directories |
There was a problem hiding this comment.
Adding only_directories is a good improvement, but you aren't using it! The path prompt for saving the model should use only_directories=True to make this actually work.
You can review those fixes you told they are done, regarding def _is_jupyter() -> bool: # pragma: no cover
"""Check if we're running in a Jupyter notebook."""
try:
get_ipython # type: ignore[name-defined]
except NameError:
return False
ipython = get_ipython() # type: ignore[name-defined]
shell = ipython.__class__.__name__
if (
"google.colab" in str(ipython.__class__)
or os.getenv("DATABRICKS_RUNTIME_VERSION")
or shell == "ZMQInteractiveShell"
):
return True # Jupyter notebook or qtconsole
elif shell == "TerminalInteractiveShell":
return False # Terminal running IPython
else:
return False # Other type (?)THIS FUNCTION ABOVE IS FROM THE RICH LIBRARY ITSELF, WHICH DOESN'T LOOK SO ROBUST ENOUGHT, AS I BUILD THE VERSION THAT USED THIS IMPORT FUNCTION, + ALSO HARDCODED THIS FUNCTION AS WELL EXACTLY SAME, BOTH TESTS DIDN'T WORKED. (ON COLAB) WHILE THIS IS MY NEW FUNCTION WHICH IS BETTER AND ACTUALLY WORKS:def is_notebook() -> bool:
# Check for specific environment variables (Colab, Kaggle)
# This is necessary because when running as a subprocess (e.g. !heretic),
# get_ipython() might not be available or might not reflect the notebook environment.
if os.getenv("COLAB_GPU") or os.getenv("KAGGLE_KERNEL_RUN_TYPE"):
return True
# Check IPython shell type (for library usage)
try:
from IPython import get_ipython
shell = get_ipython()
if shell is None:
return False
shell_name = shell.__class__.__name__
if shell_name in ["ZMQInteractiveShell", "Shell"]:
return True
if "google.colab" in str(shell.__class__):
return True
return False
except (ImportError, NameError, AttributeError):
return Falsepossible issues
|
- Move password handling to prompt_password - Use only_directories=True for save path prompt - Simplify prompt_text arguments
c527ce5 to
0710ad9
Compare
|
Merged! Thank you for the PR, being able to run Heretic from Colab is a useful feature. |
|
Excuse me, but on kaggle the program don't let me choose anythin' because I can't use inputs. Maybe there is an error by me or I don't know what to do. Can you help me? |
|
@Filippide8864 actually, for colab since it supports it's not just for heretic but for all kind of code, so it's a issue with kaggle which is there for years |
|
So, what can I do for use heretic on Kaggle? |
|
well as I remember the 1st options when all trials are done is likely save the model? that's what you can do or select a trial normally the best trial is at top of list, so just try hitting enter or mouse clicks somehow might work as I remember, since the 2x t4 GPU is what I assume mostly matter to you + it's 12 hour session time, instead of a single t4 on colab. You can route heretic into a gradio app or different tunneling options you can find, and run heretic from there, the backend will be kaggle GPUs though the frontend would be hosted to a link you'll get there OR, you can also try setting up modal.com GPUs they provide literal most powerful GPUs with minimal setup, you can even run anything from your own device in your terminal/IDE and all process will be done in their GPU clusters |
|
Thanks, but I use the P100, cuz I only run it on less then 4B models; so I don't need a lot of time, but I can't find a gradio app for heretic. There is a good one or I have to do it myself? |
|
You can do it yourself, but if need help, here's what gemini suggests: To achieve a full terminal interface (TUI) for running heretic on Kaggle from another tab, you can use ttyd (a tool to share your terminal over the web) combined with Cloudflared for tunneling. This creates a secure, web-based terminal that executes commands directly on the Kaggle GPU backend. Step-by-Step Implementation
!apt-get update && apt-get install -y ttyd
!wget -q https://github.com
!dpkg -i cloudflared-linux-amd64.deb
import subprocess
import time
# 1. Start ttyd on port 7681 in the background
# This hosts a bash shell that you can access via browser
subprocess.Popen(["ttyd", "-p", "7681", "bash"])
# 2. Start Cloudflare Tunnel to expose port 7681
# 'trycloudflare' generates a free, random public URL
proc = subprocess.Popen(
["cloudflared", "tunnel", "--url", "http://localhost:7681"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True
)
# 3. Print the public URL so you can click it
print("Wait for the 'trycloudflare.com' link to appear...")
for line in iter(proc.stdout.readline, ""):
if "trycloudflare.com" in line:
print(f"\n--- ACCESS YOUR TERMINAL HERE ---\n{line.strip()}\n----------------------------------")
breakOpen the Link: Click the trycloudflare.com link printed in the output. A new browser tab will open with a fully functional Linux terminal.Why this works for Heretic
NOT TESTED BY ME BUT YOU CAN TRY... |
This time freshly added compatiblity so it's preserves TUI experience with arrow keys for local terminal usage, while also making it usable to people using a notebook environment
Notes:
Practically tested but your suggestion of
richfor_jupyterdidn't worked that import doesn't work since that function in rich library is not so robustEven tried hardcoding that same function exactly from the library itself, yet didn't worked.
So TO Ensure it works used a better
is_notebookfunctionIt works properly as tested other don't