codewithdark commited on
Commit
b750bd2
1 Parent(s): aaa006d

Upload summarize.py

Browse files
Files changed (1) hide show
  1. pages/summarize.py +174 -0
pages/summarize.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+ from io import StringIO
4
+ import nltk
5
+ from nltk.tokenize import word_tokenize, sent_tokenize
6
+ from nltk.corpus import stopwords
7
+ import readtime
8
+ import textstat
9
+ from transformers import pipeline
10
+
11
+ st.set_page_config(page_title="DarkGPT",
12
+ page_icon="🤖",
13
+ layout="wide",
14
+ initial_sidebar_state="expanded"
15
+ )
16
+
17
+ # Function to summarize text using BART model
18
+ def summarize_text(input_text):
19
+ summarizer = pipeline("summarization")
20
+ summarized_text = summarizer(input_text, max_length=10050, min_length=50, do_sample=False)[0]['summary_text']
21
+ return summarized_text
22
+
23
+ # Function to analyze text and return metrics
24
+ def analyze_text(input_text):
25
+ nltk.download('punkt')
26
+ tokenized_words = word_tokenize(input_text)
27
+ reading_time = readtime.of_text(input_text)
28
+ text_complexity = textstat.flesch_reading_ease(input_text)
29
+ lexical_richness = len(set(tokenized_words)) / len(tokenized_words)
30
+ num_sentences = len(sent_tokenize(input_text))
31
+ analysis_results = {
32
+ 'reading_time': reading_time,
33
+ 'text_complexity': text_complexity,
34
+ 'lexical_richness': lexical_richness,
35
+ 'num_sentences': num_sentences
36
+ }
37
+ return analysis_results
38
+
39
+ # Function to display the homepage
40
+ def display_homepage():
41
+ st.markdown("<h1 style='text-align: center; color: white; font-size:28px;'>Welcome to DarkGPT!🧨</h1>", unsafe_allow_html=True)
42
+ st.markdown("<h3 style='text-align: center; font-size:56px;'<p>🤖</p></h3>", unsafe_allow_html=True)
43
+ st.markdown("<h3 style='text-align: center; color: grey; font-size:20px;'>DarkGPT: Your AI text assistant for quick summarization and analysis. Summarize text, analyze complexity, and get insights instantly.!</h3>", unsafe_allow_html=True)
44
+
45
+ st.markdown('___')
46
+ st.markdown("<h3 style='text-align: left; color:#F63366; font-size:18px;'><b>What is this App about?<b></h3>", unsafe_allow_html=True)
47
+ st.write("The provided code is a Streamlit web application named 'DarkGPT' that allows users to summarize and analyze text. Here's an overview of each page and the footer mentioning the author:")
48
+ st.markdown("<h3 style='text-align: left; color:#F63366; font-size:18px;'><b>Display Functionality:<b></h3>", unsafe_allow_html=True)
49
+
50
+ st.markdown("Homepage:\n"
51
+ "- Displays a welcome message and introduction to the DarkGPT app.\n"
52
+ "- Provides information about the purpose and target audience of the app.\n"
53
+ "- Users are encouraged to explore the app's features and contribute to its open-source development on GitHub.")
54
+
55
+ st.markdown("Summarization Page:\n"
56
+ "- Allows users to input text or upload a file for summarization.\n"
57
+ "- Users can choose between inputting text directly or uploading a file.\n"
58
+ "- Provides an example text for users to test.\n"
59
+ "- After input, users can click the 'Summarize' button to generate a summary using the BART model.\n"
60
+ "- Error messages are displayed if input text length requirements are not met.")
61
+
62
+ st.markdown("Analysis Page:\n"
63
+ "- Similar to the Summarization page, but focuses on analyzing text.\n"
64
+ "- Users can input text or upload a file for analysis.\n"
65
+ "- Provides an example text for users to test.\n"
66
+ "- After input, users can click the 'Analyze' button to generate various metrics such as reading time, text complexity, lexical richness, and number of sentences.\n"
67
+ "- Error messages are displayed if input text length requirements are not met.")
68
+
69
+ footer = '''
70
+ <footer style="text-align: center; margin-top: 50px; font-family: Arial, sans-serif; color: #555;">
71
+ <p>Developed by DarkCoder</p>
72
+ <p>
73
+ <a href="https://github.com/codewithdark-git" target="_blank" style="text-decoration: none; color: #007bff; margin-right: 10px;">GitHub</a>
74
+ <a href="https://www.linkedin.com/in/codewithdark/" target="_blank" style="text-decoration: none; color: #007bff; margin-right: 10px;">Linkedin</a>
75
+ <a href="https://www.facebook.com/codewithdark.fb/" target="_blank" style="text-decoration: none; color: #007bff;">Facebook</a>
76
+ </p>
77
+ </footer>
78
+ '''
79
+
80
+ # Display the footer
81
+ st.markdown(footer, unsafe_allow_html=True)
82
+
83
+ # Function to display the text summarization page
84
+ def display_summarization_page():
85
+ st.markdown("<h4 style='text-align: center; color:grey;'>Accelerate knowledge with DarkGPT &#129302;</h4>", unsafe_allow_html=True)
86
+ st.text('')
87
+ st.markdown("<h3 style='text-align: left; color:#F63366; font-size:28px;'>Summarize</h3>", unsafe_allow_html=True)
88
+ st.text('')
89
+
90
+ source = st.radio("How would you like to start? Choose an option below", ("I want to input some text", "I want to upload a file"))
91
+ st.text('')
92
+
93
+ s_example = "Artificial intelligence (AI) is intelligence demonstrated by machines, as opposed to the natural intelligence displayed by humans or animals. Leading AI textbooks define the field as the study of 'intelligent agents': any system that perceives its environment and takes actions that maximize its chance of achieving its goals. Some popular accounts use the term 'artificial intelligence' to describe machines that mimic cognitive functions that humans associate with the human mind, such as learning and problem solving, however this definition is rejected by major AI researchers. AI applications include advanced web search engines, recommendation systems (used by YouTube, Amazon and Netflix), understanding human speech (such as Siri or Alexa), self-driving cars (such as Tesla), and competing at the highest level in strategic game systems (such as chess and Go). As machines become increasingly capable, tasks considered to require intelligence are often removed from the definition of AI, a phenomenon known as the AI effect. For instance, optical character recognition is frequently excluded from things considered to be AI, having become a routine technology."
94
+
95
+ if source == 'I want to input some text':
96
+ input_su = st.text_area("Use the example below or input your own text in English (between 1,000 and 10,000 characters)", value=s_example, max_chars=10000, height=330)
97
+ if st.button('Summarize'):
98
+ if len(input_su) < 50:
99
+ st.error('Please enter a text in English of minimum 1,000 characters')
100
+ else:
101
+ # Call the summarization function
102
+ summarized_text = summarize_text(input_su)
103
+ st.success(summarized_text)
104
+ st.balloons()
105
+
106
+ if source == 'I want to upload a file':
107
+ file = st.file_uploader('Upload your file here', type=['txt'])
108
+ if file is not None:
109
+ stringio = StringIO(file.getvalue().decode("utf-8"))
110
+ string_data = stringio.read()
111
+ if len(string_data) < 50 or len(string_data) > 10000:
112
+ st.error('Please upload a file between 50 and 10,000 characters')
113
+ else:
114
+ # Call the summarization function
115
+ summarized_text = summarize_text(string_data)
116
+ st.success(summarized_text)
117
+ st.balloons()
118
+
119
+ # Function to display the text analysis page
120
+ def display_analysis_page():
121
+ st.markdown("<h4 style='text-align: center; color:grey;'>Accelerate knowledge with DarkGPT &#129302;</h4>", unsafe_allow_html=True)
122
+ st.text('')
123
+ st.markdown("<h3 style='text-align: left; color:#F63366; font-size:28px;'>Analyze</h3>", unsafe_allow_html=True)
124
+ st.text('')
125
+
126
+ a_example = "Artificial intelligence (AI) is intelligence demonstrated by machines, as opposed to the natural intelligence displayed by humans or animals. Leading AI textbooks define the field as the study of 'intelligent agents': any system that perceives its environment and takes actions that maximize its chance of achieving its goals. Some popular accounts use the term 'artificial intelligence' to describe machines that mimic cognitive functions that humans associate with the human mind, such as learning and problem solving, however this definition is rejected by major AI researchers. AI applications include advanced web search engines, recommendation systems (used by YouTube, Amazon and Netflix), understanding human speech (such as Siri or Alexa), self-driving cars (such as Tesla), and competing at the highest level in strategic game systems (such as chess and Go). As machines become increasingly capable, tasks considered to require intelligence are often removed from the definition of AI, a phenomenon known as the AI effect. For instance, optical character recognition is frequently excluded from things considered to be AI, having become a routine technology."
127
+
128
+ source = st.radio("How would you like to start? Choose an option below", ("I want to input some text", "I want to upload a file"))
129
+ st.text('')
130
+
131
+ if source == 'I want to input some text':
132
+ input_me = st.text_area("Use the example below or input your own text in English (maximum of 10,000 characters)", max_chars=10000, value=a_example, height=330)
133
+ if st.button('Analyze'):
134
+ if len(input_me) > 10000:
135
+ st.error('Please enter a text in English of maximum 10,000 characters')
136
+ else:
137
+ # Call the text analysis function
138
+ analysis_results = analyze_text(input_me)
139
+ st.write('Reading Time:', analysis_results['reading_time'])
140
+ st.write('Text Complexity:', analysis_results['text_complexity'])
141
+ st.write('Lexical Richness:', analysis_results['lexical_richness'])
142
+ st.write('Number of Sentences:', analysis_results['num_sentences'])
143
+ st.balloons()
144
+
145
+ if source == 'I want to upload a file':
146
+ file = st.file_uploader('Upload your file here', type=['txt'])
147
+ if file is not None:
148
+ stringio = StringIO(file.getvalue().decode("utf-8"))
149
+ string_data = stringio.read()
150
+ if len(string_data) > 10000:
151
+ st.error('Please upload a file of maximum 10,000 characters')
152
+ else:
153
+ # Call the text analysis function
154
+ analysis_results = analyze_text(string_data)
155
+ st.write('Reading Time:', analysis_results['reading_time'])
156
+ st.write('Text Complexity:', analysis_results['text_complexity'])
157
+ st.write('Lexical Richness:', analysis_results['lexical_richness'])
158
+ st.write('Number of Sentences:', analysis_results['num_sentences'])
159
+ st.balloons()
160
+
161
+ # Main function to run the Streamlit app
162
+ def main():
163
+ st.sidebar.header('DarkGPT, I want to :crystal_ball:')
164
+ nav = st.sidebar.radio('', ['Go to homepage', 'Summarize text', 'Analyze text'])
165
+
166
+ if nav == 'Go to homepage':
167
+ display_homepage()
168
+ elif nav == 'Summarize text':
169
+ display_summarization_page()
170
+ elif nav == 'Analyze text':
171
+ display_analysis_page()
172
+
173
+ if __name__ == "__main__":
174
+ main()