CoolFace
Apppublic

bjong/blessed_Text_summarization_and_lingual_model

sourceHugging Faceafl-3.0updated 2y agoView on Hugging Face
0likes
app.py176 linesDownload Raw Back to root
1import streamlit as st
2from summarizer import summarize_text, summarize_pdf, read_pdf, extractive_summary, abstractive_summary, load_model
3from sentiment_analysis import perform_sentiment_analysis
4from translator import translate_english_to_shona, translate_shona_to_english
5from io import BytesIO
6from reportlab.lib.pagesizes import letter
7from reportlab.pdfgen import canvas
8
9# Set page title
10st.set_page_config(page_title="Document Processor")
11
12# Load the models, tokenizer, and device once using Streamlit caching
13@st.cache_resource
14def get_model():
15    return load_model()
16
17model, tokenizer, device = get_model()
18
19# Navigation menu
20menu = ["Upload Document", "Summarize Document", "Sentiment Analysis", "Translation (Standalone)"]
21choice = st.sidebar.radio("Navigation", menu, key="main_navigation")
22
23# Functions for different steps
24def upload_document():
25    st.subheader("Upload Document or Input Text")
26    option = st.radio("Choose input method", ("Upload a file", "Input text"), key="upload_option")
27
28    if option == "Upload a file":
29        uploaded_file = st.file_uploader("Choose a file", type=["txt", "pdf"])
30        if uploaded_file is not None:
31            if uploaded_file.type == "application/pdf":
32                text = read_pdf(uploaded_file)
33            else:
34                text = uploaded_file.read().decode("utf-8")
35            st.success("File uploaded successfully!")
36            return text
37    elif option == "Input text":
38        text = st.text_area("Input your text here", key="upload_text_area")
39        if st.button("Submit Text", key="submit_text_button"):
40            if text:
41                st.success("Text input successfully!")
42                return text
43    return None
44
45def summarize_document_ui(text):
46    st.subheader("Summarize Document")
47    summary_type = st.radio("Choose summarization type", ("Extractive_base_model", "Abstractive_base_model", "Model"), key="summarize_type")
48    if st.button("Summarize", key="summarize_button"):
49        try:
50            if summary_type == "Extractive_base_model":
51                summary = extractive_summary(text)
52            elif summary_type == "Abstractive_base_model":
53                summary = abstractive_summary(text)
54            elif summary_type == "Model":
55                summary = summarize_text(text, model, tokenizer, device)
56            st.session_state.summary = summary
57            st.session_state.show_download = True
58        except IndexError:
59            st.error("The input text is too long for the summarization model to process. Please try a shorter text.")
60
61def generate_pdf(summary):
62    buffer = BytesIO()
63    c = canvas.Canvas(buffer, pagesize=letter)
64    c.drawString(100, 750, "Summary")
65    text_object = c.beginText(40, 730)
66    for line in summary.split("\n"):
67        text_object.textLine(line)
68    c.drawText(text_object)
69    c.showPage()
70    c.save()
71    buffer.seek(0)
72    return buffer
73
74def generate_download_buttons(summary):
75    pdf_buffer = generate_pdf(summary)
76
77    doc_buffer = BytesIO()
78    doc_buffer.write(summary.encode())
79    doc_buffer.seek(0)
80
81    txt_buffer = BytesIO()
82    txt_buffer.write(summary.encode())
83    txt_buffer.seek(0)
84
85    download_choice = st.selectbox("Select format to download", ["", "PDF", "DOC", "TXT"], key="download_choice")
86
87    if download_choice == "PDF":
88        st.download_button(label="Download as PDF", data=pdf_buffer, file_name="summary.pdf", mime="application/pdf")
89    elif download_choice == "DOC":
90        st.download_button(label="Download as DOC", data=doc_buffer, file_name="summary.doc", mime="application/msword")
91    elif download_choice == "TXT":
92        st.download_button(label="Download as TXT", data=txt_buffer, file_name="summary.txt", mime="text/plain")
93
94def sentiment_analysis_ui(text):
95    st.subheader("Sentiment Analysis")
96    if st.button("Analyze Sentiment", key="analyze_sentiment_button"):
97        sentiment_df, negative_sentences, total_negative_sentences = perform_sentiment_analysis(text)
98        st.write(sentiment_df)
99        
100        st.subheader("Sentences with Negative Sentiment")
101        if negative_sentences:
102            for sentence in negative_sentences:
103                st.write(sentence)
104            st.write(f"Total number of sentences with negative sentiment: {total_negative_sentences}")
105        else:
106            st.write("No sentences identified with negative sentiment.")
107
108def translate_document_ui(text):
109    st.subheader("Translate Document")
110    if st.button("Translate to Shona", key="translate_button"):
111        translation = translate_english_to_shona(text)
112        st.write(translation)
113
114def standalone_translation_ui():
115    st.subheader("Standalone Translation")
116    option = st.radio("Choose input method", ("Upload a file", "Input text"), key="standalone_translation_option")
117    if option == "Upload a file":
118        uploaded_file = st.file_uploader("Choose a file", type=["txt"], key="standalone_translation_file")
119        if uploaded_file is not None:
120            text = uploaded_file.read().decode("utf-8")
121            st.success("File uploaded successfully!")
122            translation_direction = st.radio("Translation Direction", ("English to Shona", "Shona to English"), key="standalone_translation_direction")
123            if st.button("Translate", key="standalone_translate_button"):
124                if translation_direction == "English to Shona":
125                    translation = translate_english_to_shona(text)
126                else:
127                    translation = translate_shona_to_english(text)
128                st.write(translation)
129    elif option == "Input text":
130        text = st.text_area("Input your text here", key="standalone_translation_text")
131        translation_direction = st.radio("Translation Direction", ("English to Shona", "Shona to English"), key="standalone_translation_direction_text")
132        if st.button("Translate", key="standalone_translate_button_text"):
133            if translation_direction == "English to Shona":
134                translation = translate_english_to_shona(text)
135            else:
136                translation = translate_shona_to_english(text)
137            st.write(translation)
138
139# Main application
140def main():
141    if choice == "Upload Document":
142        text = upload_document()
143        if text:
144            st.session_state.text = text
145            st.session_state.summary = None
146            st.session_state.show_download = False
147
148    elif choice == "Summarize Document":
149        if "text" not in st.session_state:
150            st.warning("Please upload a document or input text first.")
151        else:
152            summarize_document_ui(st.session_state.text)
153            if "show_download" in st.session_state and st.session_state.show_download:
154                st.subheader("Download Summary")
155                st.write(st.session_state.summary)
156                generate_download_buttons(st.session_state.summary)
157                sentiment_analysis_ui(st.session_state.summary)
158                translate_document_ui(st.session_state.summary)
159
160    elif choice == "Sentiment Analysis":
161        st.subheader("Upload Document file for Sentiment Analysis")
162        uploaded_file = st.file_uploader("Choose a file", type=["txt", "pdf"], key="sentiment_analysis_file")
163        if uploaded_file is not None:
164            if uploaded_file.type == "application/pdf":
165                text = read_pdf(uploaded_file)
166            else:
167                text = uploaded_file.read().decode("utf-8")
168            st.success("File uploaded successfully!")
169            sentiment_analysis_ui(text)
170
171    elif choice == "Translation (Standalone)":
172        standalone_translation_ui()
173
174if __name__ == "__main__":
175    main()
176