Instructions to use d-s-b/Qwen_Imagine with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use d-s-b/Qwen_Imagine with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("d-s-b/Qwen_Imagine", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from d-s-b/Qwen_Imagine: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/d-s-b/Qwen_Imagine/resolve/main/handler.py
- Command line
-
hf download hf://d-s-b/Qwen_Imagine/handler.py
-
curl -L -o handler.py https://huggingface.co/d-s-b/Qwen_Imagine/resolve/main/handler.py
1.93 kB
| # handler.py | |
| from typing import Dict, List, Any | |
| import torch | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| self.tokenizer = AutoTokenizer.from_pretrained(path) | |
| self.model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.float16, device_map="auto") | |
| self.inference_prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context. | |
| Write a response that appropriately completes the request. | |
| Create an engaging and educational story that combines whimsical elements with real-world facts. | |
| ### Instruction: | |
| You are a creative storyteller who specializes in writing whimsical children's stories that incorporate educational facts about the real world. | |
| Please create a story based on the following prompt. | |
| ### Prompt: | |
| {} | |
| ### Response: | |
| <think> | |
| """ | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| question = data.pop("inputs", data) | |
| parameters = data.pop("parameters", {}) | |
| # Set default parameters | |
| max_new_tokens = parameters.get("max_new_tokens", 12000) | |
| # Format prompt | |
| prompt = self.inference_prompt_style.format(question) + self.tokenizer.eos_token | |
| # Tokenize | |
| inputs = self.tokenizer([prompt], return_tensors="pt") | |
| # Generate | |
| outputs = self.model.generate( | |
| input_ids=inputs.input_ids, | |
| attention_mask=inputs.attention_mask, | |
| max_new_tokens=max_new_tokens, | |
| eos_token_id=self.tokenizer.eos_token_id, | |
| use_cache=True, | |
| **parameters | |
| ) | |
| # Decode and extract response | |
| response = self.tokenizer.batch_decode(outputs, skip_special_tokens=True) | |
| result = response[0].split("### Response:")[1] | |
| return [{"generated_text": result}] |