File size: 1,802 Bytes
1437058
391969d
d550533
eea6ac5
d16c1e4
9fd10ba
d7164de
391969d
 
 
 
 
 
 
 
9bdb949
391969d
d7164de
4569162
 
 
 
 
5ca4bcd
391969d
9607ff2
c1732d5
391969d
 
4569162
67241c5
4569162
 
67241c5
391969d
4569162
 
67241c5
391969d
67241c5
 
391969d
 
4569162
 
 
391969d
9607ff2
 
d16c1e4
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
import streamlit as st
from transformers import pipeline
from diffusers import StableDiffusionPipeline
import torch

@st.cache_resource
def load_all_models():
    # Load Tamil ➝ English translation model
    translation_pipeline = pipeline(
        "translation",
        model="ai4bharat/indictrans2-indic-en-dist-200M",
        tokenizer="ai4bharat/indictrans2-indic-en-dist-200M",
        src_lang="ta", tgt_lang="en",
        device=0 if torch.cuda.is_available() else -1,
    )

    # Load image generation model
    img_pipe = StableDiffusionPipeline.from_pretrained(
        "stabilityai/stable-diffusion-2-1",
        torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
        revision="fp16" if torch.cuda.is_available() else None,
    )
    img_pipe = img_pipe.to("cuda" if torch.cuda.is_available() else "cpu")

    return translation_pipeline, img_pipe

def main():
    st.set_page_config(page_title="Tamil ➝ English ➝ Image", layout="centered")
    st.title("🌐 Tamil to English to Image Generator")

    tamil_text = st.text_area("Enter Tamil text:", height=150)

    if st.button("Generate Image"):
        if not tamil_text.strip():
            st.warning("Please enter Tamil text.")
            return

        with st.spinner("Loading models..."):
            translation_pipeline, img_pipe = load_all_models()

        with st.spinner("Translating Tamil to English..."):
            translated = translation_pipeline(tamil_text)[0]["translation_text"]
            st.success(f"πŸ”€ Translated English: `{translated}`")

        with st.spinner("Generating image..."):
            image = img_pipe(prompt=translated).images[0]
            st.image(image, caption="🎨 AI-generated Image", use_column_width=True)

if __name__ == "__main__":
    main()