mohammad-shirkhani
commited on
Commit
•
f0c880a
1
Parent(s):
91012bc
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Text-to-Image Pipeline in Persian
|
2 |
+
|
3 |
+
Install Required Libraries
|
4 |
+
|
5 |
+
!pip install transformers diffusers accelerate torch
|
6 |
+
|
7 |
+
Import Necessary Libraries
|
8 |
+
|
9 |
+
import torch
|
10 |
+
from transformers import MT5ForConditionalGeneration, T5Tokenizer
|
11 |
+
from diffusers import StableDiffusionPipeline
|
12 |
+
|
13 |
+
Set Device (GPU or CPU)
|
14 |
+
|
15 |
+
# Determine the device: GPU if available, otherwise CPU
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
print(f"Using device: {device}")
|
18 |
+
|
19 |
+
Define and Load the Persian-to-Image Model Class
|
20 |
+
|
21 |
+
# Define the model class
|
22 |
+
class PersianToImageModel:
|
23 |
+
def __init__(self, translation_model_name, image_model_name, device):
|
24 |
+
self.device = device
|
25 |
+
|
26 |
+
# Load translation model
|
27 |
+
self.translation_model = MT5ForConditionalGeneration.from_pretrained(translation_model_name).to(device)
|
28 |
+
self.translation_tokenizer = T5Tokenizer.from_pretrained(translation_model_name)
|
29 |
+
|
30 |
+
# Load image generation model
|
31 |
+
self.image_model = StableDiffusionPipeline.from_pretrained(image_model_name).to(device)
|
32 |
+
|
33 |
+
def translate_text(self, persian_text):
|
34 |
+
input_ids = self.translation_tokenizer.encode(persian_text, return_tensors="pt").to(self.device)
|
35 |
+
translated_ids = self.translation_model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
|
36 |
+
translated_text = self.translation_tokenizer.decode(translated_ids[0], skip_special_tokens=True)
|
37 |
+
return translated_text
|
38 |
+
|
39 |
+
def generate_image(self, english_text):
|
40 |
+
image = self.image_model(english_text).images[0]
|
41 |
+
return image
|
42 |
+
|
43 |
+
def __call__(self, persian_text):
|
44 |
+
# Translate Persian text to English
|
45 |
+
english_text = self.translate_text(persian_text)
|
46 |
+
print(f"Translated Text: {english_text}")
|
47 |
+
|
48 |
+
# Generate and return image
|
49 |
+
return self.generate_image(english_text)
|
50 |
+
|
51 |
+
Instantiate the Model
|
52 |
+
|
53 |
+
# Instantiate the combined model
|
54 |
+
translation_model_name = 'mohammad-shirkhani/finetune_persian_to_english_mt5_base_summarize_on_celeba_hq'
|
55 |
+
image_model_name = 'ebrahim-k/Stable-Diffusion-1_5-FT-celeba_HQ_en'
|
56 |
+
|
57 |
+
persian_to_image_model = PersianToImageModel(translation_model_name, image_model_name, device)
|
58 |
+
|
59 |
+
Example Usage of the Model
|
60 |
+
|
61 |
+
from IPython.display import display
|
62 |
+
|
63 |
+
persian_text = "این زن دارای موهای موج دار ، لب های بزرگ و موهای قهوه ای است و رژ لب دارد.این زن موهای موج دار و لب های بزرگ دارد و رژ لب دارد.فرد جذاب است و موهای موج دار ، چشم های باریک و موهای قهوه ای دارد."
|
64 |
+
image = persian_to_image_model(persian_text)
|
65 |
+
display(image)
|
66 |
+
|
67 |
+
|
68 |
+
persian_text2 = "این مرد جذاب دارای موهای قهوه ای ، سوزش های جانبی ، دهان کمی باز و کیسه های زیر چشم است.این فرد جذاب دارای کیسه های زیر چشم ، سوزش های جانبی و دهان کمی باز است."
|
69 |
+
image2 = persian_to_image_model(persian_text2)
|
70 |
+
display(image2)
|