Ahmad-Moiz commited on
Commit
9682bdc
·
1 Parent(s): aab69e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
app.py CHANGED
@@ -19,7 +19,7 @@ def read_article(article):
19
  # Function to compute sentence similarity based on cosine similarity
20
  def sentence_similarity(sent1, sent2, stopwords):
21
  words1 = nltk.word_tokenize(sent1)
22
- words2 = nltk.word_tokenize (sent2)
23
 
24
  words1 = [word.lower() for word in words1 if word.isalnum()]
25
  words2 = [word.lower() for word in words2 if word.isalnum()]
@@ -43,8 +43,7 @@ def sentence_similarity(sent1, sent2, stopwords):
43
 
44
  # Function to create a similarity matrix of sentences
45
  def build_similarity_matrix(sentences, stopwords):
46
- similarity_matrix = np.zeros((len(sentences), len(sentences)))
47
-
48
 
49
  for i in range(len(sentences)):
50
  for j in range(len(sentences)):
@@ -67,15 +66,39 @@ def generate_summary(article, top_n=5):
67
  scores = nx.pagerank(sentence_similarity_graph)
68
 
69
  # Sort the sentences by score
70
- ranked_sentences = sorted(((scores[i], sentence) for i, sentence in enumerate(sentences)), reverse=True)
71
 
72
  # Get the top N sentences as the summary
73
  summary = " ".join([sentence for _, sentence in ranked_sentences[:top_n]])
74
  return summary
75
 
76
- # Streamlit web app
 
77
  st.title("Article Summarizer")
78
- user_article = st.text_area("Enter your article here:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  translate = st.checkbox("Translate Summary")
80
  target_language = st.selectbox("Select Target Language", ["English", "French", "Spanish", "German"])
81
 
@@ -84,7 +107,7 @@ if st.button("Summarize"):
84
  summary = generate_summary(user_article)
85
  st.subheader("Summary:")
86
  st.write(summary)
87
-
88
  if translate:
89
  if target_language == "English":
90
  target_language_code = "en"
@@ -94,10 +117,10 @@ if st.button("Summarize"):
94
  target_language_code = "es"
95
  elif target_language == "German":
96
  target_language_code = "de"
97
-
98
  translator = Translator()
99
  translated_summary = translator.translate(summary, dest=target_language_code)
100
  st.subheader("Translated Summary:")
101
  st.write(translated_summary.text)
102
  else:
103
- st.warning("Please enter an article to summarize.")
 
19
  # Function to compute sentence similarity based on cosine similarity
20
  def sentence_similarity(sent1, sent2, stopwords):
21
  words1 = nltk.word_tokenize(sent1)
22
+ words2 = nltk.word_tokenize(sent2)
23
 
24
  words1 = [word.lower() for word in words1 if word.isalnum()]
25
  words2 = [word.lower() for word in words2 if word.isalnum()]
 
43
 
44
  # Function to create a similarity matrix of sentences
45
  def build_similarity_matrix(sentences, stopwords):
46
+ similarity_matrix = np.zeros((len(sentences), len(sentences))
 
47
 
48
  for i in range(len(sentences)):
49
  for j in range(len(sentences)):
 
66
  scores = nx.pagerank(sentence_similarity_graph)
67
 
68
  # Sort the sentences by score
69
+ ranked_sentences = sorted(((scores[i], sentence) for i, sentence in enumerate sentences)), reverse=True)
70
 
71
  # Get the top N sentences as the summary
72
  summary = " ".join([sentence for _, sentence in ranked_sentences[:top_n]])
73
  return summary
74
 
75
+ # Streamlit web app with improved styling
76
+ st.set_page_config(page_title="Article Summarizer", page_icon="📄")
77
  st.title("Article Summarizer")
78
+
79
+ # Custom CSS to style the app
80
+ st.markdown(
81
+ """
82
+ <style>
83
+ .stApp {
84
+ background-color: #f7f7f7;
85
+ }
86
+ .stButton button {
87
+ background-color: #008CBA;
88
+ color: white;
89
+ }
90
+ .stTextInput input {
91
+ color: #333;
92
+ }
93
+ .stText {
94
+ color: #333;
95
+ }
96
+ </style>
97
+ """,
98
+ unsafe_allow_html=True,
99
+ )
100
+
101
+ user_article = st.text_area("Enter your article here:", height=200)
102
  translate = st.checkbox("Translate Summary")
103
  target_language = st.selectbox("Select Target Language", ["English", "French", "Spanish", "German"])
104
 
 
107
  summary = generate_summary(user_article)
108
  st.subheader("Summary:")
109
  st.write(summary)
110
+
111
  if translate:
112
  if target_language == "English":
113
  target_language_code = "en"
 
117
  target_language_code = "es"
118
  elif target_language == "German":
119
  target_language_code = "de"
120
+
121
  translator = Translator()
122
  translated_summary = translator.translate(summary, dest=target_language_code)
123
  st.subheader("Translated Summary:")
124
  st.write(translated_summary.text)
125
  else:
126
+ st.warning("Please enter an article to summarize.")