Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,60 @@
|
|
1 |
# app.py
|
2 |
import streamlit as st
|
3 |
-
from transformers import
|
4 |
-
from diffusers import
|
5 |
import torch
|
6 |
|
7 |
-
@st.cache_resource
|
8 |
-
def
|
9 |
# Load translation model
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
# Load
|
15 |
-
textgen = pipeline("text-generation", model="gpt2"
|
16 |
|
17 |
-
# Load
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
return
|
23 |
|
24 |
def translate(text, tokenizer, model):
|
25 |
-
tokenizer.
|
26 |
-
|
27 |
-
|
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
|
36 |
-
st.title("🌐 தமிழ்
|
37 |
-
|
38 |
-
tokenizer, model, textgen, img_pipe = load_all_models()
|
39 |
|
40 |
-
|
41 |
|
42 |
-
if st.button("
|
43 |
-
if not
|
44 |
-
st.warning("
|
45 |
return
|
46 |
|
47 |
-
with st.spinner("
|
48 |
-
|
49 |
-
st.success(f"🔁 Translated: {english_text}")
|
50 |
|
51 |
-
with st.spinner("
|
52 |
-
|
53 |
-
|
54 |
-
st.write(creative_text)
|
55 |
|
56 |
with st.spinner("படம் உருவாக்கப்படுகிறது..."):
|
57 |
-
image =
|
58 |
-
|
|
|
|
|
|
|
|
|
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()
|