Download utils.py from bhardwaj08sarthak/STEM-Question-Generator: direct link, hf CLI and curl.
- Browser
- Download file 981 Bytes
-
https://huggingface.co/spaces/bhardwaj08sarthak/STEM-Question-Generator/resolve/main/utils.py
- Command line
-
hf download hf://spaces/bhardwaj08sarthak/STEM-Question-Generator/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/bhardwaj08sarthak/STEM-Question-Generator/resolve/main/utils.py
981 Bytes
| import spaces | |
| def extract_top_level_json(s: str) -> str: | |
| start = s.find("{") | |
| if start == -1: | |
| return "" | |
| depth = 0 | |
| for i in range(start, len(s)): | |
| ch = s[i] | |
| if ch == "{": | |
| depth += 1 | |
| elif ch == "}": | |
| depth -= 1 | |
| if depth == 0: | |
| candidate = s[start:i + 1] | |
| try: | |
| json.loads(candidate) # validate | |
| return candidate | |
| except Exception: | |
| return "" | |
| return "" | |
| def get_local_model_gpu(model_id: str): | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32) | |
| model.to(device) | |
| model.eval() | |
| return model |