24Sureshkumar's picture
Update app.py
e97aebb verified
raw
history blame
1.66 kB
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
from diffusers import StableDiffusionPipeline
import torch
# 1. Tamil to English Translator
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ta-en")
# 2. English Text Generator (you can use GPT2 or any causal model)
generator = pipeline("text-generation", model="gpt2")
# 3. Image Generator using Stable Diffusion
device = "cuda" if torch.cuda.is_available() else "cpu"
image_pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
torch_dtype=torch.float16 if device == "cuda" else torch.float32
)
image_pipe = image_pipe.to(device)
# πŸ‘‡ Combined function
def generate_image_from_tamil(tamil_input):
# Step 1: Translate Tamil β†’ English
translated = translator(tamil_input, max_length=100)[0]['translation_text']
# Step 2: Generate English sentence based on translated input
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
# Step 3: Generate Image based on English text
image = image_pipe(generated).images[0]
return translated, generated, image
# 🎨 Gradio UI
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 to Image Generator πŸŒ…",
description="Translates Tamil β†’ English, generates story β†’ creates image using Stable Diffusion."
)
iface.launch()