24Sureshkumar commited on
Commit
a1cf7cb
·
verified ·
1 Parent(s): d7164de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -20
app.py CHANGED
@@ -1,52 +1,91 @@
1
  import streamlit as st
2
- from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer, pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
 
6
  @st.cache_resource(show_spinner=False)
7
  def load_all_models():
8
- tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
9
- model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
 
 
 
10
  text_gen = pipeline("text-generation", model="gpt2", device=-1)
11
  img_pipe = StableDiffusionPipeline.from_pretrained(
12
- "stabilityai/stable-diffusion-2-base", torch_dtype=torch.float32
 
13
  ).to("cpu")
14
- return tokenizer, model, text_gen, img_pipe
 
15
 
16
  def translate_tamil(text, tokenizer, model):
17
- tokenizer.src_lang = "ta"
18
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
 
 
 
 
 
 
 
 
19
  generated = model.generate(
20
  **inputs,
21
- forced_bos_token_id=tokenizer.get_lang_id("en"),
22
- max_length=128
 
23
  )
24
- return tokenizer.decode(generated[0], skip_special_tokens=True)
 
 
 
 
 
 
25
 
26
  def main():
27
  st.set_page_config(page_title="Tamil→EN→Image/Text", layout="centered")
28
  st.title("🌐 தமிழ் → English → Creative Text & Image")
29
-
 
30
  tokenizer, model, text_gen, img_pipe = load_all_models()
31
- tamil_text = st.text_area("தமிழ் உரை:", height=150)
 
 
 
 
 
 
32
 
33
- if st.button("உருவாக்கு"):
34
  if not tamil_text.strip():
35
  st.warning("தயவு செய்து உரையை உள்ளிடவும்.")
36
  return
37
 
38
- with st.spinner("Translating…"):
39
  eng = translate_tamil(tamil_text, tokenizer, model)
40
- st.success(f"🔤 Translation: {eng}")
 
 
41
 
42
- with st.spinner("Creating creative text…"):
43
- creative = text_gen(f"Write a creative description of: {eng}", max_length=60, num_return_sequences=1)[0]["generated_text"]
 
 
 
 
 
44
  st.info("📝 Creative Text:")
45
  st.write(creative)
46
 
47
- with st.spinner("Generating image…"):
48
- img = img_pipe(eng, num_inference_steps=25, guidance_scale=7.5).images[0]
 
 
 
 
 
49
  st.image(img, caption="🎨 Generated Image")
50
 
51
  if __name__ == "__main__":
52
- main()
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
 
6
  @st.cache_resource(show_spinner=False)
7
  def load_all_models():
8
+ # Load IndicTrans2 model for Tamil→English
9
+ indic_tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-ta-en-dist-200M")
10
+ indic_model = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-ta-en-dist-200M")
11
+
12
+ # Load other models
13
  text_gen = pipeline("text-generation", model="gpt2", device=-1)
14
  img_pipe = StableDiffusionPipeline.from_pretrained(
15
+ "stabilityai/stable-diffusion-2-base",
16
+ torch_dtype=torch.float32
17
  ).to("cpu")
18
+
19
+ return indic_tokenizer, indic_model, text_gen, img_pipe
20
 
21
  def translate_tamil(text, tokenizer, model):
22
+ # Tokenize with batch processing
23
+ inputs = tokenizer(
24
+ text,
25
+ return_tensors="pt",
26
+ padding=True,
27
+ truncation=True,
28
+ max_length=128
29
+ )
30
+
31
+ # Generate translation
32
  generated = model.generate(
33
  **inputs,
34
+ max_length=150,
35
+ num_beams=5,
36
+ early_stopping=True
37
  )
38
+
39
+ # Decode with normalization
40
+ return tokenizer.batch_decode(
41
+ generated,
42
+ skip_special_tokens=True,
43
+ clean_up_tokenization_spaces=True
44
+ )[0]
45
 
46
  def main():
47
  st.set_page_config(page_title="Tamil→EN→Image/Text", layout="centered")
48
  st.title("🌐 தமிழ் → English → Creative Text & Image")
49
+
50
+ # Load models once
51
  tokenizer, model, text_gen, img_pipe = load_all_models()
52
+
53
+ # Input with Tamil placeholder
54
+ tamil_text = st.text_area(
55
+ "தமிழ் உரை:",
56
+ height=150,
57
+ placeholder="உங்கள் உரையை இங்கே உள்ளிடவும்..."
58
+ )
59
 
60
+ if st.button("உருவாக்கு", type="primary"):
61
  if not tamil_text.strip():
62
  st.warning("தயவு செய்து உரையை உள்ளிடவும்.")
63
  return
64
 
65
+ with st.spinner("மொழிபெயர்க்கிறது..."):
66
  eng = translate_tamil(tamil_text, tokenizer, model)
67
+
68
+ with st.expander("🔤 Translation", expanded=True):
69
+ st.success(eng)
70
 
71
+ with st.spinner("உரை உருவாக்குதல்..."):
72
+ creative = text_gen(
73
+ f"Create a creative description about: {eng}",
74
+ max_length=80,
75
+ num_return_sequences=1
76
+ )[0]["generated_text"]
77
+
78
  st.info("📝 Creative Text:")
79
  st.write(creative)
80
 
81
+ with st.spinner("படத்தை உருவாக்குதல்..."):
82
+ img = img_pipe(
83
+ eng,
84
+ num_inference_steps=35,
85
+ guidance_scale=8.0
86
+ ).images[0]
87
+
88
  st.image(img, caption="🎨 Generated Image")
89
 
90
  if __name__ == "__main__":
91
+ main()