|
import os |
|
import gradio as gr |
|
from transformers import pipeline |
|
from diffusers import StableDiffusionPipeline |
|
import torch |
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
|
|
|
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en") |
|
|
|
|
|
generator = pipeline("text-generation", model="gpt2") |
|
|
|
|
|
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 |
|
) |
|
image_pipe = image_pipe.to(device) |
|
|
|
def generate_image_from_tamil(tamil_text): |
|
|
|
translated = translator(tamil_text, max_length=100)[0]['translation_text'] |
|
|
|
|
|
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text'] |
|
|
|
|
|
image = image_pipe(generated).images[0] |
|
|
|
return translated, generated, image |
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_image_from_tamil, |
|
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"), |
|
outputs=[ |
|
gr.Textbox(label="Translated English Text"), |
|
gr.Textbox(label="Generated English Prompt"), |
|
gr.Image(label="Generated Image") |
|
], |
|
title="Tamil Text to English and Image Generator", |
|
description="Translate Tamil to English, generate English text, and create image using Stable Diffusion." |
|
) |
|
|
|
|
|
iface.launch(share=True) |
|
|