Spaces:
Build error
Build error
Download src/model_utils.py from amiguel/ai_systems: direct link, hf CLI and curl.
- Browser
- Download file 949 Bytes
-
https://huggingface.co/spaces/amiguel/ai_systems/resolve/main/src/model_utils.py
- Command line
-
hf download hf://spaces/amiguel/ai_systems/src/model_utils.py
-
curl -L -o model_utils.py https://huggingface.co/spaces/amiguel/ai_systems/resolve/main/src/model_utils.py
949 Bytes
| from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline | |
| import torch | |
| def load_hf_model(model_name, device="cpu"): | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name) | |
| # Handle meta tensors properly | |
| if device == "cuda" and torch.cuda.is_available(): | |
| try: | |
| model = model.to(device) | |
| except NotImplementedError: | |
| # If meta tensor error occurs, use to_empty() | |
| model = model.to_empty(device=device) | |
| # Don't specify device in pipeline when using accelerate | |
| return pipeline("text-generation", model=model, tokenizer=tokenizer) | |
| def generate_answer(text_gen, question, context): | |
| prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:" | |
| result = text_gen(prompt, max_new_tokens=256, do_sample=True, temperature=0.7) | |
| return result[0]["generated_text"].split("Answer:")[-1].strip() | |