ahmadmac commited on
Commit
a567818
1 Parent(s): 7cc6f1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -32
app.py CHANGED
@@ -1,32 +1,50 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
-
4
-
5
- def summarize_text(input_text):
6
- summarize=pipeline("summarization", model="Falconsai/text_summarization")
7
- summary= summarize(input_text,max_length=1000,min_length=128,do_sample=False)
8
- return summary[0]['summary_text']
9
- #summary_text=summarize_text(text)
10
- def translate_urdu(english_summary):
11
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
12
- urdu_text=translator(english_summary)
13
- urdu_summary=urdu_text[0]['translation_text']
14
- return urdu_text[0]['translation_text']
15
-
16
- def main():
17
-
18
- st.title(" Text Summarization and Translation")
19
- input_text= st.text_area("Enter text to summarize:","")
20
- if st.button("Summarize"):
21
- if input_text:
22
- st.write("English Summary:")
23
- english_summary=summarize_text(input_text)
24
- st.write(english_summary)
25
- if st.button("Translate English to Urdu"):
26
- urdu_translation=translate_urdu(english_summary)
27
- st.write("Urdu Translation")
28
- st.write(urdu_translation)
29
- st.write("English summary text")
30
- st.write(english_summary)
31
- if __name__=="__main__":
32
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()