|
|
|
|
|
import gradio as gr |
|
from transformers import pipeline, MarianMTModel, MarianTokenizer |
|
from diffusers import StableDiffusionPipeline |
|
import torch |
|
import os |
|
|
|
|
|
hf_token = os.getenv("HUGGINGFACE_TOKEN") |
|
|
|
|
|
translation_model_name = "Helsinki-NLP/opus-mt-ta-en" |
|
translator = pipeline("translation", model=translation_model_name) |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
image_pipe = StableDiffusionPipeline.from_pretrained( |
|
"CompVis/stable-diffusion-v1-4", |
|
use_auth_token=hf_token, |
|
torch_dtype=torch.float16 if device == "cuda" else torch.float32 |
|
).to(device) |
|
|
|
|
|
def translate_and_generate(tamil_text): |
|
|
|
english_output = translator(tamil_text)[0]['translation_text'] |
|
|
|
|
|
image = image_pipe(english_output).images[0] |
|
|
|
return english_output, image |
|
|
|
|
|
iface = gr.Interface( |
|
fn=translate_and_generate, |
|
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"), |
|
outputs=[ |
|
gr.Textbox(label="Translated English Text"), |
|
gr.Image(label="Generated Image") |
|
], |
|
title="Tamil-to-Image Generator πΈ", |
|
description="Enter Tamil text. It will be translated to English and visualized using Stable Diffusion." |
|
) |
|
|
|
iface.launch() |
|
|