File size: 1,893 Bytes
1437058 391969d d550533 eea6ac5 d16c1e4 770a398 9fd10ba d7164de 391969d 770a398 391969d 770a398 391969d 9bdb949 d7164de 770a398 4569162 5ca4bcd 391969d 9607ff2 b4aa913 c1732d5 770a398 4569162 770a398 4569162 770a398 4569162 770a398 391969d 67241c5 770a398 4569162 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 58 |
import streamlit as st
from transformers import pipeline
from diffusers import StableDiffusionPipeline
import torch
# Cache and load models only once
@st.cache_resource
def load_all_models():
translation_pipeline = pipeline(
"text2text-generation",
model="ai4bharat/indictrans2-indic-en-dist-200M",
tokenizer="ai4bharat/indictrans2-indic-en-dist-200M",
device=0 if torch.cuda.is_available() else -1,
trust_remote_code=True # Required for custom tokenizer
)
img_pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
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 Image Generator")
tamil_text = st.text_area("✍️ Enter Tamil word or sentence:")
if st.button("Translate & Generate Image"):
if tamil_text.strip() == "":
st.warning("⚠️ Please enter some Tamil text.")
return
with st.spinner("⏳ Loading models..."):
translation_pipeline, img_pipe = load_all_models()
# Format input for IndicTrans2
formatted_input = f"<2ta> <2en> {tamil_text.strip()}"
try:
translated = translation_pipeline(formatted_input)[0]["translation_text"]
except Exception as e:
st.error(f"❌ Translation failed: {e}")
return
st.success("✅ Translation Successful!")
st.markdown(f"**🗣️ English Translation:** {translated}")
with st.spinner("🎨 Generating image..."):
image = img_pipe(translated).images[0]
st.image(image, caption="🖼️ AI-Generated Image", use_column_width=True)
if __name__ == "__main__":
main()
|