File size: 1,249 Bytes
045a5ef
 
 
 
cc1a4a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328625d
 
25b0b21
 
328625d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1078887
328625d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
license: mit
base_model:
- stable-diffusion-v1-5/stable-diffusion-v1-5
language:
- hi
- bn
- as
- gu
- kn
- ml
- mr
- ne
- or
- pa
- sa
- ta
- te
- ur
- ks
- es
- fr
- ja
- zh
- tr
- de
- ar
- pt
- ru
- vi
- it
- ko
---

**Use with the Stable Diffusion Pipeline**

```python
import torch
from diffusers import StableDiffusionPipeline
from transformers import CLIPTokenizer, CLIPTextModel

device = "cuda" if torch.cuda.is_available() else "cpu"
lang = "hin_Deva"  # Hindi

# Load pipeline
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")

# Load the multilingual tokenizer 
tokenizer = CLIPTokenizer.from_pretrained("tokenizers/multilingual")
pipe.tokenizer = tokenizer
pipe.text_encoder.resize_token_embeddings(len(tokenizer))

# Load the fine-tuned text encoder
state_dict = torch.load(f"models/{lang}/{lang}_text_encoder.pth")
new_text_encoder = CLIPTextModel(config=pipe.text_encoder.config)
new_text_encoder.load_state_dict(state_dict)
new_text_encoder = new_text_encoder.to(device)
pipe.text_encoder = new_text_encoder
pipe = pipe.to(device)

# Generate and save image
caption = "गाँव का शांतिपूर्ण दृश्य|"
image = pipe(caption).images[0]
image.save(f"example.png")