File size: 2,611 Bytes
eea6ac5
4deb4b5
9607ff2
 
eea6ac5
4deb4b5
9607ff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
# app.py
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from diffusers import StableDiffusionPipeline
import torch

# Load NLLB translation model
@st.cache_resource
def load_translation_model():
    model_name = "facebook/nllb-200-distilled-600M"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
    return tokenizer, model

# Load Stable Diffusion model
@st.cache_resource
def load_diffusion_model():
    pipe = StableDiffusionPipeline.from_pretrained(
        "CompVis/stable-diffusion-v1-4",
        torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
    )
    if torch.cuda.is_available():
        pipe = pipe.to("cuda")
    return pipe

# Translate Tamil to English using NLLB
def translate_text(text, tokenizer, model):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    inputs["forced_bos_token_id"] = tokenizer.lang_code_to_id["eng_Latn"]
    with torch.no_grad():
        translated_tokens = model.generate(**inputs, max_length=512)
    return tokenizer.decode(translated_tokens[0], skip_special_tokens=True)

# Streamlit App
def main():
    st.set_page_config(page_title="Tamil to Image Generator", layout="centered")
    with open("style.css") as f:
        st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

    st.title("🪔 தமிழ் உரை -> பட உருவாக்கம்")

    user_input = st.text_area("உங்கள் உரையை உள்ளிடவும் (தமிழில்)", height=150)

    if st.button("படம் உருவாக்கவும்"):
        if not user_input.strip():
            st.warning("தயவு செய்து தமிழ் உரையை உள்ளிடவும்.")
            return

        with st.spinner("மொழிபெயர்ப்பு மற்றும் பட உருவாக்கம் நடைபெறுகிறது..."):
            tokenizer, model = load_translation_model()
            tokenizer.src_lang = "tam_Taml"  # Tamil
            translated_text = translate_text(user_input, tokenizer, model)
            st.success(f"மொழிபெயர்ப்பு (ஆங்கிலம்): {translated_text}")

            pipe = load_diffusion_model()
            image = pipe(translated_text).images[0]
            st.image(image, caption="உங்கள் உருவாக்கப்பட்ட படம்", use_column_width=True)

if __name__ == "__main__":
    main()