File size: 1,654 Bytes
e97aebb 59921cd 803ddb4 59921cd f77ae83 c652e39 f77ae83 59921cd e5964e8 f77ae83 060cc15 45e16e5 060cc15 e97aebb f77ae83 e97aebb f77ae83 e97aebb 59921cd e97aebb f77ae83 e97aebb 45e16e5 e97aebb f77ae83 e97aebb f77ae83 e97aebb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import gradio as gr
from transformers import pipeline
from diffusers import StableDiffusionPipeline
import torch
import os
# β
Load HF token safely from environment
HF_TOKEN = os.getenv("HF_TOKEN", None)
# β
Select device
device = "cuda" if torch.cuda.is_available() else "cpu"
# β
1. Tamil β English Translator (multilingual model)
translator = pipeline(
"translation",
model="Helsinki-NLP/opus-mt-mul-en",
use_auth_token=HF_TOKEN
)
# β
2. English Text Generator
generator = pipeline("text-generation", model="gpt2")
# β
3. Image Generator using Stable Diffusion
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)
# β
Function to connect all 3 stages
def generate_image_from_tamil(tamil_input):
translated = translator(tamil_input, max_length=100)[0]['translation_text']
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text'].strip()
image = image_pipe(generated).images[0]
return translated, generated, image
# β
Gradio Interface
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 prompt β Creates an image using Stable Diffusion"
)
iface.launch()
|