File size: 1,520 Bytes
eea6ac5
 
625a47c
eea6ac5
edac42b
eea6ac5
 
0724936
eea6ac5
1c69011
625a47c
eea6ac5
 
 
625a47c
eea6ac5
edac42b
eea6ac5
 
 
1c69011
edac42b
1c69011
 
eea6ac5
 
 
 
 
 
 
 
 
1c69011
a68e5a8
eea6ac5
 
 
edac42b
 
 
 
eea6ac5
 
edac42b
 
eea6ac5
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
# app.py

import gradio as gr
from transformers import pipeline, MarianMTModel, MarianTokenizer
from diffusers import StableDiffusionPipeline
import torch
import os

# Get Hugging Face token from environment (in Hugging Face Spaces this is auto-populated from secrets)
hf_token = os.getenv("HUGGINGFACE_TOKEN")

# Load Translation Pipeline: Tamil → English using MarianMT
translation_model_name = "Helsinki-NLP/opus-mt-ta-en"
translator = pipeline("translation", model=translation_model_name)

# Load Stable Diffusion Pipeline: English → Image
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)

# Core function
def translate_and_generate(tamil_text):
    # Step 1: Translate Tamil to English
    english_output = translator(tamil_text)[0]['translation_text']
    
    # Step 2: Generate Image from English Text
    image = image_pipe(english_output).images[0]
    
    return english_output, image

# Gradio UI
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()