24Sureshkumar commited on
Commit
d550533
·
verified ·
1 Parent(s): 1d8b46e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -39
app.py CHANGED
@@ -1,61 +1,60 @@
1
  # app.py
2
  import streamlit as st
3
- from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer, pipeline
4
- from diffusers import DiffusionPipeline
5
  import torch
6
 
7
- @st.cache_resource(show_spinner=False)
8
- def load_all_models():
9
  # Load translation model
10
- model_name = "facebook/m2m100_418M"
11
- tokenizer = M2M100Tokenizer.from_pretrained(model_name)
12
- model = M2M100ForConditionalGeneration.from_pretrained(model_name)
13
 
14
- # Load creative text model (smaller GPT-2)
15
- textgen = pipeline("text-generation", model="gpt2", device=-1)
16
 
17
- # Load lightweight image generation pipeline
18
- img_pipe = DiffusionPipeline.from_pretrained(
19
- "stabilityai/sdxl-lite", torch_dtype=torch.float32
20
- ).to("cpu")
 
 
 
 
21
 
22
- return tokenizer, model, textgen, img_pipe
23
 
24
  def translate(text, tokenizer, model):
25
- tokenizer.src_lang = "ta"
26
- inputs = tokenizer(text, return_tensors="pt")
27
- output = model.generate(inputs["input_ids"], forced_bos_token_id=tokenizer.get_lang_id("en"), max_length=100)
28
- return tokenizer.decode(output[0], skip_special_tokens=True)
29
-
30
- def generate_text(prompt, pipe):
31
- output = pipe(prompt, max_length=60, do_sample=True)[0]
32
- return output["generated_text"]
33
 
34
  def main():
35
- st.set_page_config(page_title="Tamil to English Creative Image", layout="centered")
36
- st.title("🌐 தமிழ் English Creative Text + Image")
37
-
38
- tokenizer, model, textgen, img_pipe = load_all_models()
39
 
40
- tamil_text = st.text_area("தமிழ் உரையை உள்ளிடவும்:", height=130)
41
 
42
- if st.button("உருவாக்கு"):
43
- if not tamil_text.strip():
44
- st.warning("தயவுசெய்து உரையை உள்ளிடவும்.")
45
  return
46
 
47
- with st.spinner("மொழிபெயர்ப்பு..."):
48
- english_text = translate(tamil_text, tokenizer, model)
49
- st.success(f"🔁 Translated: {english_text}")
50
 
51
- with st.spinner("உரையாக்கம்..."):
52
- creative_text = generate_text(english_text, textgen)
53
- st.info("📝 Creative Output:")
54
- st.write(creative_text)
55
 
56
  with st.spinner("படம் உருவாக்கப்படுகிறது..."):
57
- image = img_pipe(english_text).images[0]
58
- st.image(image, caption="🎨 Generated Image", use_column_width=True)
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
  main()
 
1
  # app.py
2
  import streamlit as st
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
4
+ from diffusers import StableDiffusionPipeline
5
  import torch
6
 
7
+ @st.cache_resource
8
+ def load_models():
9
  # Load translation model
10
+ translation_model_name = "Helsinki-NLP/opus-mt-ta-en"
11
+ translator_tokenizer = AutoTokenizer.from_pretrained(translation_model_name)
12
+ translator_model = AutoModelForSeq2SeqLM.from_pretrained(translation_model_name)
13
 
14
+ # Load text generation model
15
+ textgen = pipeline("text-generation", model="gpt2")
16
 
17
+ # Load stable diffusion (light version)
18
+ image_model_name = "CompVis/stable-diffusion-v1-4"
19
+ image_pipe = StableDiffusionPipeline.from_pretrained(
20
+ image_model_name,
21
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
22
+ )
23
+ if torch.cuda.is_available():
24
+ image_pipe = image_pipe.to("cuda")
25
 
26
+ return translator_tokenizer, translator_model, textgen, image_pipe
27
 
28
  def translate(text, tokenizer, model):
29
+ inputs = tokenizer.encode(text, return_tensors="pt", truncation=True, max_length=512)
30
+ outputs = model.generate(inputs, max_length=512, num_beams=4)
31
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
32
 
33
  def main():
34
+ st.set_page_config(page_title="Tamil Text to English + Image + Text", layout="centered")
35
+ st.title("🌐 தமிழ் உரையை ஆங்கிலம் + படம் + உரை ஆக மாற்றவும்")
 
 
36
 
37
+ tamil_input = st.text_area("தமிழ் உரையை உள்ளிடவும்", height=150)
38
 
39
+ if st.button("உரையை மொழிபெயர்த்து உருவாக்கவும்"):
40
+ if not tamil_input.strip():
41
+ st.warning("உரையை உள்ளிடவும்.")
42
  return
43
 
44
+ with st.spinner("மாடல்கள் ஏற்றப்படுகிறது..."):
45
+ tokenizer, model, textgen, image_pipe = load_models()
 
46
 
47
+ with st.spinner("மொழிபெயர்ப்பு நடைபெறுகிறது..."):
48
+ english_text = translate(tamil_input, tokenizer, model)
49
+ st.success(f"🔤 மொழிபெயர்ப்பு: {english_text}")
 
50
 
51
  with st.spinner("படம் உருவாக்கப்படுகிறது..."):
52
+ image = image_pipe(english_text).images[0]
53
+ st.image(image, caption="உருவாக்கப்பட்ட படம்", use_column_width=True)
54
+
55
+ with st.spinner("உரையை உருவாக்கப்படுகிறது..."):
56
+ story = textgen(english_text, max_length=100, num_return_sequences=1)[0]["generated_text"]
57
+ st.text_area("✍️ உருவாக்கப்பட்ட உரை", value=story, height=200)
58
 
59
  if __name__ == "__main__":
60
  main()