24Sureshkumar commited on
Commit
2d8ff37
·
verified ·
1 Parent(s): e3ee8f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -64
app.py CHANGED
@@ -1,85 +1,57 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
 
6
- st.set_page_config(
7
- page_title="Tamil Creative Studio",
8
- page_icon="🇮🇳",
9
- layout="centered",
10
- )
11
-
12
- def load_css():
13
- st.markdown(
14
- """<style>
15
- .header {
16
- text-align: center;
17
- padding: 20px;
18
- background: #f9f9f9;
19
- border-radius: 10px;
20
- margin-bottom: 20px;
21
- }
22
- .header h1 { color: #cc0000; }
23
- .header p { color: #333; font-style: italic; }
24
- </style>""",
25
- unsafe_allow_html=True,
26
- )
27
-
28
  @st.cache_resource
29
  def load_all_models():
30
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
31
- from diffusers import StableDiffusionPipeline
32
- import torch
33
-
34
- # Translation model
35
- model_id = "ai4bharat/indictrans2-indic-en-dist-200M"
36
- tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
37
- model = AutoModelForSeq2SeqLM.from_pretrained(model_id, trust_remote_code=True)
38
 
39
- # Text generation model (simple GPT-2)
40
  text_gen = pipeline("text-generation", model="gpt2")
41
 
42
- # Image generation model (Stable Diffusion)
43
  img_pipe = StableDiffusionPipeline.from_pretrained(
44
- "CompVis/stable-diffusion-v1-4",
45
- revision="fp16",
46
- torch_dtype=torch.float16
47
- ).to("cuda")
48
 
49
- return tokenizer, model, text_gen, img_pipe
50
 
51
-
52
- def translate_tamil(text, tokenizer, model):
53
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
54
- outs = model.generate(**inputs, max_length=150, num_beams=5, early_stopping=True)
55
- return tokenizer.decode(outs[0], skip_special_tokens=True)
56
 
57
  def main():
58
- load_css()
59
- st.markdown(
60
- '<div class="header"><h1>🌐 தமிழ் → English → Creative Studio</h1>'
61
- '<p>Translate Tamil text and generate creative content</p></div>',
62
- unsafe_allow_html=True
63
- )
64
- tokenizer, model, text_gen, img_pipe = load_all_models()
65
- tamil_text = st.text_area("**தமிழ் உரை:**", height=150, placeholder="உங்கள் உரையை இங்கே உள்ளிடவும்...")
66
 
67
- if st.button("உருவாக்கு"):
68
- if not tamil_text.strip():
69
- st.warning("உரையை உள்ளிடவும்.")
70
- return
71
 
72
- with st.spinner("மொழிபெயர்க்கிறது..."):
73
- eng = translate_tamil(tamil_text, tokenizer, model)
74
- st.success(eng)
 
75
 
76
- with st.spinner("உரை உருவாக்குதல்..."):
77
- creative = text_gen(f"Create a creative description about: {eng}", max_length=80, num_return_sequences=1)[0]["generated_text"]
78
- st.info(creative)
 
79
 
80
- with st.spinner("படத்தை உருவாக்குதல்..."):
81
- img = img_pipe(eng, num_inference_steps=40, guidance_scale=8.5).images[0]
82
- st.image(img, caption="Generated Image", use_column_width=True)
 
83
 
84
  if __name__ == "__main__":
85
  main()
 
1
  import streamlit as st
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  @st.cache_resource
7
  def load_all_models():
8
+ # Load IndicTrans2 Tamil-to-English model
9
+ trans_model_id = "ai4bharat/indictrans2-indic-en-dist-200M"
10
+ trans_tokenizer = AutoTokenizer.from_pretrained(trans_model_id, trust_remote_code=True)
11
+ trans_model = AutoModelForSeq2SeqLM.from_pretrained(trans_model_id, trust_remote_code=True)
 
 
 
 
12
 
13
+ # Load English text generation model (you can use GPT2 or Falcon, etc.)
14
  text_gen = pipeline("text-generation", model="gpt2")
15
 
16
+ # Load Stable Diffusion for image generation
17
  img_pipe = StableDiffusionPipeline.from_pretrained(
18
+ "runwayml/stable-diffusion-v1-5",
19
+ torch_dtype=torch.float16,
20
+ revision="fp16"
21
+ ).to("cuda" if torch.cuda.is_available() else "cpu")
22
 
23
+ return trans_tokenizer, trans_model, text_gen, img_pipe
24
 
25
+ def translate_text(text, tokenizer, model):
26
+ input_text = f"translate Tamil to English: {text}"
27
+ inputs = tokenizer(input_text, return_tensors="pt", padding=True)
28
+ outputs = model.generate(**inputs, max_length=128)
29
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
30
 
31
  def main():
32
+ st.title("Multimodal Tamil to Image Generator 🚀")
33
+ st.markdown("Enter Tamil text, we translate it to English, continue the sentence, and generate an image!")
34
+
35
+ user_input = st.text_area("Enter Tamil text:", "")
 
 
 
 
36
 
37
+ if st.button("Generate"):
38
+ with st.spinner("Loading models..."):
39
+ tokenizer, model, text_gen, img_pipe = load_all_models()
 
40
 
41
+ with st.spinner("Translating to English..."):
42
+ english_text = translate_text(user_input, tokenizer, model)
43
+ st.subheader("Translated English:")
44
+ st.write(english_text)
45
 
46
+ with st.spinner("Generating continuation..."):
47
+ continuation = text_gen(english_text, max_length=50, do_sample=True)[0]['generated_text']
48
+ st.subheader("Generated Text:")
49
+ st.write(continuation)
50
 
51
+ with st.spinner("Generating Image..."):
52
+ image = img_pipe(continuation).images[0]
53
+ st.subheader("Generated Image:")
54
+ st.image(image)
55
 
56
  if __name__ == "__main__":
57
  main()