File size: 2,954 Bytes
0f03fc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0c880a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f03fc1
f0c880a
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
---
license: cc-by-nc-sa-4.0  
language:  
- fa  
- en  
library_name: transformers  
tags:  
- text-to-image  
- stable-diffusion  
- transformers  
pipeline_tag: text-to-image  
co2_eq_emissions:  
  emissions: 200000  
---

<p align="center">  
  <img src="PersianToImage.jpg" alt="PersianToImage logo" width=200/>  
</p>

# <span style="font-variant:small-caps;">PersianToImage</span>

<span style="font-variant:small-caps;">PersianToImage</span> is a unique pipeline that translates Persian text to English and generates corresponding images using a fine-tuned Stable Diffusion model. This tool is designed to easily convert descriptive Persian text into high-quality images, making it ideal for creative projects, visual content generation, and more.

## Model Description

- **Developed by:** [Your Name](mailto:your.email@example.com)  
- **Model type:** Text-to-Image generation  
- **Languages:** Persian and English  
- **License:** [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/) (non-commercial use only)

## How to Get Started with the Model

Use the code below to get started with the model.  
Make sure you have installed <code><b>torch</b></code>, <code><b>diffusers</b></code>, <code><b>transformers</b></code>, and <code><b>accelerate</b></code> libraries.

```python
import torch
from transformers import MT5ForConditionalGeneration, T5Tokenizer
from diffusers import StableDiffusionPipeline

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class PersianToImageModel:
    def __init__(self, translation_model_name, image_model_name, device):
        self.device = device

        self.translation_model = MT5ForConditionalGeneration.from_pretrained(translation_model_name).to(device)
        self.translation_tokenizer = T5Tokenizer.from_pretrained(translation_model_name)

        self.image_model = StableDiffusionPipeline.from_pretrained(image_model_name).to(device)

    def translate_text(self, persian_text):
        input_ids = self.translation_tokenizer.encode(persian_text, return_tensors="pt").to(self.device)
        translated_ids = self.translation_model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
        translated_text = self.translation_tokenizer.decode(translated_ids[0], skip_special_tokens=True)
        return translated_text

    def generate_image(self, english_text):
        image = self.image_model(english_text).images[0]
        return image

    def __call__(self, persian_text):
        english_text = self.translate_text(persian_text)
        print(f"Translated Text: {english_text}")

        return self.generate_image(english_text)

# Instantiate the model
translation_model_name = 'mohammad-shirkhani/finetune_persian_to_english_mt5_base_summarize_on_celeba_hq'
image_model_name = 'ebrahim-k/Stable-Diffusion-1_5-FT-celeba_HQ_en'

persian_to_image_model = PersianToImageModel(translation_model_name, image_model_name, device)