File size: 3,902 Bytes
32dff32 c13fd5d d9f1d02 8a890e9 1e7e8be c13fd5d 9748ff4 c13fd5d 9682bdc c13fd5d c85d7fb c13fd5d 2e598b9 aab69e8 c13fd5d b29e8af c13fd5d c6a64cf c13fd5d 9748ff4 8a890e9 1e7e8be e7a30ef d50e4bc 9682bdc d790243 1e7e8be 9682bdc b47f89f 9682bdc 1e7e8be 9682bdc 1e7e8be 9682bdc 1e7e8be d50e4bc 8de644b 1e7e8be d50e4bc 8de644b d50e4bc 8a890e9 1e7e8be 8a890e9 c13fd5d d50e4bc 8a890e9 9682bdc 1e7e8be d9f1d02 9a6d2b3 4a3dc04 9a6d2b3 4a3dc04 9a6d2b3 4a3dc04 9a6d2b3 4a3dc04 9682bdc 4a3dc04 d50e4bc 4a3dc04 8a890e9 d50e4bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import streamlit as st
import nltk
from nltk.corpus import stopwords
from nltk.cluster.util import cosine_distance
import numpy as np
import networkx as nx
from googletrans import Translator
# Download NLTK resources
nltk.download('punkt')
nltk.download('stopwords')
def read_article(article):
sentences = nltk.sent_tokenize(article)
sentences = [sentence for sentence in sentences if len(sentence) > 10] # filter very short sentences
return sentences
def sentence_similarity(sent1, sent2, stopwords):
words1 = nltk.word_tokenize(sent1)
words2 = nltk.word_tokenize(sent2)
words1 = [word.lower() for word in words1 if word.isalnum()]
words2 = [word.lower() for word in words2 if word.isalnum()]
all_words = list(set(words1 + words2))
vector1 = [0] * len(all_words)
vector2 = [0] * len(all_words)
for word in words1:
if word in stopwords:
continue
vector1[all_words.index(word)] += 1
for word in words2:
if word in stopwords:
continue
vector2[all_words.index(word)] += 1
return 1 - cosine_distance(vector1, vector2)
def build_similarity_matrix(sentences, stopwords):
similarity_matrix = np.zeros((len(sentences), len(sentences)))
for i in range(len(sentences)):
for j in range(len(sentences)):
if i == j:
continue
similarity_matrix[i][j] = sentence_similarity(sentences[i], sentences[j], stopwords)
return similarity_matrix
def generate_summary(article, top_n=5):
sentences = read_article(article)
stop_words = set(stopwords.words('english'))
sentence_similarity_matrix = build_similarity_matrix(sentences, stop_words)
sentence_similarity_graph = nx.from_numpy_array(sentence_similarity_matrix)
scores = nx.pagerank(sentence_similarity_graph)
ranked_sentences = sorted(((scores[i], sentence) for i, sentence in enumerate(sentences)), reverse=True)
summary = " ".join([sentence for _, sentence in ranked_sentences[:top_n]])
return summary
# Set page configuration and styles
st.set_page_config(page_title="Article Summarizer", page_icon="βοΈ")
st.title("We sumerize and translate your articles")
st.markdown(
"""
<style>
.stApp {
background-color: orange;
color: #333;
}
.stButton button {
background-color: black;
color: white;
border-radius: 5px;
}
.stTextInput input {
color: #333;
border: 1px solid #008CBA;
border-radius: 5px;
}
.stText {
color: #333;
}
</style>
""",
unsafe_allow_html=True,
)
# Input text area
user_article = st.text_area("βοΈ Enter your article here:", height=100)
# Translation options
translate = st.checkbox("π Translate Summary")
if translate:
target_language = st.selectbox("π Select Target Language", ["πΊπΈ English", "π«π· French", "πͺπΈ Spanish", "π©πͺ German"])
# Summarize button
if st.button("Summarize"):
if user_article:
summary = generate_summary(user_article)
st.subheader("π Summary:")
st.write(summary)
# Translation logic
if translate:
if target_language == "πΊπΈ English":
target_language_code = "en"
elif target_language == "π«π· French":
target_language_code = "fr"
elif target_language == "πͺπΈ Spanish":
target_language_code = "es"
elif target_language == "π©πͺ German":
target_language_code = "de"
translator = Translator()
translated_summary = translator.translate(summary, dest=target_language_code)
st.subheader("π Translated Summary:")
st.write(translated_summary.text)
else:
st.warning("π« Please enter an article to summarize.") |