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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -46
app.py CHANGED
@@ -2,24 +2,40 @@ 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",
@@ -28,7 +44,6 @@ def translate_tamil(text, tokenizer, model):
28
  max_length=128
29
  )
30
 
31
- # Generate translation
32
  generated = model.generate(
33
  **inputs,
34
  max_length=150,
@@ -36,7 +51,6 @@ def translate_tamil(text, tokenizer, model):
36
  early_stopping=True
37
  )
38
 
39
- # Decode with normalization
40
  return tokenizer.batch_decode(
41
  generated,
42
  skip_special_tokens=True,
@@ -44,48 +58,60 @@ def translate_tamil(text, tokenizer, model):
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()
 
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
+ import base64
6
+
7
+ # Set page config
8
+ st.set_page_config(
9
+ page_title="Tamil Creative Studio",
10
+ page_icon="🇮🇳",
11
+ layout="centered",
12
+ initial_sidebar_state="collapsed"
13
+ )
14
+
15
+ # Load custom CSS
16
+ def load_css(file_name):
17
+ with open(file_name, "r") as f:
18
+ st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
19
 
20
  @st.cache_resource(show_spinner=False)
21
  def load_all_models():
22
+ # Load translation model
23
+ trans_tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-ta-en-dist-200M")
24
+ trans_model = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-ta-en-dist-200M")
25
 
26
+ # Load text generation model
27
  text_gen = pipeline("text-generation", model="gpt2", device=-1)
28
+
29
+ # Load image generation model
30
  img_pipe = StableDiffusionPipeline.from_pretrained(
31
  "stabilityai/stable-diffusion-2-base",
32
+ torch_dtype=torch.float32,
33
+ safety_checker=None
34
  ).to("cpu")
35
 
36
+ return trans_tokenizer, trans_model, text_gen, img_pipe
37
 
38
  def translate_tamil(text, tokenizer, model):
 
39
  inputs = tokenizer(
40
  text,
41
  return_tensors="pt",
 
44
  max_length=128
45
  )
46
 
 
47
  generated = model.generate(
48
  **inputs,
49
  max_length=150,
 
51
  early_stopping=True
52
  )
53
 
 
54
  return tokenizer.batch_decode(
55
  generated,
56
  skip_special_tokens=True,
 
58
  )[0]
59
 
60
  def main():
61
+ load_css("style.css")
62
+
63
+ # Header with background
64
+ st.markdown(
65
+ """
66
+ <div class="header">
67
+ <h1>🌐 தமிழ் → English → Creative Studio</h1>
68
+ <p>Translate Tamil text and generate creative content</p>
69
+ </div>
70
+ """,
71
+ unsafe_allow_html=True
72
+ )
73
 
 
74
  tokenizer, model, text_gen, img_pipe = load_all_models()
75
 
76
+ with st.container():
77
+ tamil_text = st.text_area(
78
+ "**தமிழ் உரை:**",
79
+ height=150,
80
+ placeholder="உங்கள் உரையை இங்கே உள்ளிடவும்...",
81
+ key="tamil_input"
82
+ )
 
 
 
 
 
 
 
83
 
84
+ col1, col2 = st.columns([1, 3])
85
+ with col1:
86
+ if st.button("**உருவாக்கு**", type="primary", use_container_width=True):
87
+ if not tamil_text.strip():
88
+ st.warning("தயவு செய்து உரையை உள்ளிடவும்.")
89
+ st.stop()
90
+
91
+ with st.spinner("மொழிபெயர்க்கிறது..."):
92
+ eng = translate_tamil(tamil_text, tokenizer, model)
93
+
94
+ with st.expander("**🔤 Translation**", expanded=True):
95
+ st.success(eng)
96
+
97
+ with st.spinner("உரை உருவாக்குதல்..."):
98
+ creative = text_gen(
99
+ f"Create a creative description about: {eng}",
100
+ max_length=80,
101
+ num_return_sequences=1
102
+ )[0]["generated_text"]
103
+
104
+ st.info("**📝 Creative Text:**")
105
+ st.write(creative)
106
 
107
+ with st.spinner("படத்தை உருவாக்குதல்..."):
108
+ img = img_pipe(
109
+ eng,
110
+ num_inference_steps=40,
111
+ guidance_scale=8.5
112
+ ).images[0]
113
+
114
+ st.image(img, caption="**🎨 Generated Image**", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
115
 
116
  if __name__ == "__main__":
117
  main()