Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,50 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
st.
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
@st.cache(allow_output_mutation=True)
|
| 5 |
+
def load_summarizer():
|
| 6 |
+
return pipeline("summarization", model="Falconsai/text_summarization")
|
| 7 |
+
|
| 8 |
+
@st.cache(allow_output_mutation=True)
|
| 9 |
+
def load_translator():
|
| 10 |
+
return pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
|
| 11 |
+
|
| 12 |
+
def summarize_text(input_text, summarizer):
|
| 13 |
+
summary = summarizer(input_text, max_length=1000, min_length=128, do_sample=False)
|
| 14 |
+
return summary[0]['summary_text']
|
| 15 |
+
|
| 16 |
+
def translate_urdu(english_summary, translator):
|
| 17 |
+
urdu_text = translator(english_summary)
|
| 18 |
+
return urdu_text[0]['translation_text']
|
| 19 |
+
|
| 20 |
+
def main():
|
| 21 |
+
st.title("Text Summarization and Translation")
|
| 22 |
+
input_text = st.text_area("Enter text to summarize:", "")
|
| 23 |
+
|
| 24 |
+
if 'english_summary' not in st.session_state:
|
| 25 |
+
st.session_state.english_summary = ""
|
| 26 |
+
|
| 27 |
+
if 'urdu_translation' not in st.session_state:
|
| 28 |
+
st.session_state.urdu_translation = ""
|
| 29 |
+
|
| 30 |
+
summarizer = load_summarizer()
|
| 31 |
+
translator = load_translator()
|
| 32 |
+
|
| 33 |
+
if st.button("Summarize"):
|
| 34 |
+
if input_text:
|
| 35 |
+
english_summary = summarize_text(input_text, summarizer)
|
| 36 |
+
st.session_state.english_summary = english_summary
|
| 37 |
+
st.write("English Summary:")
|
| 38 |
+
st.write(english_summary)
|
| 39 |
+
|
| 40 |
+
if st.session_state.english_summary:
|
| 41 |
+
if st.button("Translate English to Urdu"):
|
| 42 |
+
urdu_translation = translate_urdu(st.session_state.english_summary, translator)
|
| 43 |
+
st.session_state.urdu_translation = urdu_translation
|
| 44 |
+
st.write("Urdu Translation:")
|
| 45 |
+
st.write(urdu_translation)
|
| 46 |
+
st.write("English Summary Text:")
|
| 47 |
+
st.write(st.session_state.english_summary)
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
main()
|