Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,57 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
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 |
-
|
31 |
-
|
32 |
-
|
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 |
-
#
|
40 |
text_gen = pipeline("text-generation", model="gpt2")
|
41 |
|
42 |
-
#
|
43 |
img_pipe = StableDiffusionPipeline.from_pretrained(
|
44 |
-
"
|
45 |
-
|
46 |
-
|
47 |
-
).to("cuda")
|
48 |
|
49 |
-
return
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
inputs = tokenizer(
|
54 |
-
|
55 |
-
return tokenizer.decode(
|
56 |
|
57 |
def main():
|
58 |
-
|
59 |
-
st.markdown(
|
60 |
-
|
61 |
-
|
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 |
-
|
69 |
-
|
70 |
-
return
|
71 |
|
72 |
-
with st.spinner("
|
73 |
-
|
74 |
-
|
|
|
75 |
|
76 |
-
with st.spinner("
|
77 |
-
|
78 |
-
|
|
|
79 |
|
80 |
-
with st.spinner("
|
81 |
-
|
82 |
-
|
|
|
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()
|