Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,53 @@
|
|
1 |
# app.py
|
2 |
import streamlit as st
|
3 |
-
from transformers import
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
import torch
|
6 |
|
7 |
-
# Load
|
8 |
@st.cache_resource
|
9 |
def load_translation_model():
|
10 |
-
model_name = "
|
11 |
-
tokenizer =
|
12 |
-
model =
|
13 |
return tokenizer, model
|
14 |
|
15 |
-
# Load
|
16 |
@st.cache_resource
|
17 |
def load_diffusion_model():
|
18 |
pipe = StableDiffusionPipeline.from_pretrained(
|
19 |
-
"
|
20 |
-
torch_dtype=torch.float32
|
21 |
-
)
|
22 |
-
pipe = pipe.to("cpu")
|
23 |
return pipe
|
24 |
|
25 |
-
# Translate Tamil to English
|
26 |
-
def
|
27 |
-
prompt = f"
|
28 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
29 |
with torch.no_grad():
|
30 |
-
|
31 |
-
return tokenizer.decode(
|
32 |
|
33 |
-
# Streamlit
|
34 |
def main():
|
35 |
-
st.set_page_config(page_title="
|
36 |
-
|
37 |
-
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
38 |
|
39 |
-
st.
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
if st.button("படம் உருவாக்கவும்"):
|
44 |
if not user_input.strip():
|
45 |
-
st.warning("
|
46 |
return
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
st.success(f"மொழிபெயர்ப்பு (ஆங்கிலம்): {translated_text}")
|
52 |
|
53 |
pipe = load_diffusion_model()
|
54 |
-
|
55 |
-
st.image(
|
56 |
|
57 |
if __name__ == "__main__":
|
58 |
main()
|
|
|
1 |
# app.py
|
2 |
import streamlit as st
|
3 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
import torch
|
6 |
|
7 |
+
# Load translation model
|
8 |
@st.cache_resource
|
9 |
def load_translation_model():
|
10 |
+
model_name = "nithalis/tamil_translation_t5"
|
11 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
12 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
13 |
return tokenizer, model
|
14 |
|
15 |
+
# Load faster image model
|
16 |
@st.cache_resource
|
17 |
def load_diffusion_model():
|
18 |
pipe = StableDiffusionPipeline.from_pretrained(
|
19 |
+
"stabilityai/stable-diffusion-2-base",
|
20 |
+
torch_dtype=torch.float32
|
21 |
+
).to("cpu")
|
|
|
22 |
return pipe
|
23 |
|
24 |
+
# Translate Tamil to English
|
25 |
+
def translate_tamil_to_english(text, tokenizer, model):
|
26 |
+
prompt = f"translate Tamil to English: {text}"
|
27 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
28 |
with torch.no_grad():
|
29 |
+
output = model.generate(**inputs, max_length=128)
|
30 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
31 |
|
32 |
+
# Main Streamlit app
|
33 |
def main():
|
34 |
+
st.set_page_config(page_title="தமிழ்→Image", layout="centered")
|
35 |
+
st.title("🌐 தமிழ் → ஆங்கிலம் → படம்")
|
|
|
36 |
|
37 |
+
user_input = st.text_area("உங்கள் உரையை தமிழ் எழுத்துப்படியாக உள்ளிடவும்:", height=150)
|
38 |
|
39 |
+
if st.button("உருவாக்கு"):
|
|
|
|
|
40 |
if not user_input.strip():
|
41 |
+
st.warning("உரை பதிவுசெய்யவும்")
|
42 |
return
|
43 |
+
with st.spinner("மொழிபெயர்ப்பு மற்றும் பட உருவாக்கம்..."):
|
44 |
+
tok, mdl = load_translation_model()
|
45 |
+
translation = translate_tamil_to_english(user_input, tok, mdl)
|
46 |
+
st.success(f"ஆங்கில மொழிபெயர்ப்பு: {translation}")
|
|
|
47 |
|
48 |
pipe = load_diffusion_model()
|
49 |
+
img = pipe(translation, num_inference_steps=25, guidance_scale=7.5).images[0]
|
50 |
+
st.image(img, caption="உருவாக்கப்பட்ட படம்", use_column_width=True)
|
51 |
|
52 |
if __name__ == "__main__":
|
53 |
main()
|