-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed HF download + gui stable (#65)
* Update load_env.py * Create utils.py * Update gui.py * Update startLLM.py * Update ask_libgen.py * Update ingest.py * Delete meta.json * Update README.md
- Loading branch information
Showing
8 changed files
with
113 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"""some useful functions""" | ||
from pathlib import Path | ||
|
||
from huggingface_hub import snapshot_download | ||
from huggingface_hub.utils import HFValidationError, validate_repo_id | ||
from prompt_toolkit import HTML, PromptSession, print_formatted_text | ||
from prompt_toolkit.styles import Style | ||
from pyexpat import ExpatError | ||
from requests import HTTPError | ||
|
||
style = Style.from_dict( | ||
{ | ||
"r": "italic gray", # remark | ||
"w": "italic yellow", # warning | ||
"d": "bold red", # danger | ||
"b": "bold", | ||
"i": "italic", | ||
"question": "ansicyan", | ||
"answer": "ansigreen", | ||
"source": "ansimagenta", | ||
} | ||
) | ||
|
||
|
||
def print_HTML(text: str, **kwargs) -> None: | ||
"""print formatted HTML text""" | ||
try: | ||
for k, v in kwargs.items(): # necessary | ||
kwargs[k] = str(v).replace("\f", "") | ||
text = text.replace("\f", "") | ||
print_formatted_text(HTML(text).format(**kwargs), style=style) | ||
except ExpatError: | ||
print(text) | ||
|
||
|
||
def prompt_HTML(session: PromptSession, prompt: str, **kwargs) -> str: | ||
"""print formatted HTML text""" | ||
try: | ||
for k, v in kwargs.items(): # necessary | ||
kwargs[k] = str(v).replace("\f", "") | ||
prompt = prompt.replace("\f", "") | ||
return session.prompt(HTML(prompt).format(**kwargs), style=style) | ||
except ExpatError: | ||
return input(prompt) | ||
|
||
|
||
def download_if_repo(path: str, file: str = None, allow_patterns: str | list[str] = None) -> str: | ||
"""download model from HF if not local""" | ||
if allow_patterns is None: | ||
allow_patterns = ["*.bin", "*.json"] | ||
p = Path("models/"+path) | ||
if p.is_file() or p.is_dir(): | ||
print(p, "already installed") | ||
return str(p) | ||
|
||
try: | ||
split = path.split("/") | ||
is_dataset = split[0] == "datasets" | ||
if is_dataset: | ||
split = split[1:] | ||
path = "/".join(split) | ||
|
||
if path.endswith(".bin"): | ||
path, file = "/".join(split[: 3 if is_dataset else 2]), split[-1] | ||
validate_repo_id(path) | ||
print_HTML("<r>Downloading {model} from HF</r>", model=path) | ||
new_path = Path( | ||
snapshot_download( | ||
repo_id=path, | ||
allow_patterns=file or allow_patterns, | ||
local_dir=f"models/{path}", | ||
repo_type="dataset" if is_dataset else None, | ||
local_dir_use_symlinks=False, | ||
) | ||
) | ||
if file is not None: | ||
files = [f for f in new_path.iterdir() if f.is_file() and f.name.endswith(".bin")] | ||
if len(files) > 1: | ||
names = "\n".join([f" - {f.name}" for f in files]) | ||
raise ValueError(f"Multiple model files found: \n\n{names}\n\n") | ||
new_path = files[0] | ||
return str(new_path.resolve()) | ||
|
||
except (HFValidationError, HTTPError) as e: | ||
print_HTML("<w>Could not download model {model} from HF: {e}</w>", model=path, e=e) |