File size: 1,982 Bytes
1437058
048c81d
d550533
eea6ac5
d16c1e4
048c81d
9fd10ba
d7164de
048c81d
 
 
 
 
 
 
391969d
048c81d
391969d
9bdb949
048c81d
d7164de
048c81d
4569162
 
048c81d
391969d
9607ff2
c1732d5
048c81d
4569162
048c81d
4569162
048c81d
 
 
4569162
 
048c81d
391969d
67241c5
048c81d
 
 
770a398
048c81d
 
770a398
048c81d
 
 
4569162
048c81d
 
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
52
53
54
55
56
57
import streamlit as st
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
from diffusers import StableDiffusionPipeline
import torch

# Cache models for faster loading
@st.cache_resource
def load_all_models():
    # Translation model
    translation_model = AutoModelForSeq2SeqLM.from_pretrained(
        "ai4bharat/indictrans2-indic-en-dist-200M", trust_remote_code=True
    )
    translation_tokenizer = AutoTokenizer.from_pretrained(
        "ai4bharat/indictrans2-indic-en-dist-200M", trust_remote_code=True
    )
    translation_pipeline = pipeline(
        "text2text-generation", model=translation_model, tokenizer=translation_tokenizer
    )

    # Image generation model (Stable Diffusion)
    img_pipe = StableDiffusionPipeline.from_pretrained(
        "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
    )
    img_pipe = img_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
    
    return translation_pipeline, img_pipe

def main():
    st.title("πŸ“˜ Tamil to English Translator & Image Generator")

    tamil_text = st.text_area("πŸ“ Enter Tamil text (word or sentence)", height=100)

    if st.button("πŸ”„ Translate & Generate Image"):
        if not tamil_text.strip():
            st.warning("Please enter some Tamil text.")
            return

        try:
            translation_pipeline, img_pipe = load_all_models()

            # Prepare translation input
            formatted_input = "<2en><|ta|>" + tamil_text.strip()
            translated = translation_pipeline(formatted_input, max_length=256)[0]["generated_text"]

            st.success("βœ… English Translation:")
            st.write(translated)

            with st.spinner("πŸ–ΌοΈ Generating image..."):
                image = img_pipe(translated).images[0]
                st.image(image, caption="πŸ–ΌοΈ Generated from English text")

        except Exception as e:
            st.error(f"❌ Error: {str(e)}")

if __name__ == "__main__":
    main()