Downloads · 30 days
5
11% of all-time downloads
parsi-ai-nlpclass/image-generation-mt5-finetune
image-generation-mt5-finetune is a machine learning model from parsi-ai-nlpclass. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This model pipeline is designed to generate images from Persian text descriptions. It works by first translating the Persian text into English and then using a fine-tuned Stable Diffusion model to generate the corresp…
Downloads · 30 days
5
11% of all-time downloads
All-time downloads
44
Public
Parameters
582M
2.4 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors2.3 GB · 99%
From the Hugging Face model README
This model pipeline is designed to generate images from Persian text descriptions. It works by first translating the Persian text into English and then using a fine-tuned Stable Diffusion model to generate the corresponding image. The pipeline combines two models: a translation model (mohammad-shirkhani/finetune_persian_to_english_mt5_base_summarize_on_celeba_hq) and an image generation model (ebrahim-k/Stable-Diffusion-1_5-FT-celeba_HQ_en).
mohammad-shirkhani/finetune_persian_to_english_mt5_base_summarize_on_celeba_hqebrahim-k/Stable-Diffusion-1_5-FT-celeba_HQ_enThe pipeline operates through the following steps:
!pip install transformers diffusers accelerate torch
import torch
from transformers import MT5ForConditionalGeneration, T5Tokenizer
from diffusers import StableDiffusionPipeline
This code determines whether the pipeline should use a GPU (if available) or fallback to a CPU.
# Determine the device: GPU if available, otherwise CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
The following class handles both translation and image generation tasks.
# Define the model class
class PersianToImageModel:
def __init__(self, translation_model_name, image_model_name, device):
self.device = device
# Load translation model
self.translation_model = MT5ForConditionalGeneration.from_pretrained(translation_model_name).to(device)
self.translation_tokenizer = T5Tokenizer.from_pretrained(translation_model_name)
# Load image generation model
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):
# Translate Persian text to English
english_text = self.translate_text(persian_text)
print(f"Translated Text: {english_text}")
# Generate and return image
return self.generate_image(english_text)
The following code snippet demonstrates how to instantiate the combined model.
# Instantiate the combined 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)
Below are examples of how to use the model to generate images from Persian text.
from IPython.display import display
# Persian text describing a person
persian_text = "این زن دارای موهای موج دار ، لب های بزرگ و موهای قهوه ای است و رژ لب دارد.این زن موهای موج دار و لب های بزرگ دارد و رژ لب دارد.فرد جذاب است و موهای موج دار ، چشم های باریک و موهای قهوه ای دارد."
# Generate and display the image
image = persian_to_image_model(persian_text)
display(image)
# Another example
persian_text2 = "این مرد جذاب دارای موهای قهوه ای ، سوزش های جانبی ، دهان کمی باز و کیسه های زیر چشم است.این فرد جذاب دارای کیسه های زیر چشم ، سوزش های جانبی و دهان کمی باز است."
image2 = persian_to_image_model(persian_text2)
display(image2)