Instructions to use xfcghj/AR with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use xfcghj/AR with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("xfcghj/AR", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Download models/clip.py from xfcghj/AR: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/xfcghj/AR/resolve/main/models/clip.py
- Command line
-
hf download hf://xfcghj/AR/models/clip.py
-
curl -L -o clip.py https://huggingface.co/xfcghj/AR/resolve/main/models/clip.py
1.66 kB
| import os | |
| import torch | |
| import torch.nn as nn | |
| from transformers import CLIPTokenizer, CLIPTextModel | |
| class FrozenCLIPTextEncoder(nn.Module): | |
| def __init__(self, model_path_or_id: str, device: str = "cuda"): | |
| super().__init__() | |
| self.device = device | |
| if os.path.isdir(model_path_or_id): | |
| assert os.path.exists(os.path.join(model_path_or_id, "config.json")), \ | |
| f"Error: Local path '{model_path_or_id}' does not contain 'config.json'. Check your weight path." | |
| self.tokenizer = CLIPTokenizer.from_pretrained(model_path_or_id, local_files_only=True) | |
| self.text_encoder = CLIPTextModel.from_pretrained(model_path_or_id, local_files_only=True).to(self.device) | |
| else: | |
| self.tokenizer = CLIPTokenizer.from_pretrained(model_path_or_id) | |
| self.text_encoder = CLIPTextModel.from_pretrained(model_path_or_id).to(self.device) | |
| self.text_encoder.eval() | |
| for param in self.text_encoder.parameters(): | |
| param.requires_grad = False | |
| def forward(self, text: str or list[str]) -> torch.Tensor: | |
| if isinstance(text, str): | |
| text = [text] | |
| inputs = self.tokenizer( | |
| text, | |
| padding="max_length", | |
| truncation=True, | |
| max_length=77, | |
| return_tensors="pt", | |
| ).to(self.device) | |
| outputs = self.text_encoder(**inputs) | |
| text_tokens = outputs.last_hidden_state # [B, 77, 512] | |
| text_tokens = text_tokens / text_tokens.norm(dim=-1, keepdim=True).clamp_min(1e-6) | |
| return text_tokens |