mohammad-shirkhani's picture
Update README.md
0f03fc1 verified
|
raw
history blame
2.95 kB
metadata
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

PersianToImage logo

PersianToImage

PersianToImage 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
  • Model type: Text-to-Image generation
  • Languages: Persian and English
  • License: CC 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 torch, diffusers, transformers, and accelerate libraries.

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)