Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,100 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
# Custom CSS for styling
|
| 5 |
st.markdown(
|
| 6 |
"""
|
| 7 |
<style>
|
| 8 |
.stApp {
|
| 9 |
-
background:
|
| 10 |
-
color: #
|
| 11 |
}
|
| 12 |
.stTitle {
|
| 13 |
text-align: center;
|
| 14 |
-
color: #
|
| 15 |
}
|
| 16 |
-
.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
background-color: #FFD700;
|
| 21 |
-
color: #0F2027;
|
| 22 |
-
transform: scale(1.05);
|
| 23 |
}
|
| 24 |
</style>
|
| 25 |
""",
|
| 26 |
unsafe_allow_html=True,
|
| 27 |
)
|
| 28 |
|
| 29 |
-
st.title("
|
| 30 |
-
|
| 31 |
-
st.sidebar.subheader("π Explore NLP Features")
|
| 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 |
-
st.write("**Sentiment Analysis Results:**")
|
| 57 |
-
for res in result:
|
| 58 |
-
st.write(f"- **Label**: {res['label']}, **Score**: {res['score']:.2f}")
|
| 59 |
-
|
| 60 |
-
elif nlp_task == "Question Answering":
|
| 61 |
-
st.subheader("β Question Answering")
|
| 62 |
-
question = st.text_input("Ask a question about the provided text:")
|
| 63 |
-
if question:
|
| 64 |
-
qa_pipeline = pipeline("question-answering")
|
| 65 |
-
result = qa_pipeline(question=question, context=user_input)
|
| 66 |
-
st.write(f"**Answer:** {result['answer']}")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
st.
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import re
|
| 3 |
+
from collections import Counter
|
| 4 |
|
| 5 |
# Custom CSS for styling
|
| 6 |
st.markdown(
|
| 7 |
"""
|
| 8 |
<style>
|
| 9 |
.stApp {
|
| 10 |
+
background-color: #f4f4f9;
|
| 11 |
+
color: #333333;
|
| 12 |
}
|
| 13 |
.stTitle {
|
| 14 |
text-align: center;
|
| 15 |
+
color: #4CAF50;
|
| 16 |
}
|
| 17 |
+
.section-title {
|
| 18 |
+
font-size: 24px;
|
| 19 |
+
color: #FF5722;
|
| 20 |
+
text-decoration: underline;
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
</style>
|
| 23 |
""",
|
| 24 |
unsafe_allow_html=True,
|
| 25 |
)
|
| 26 |
|
| 27 |
+
st.title("π Explore Natural Language Processing")
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Sidebar navigation
|
| 30 |
+
menu = st.sidebar.radio(
|
| 31 |
+
"Explore Topics",
|
| 32 |
+
["Introduction to NLP", "Tokenization", "Word Frequency Analysis", "Stop Words"]
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Introduction to NLP
|
| 36 |
+
if menu == "Introduction to NLP":
|
| 37 |
+
st.header("π What is NLP?")
|
| 38 |
+
st.write(
|
| 39 |
+
"""
|
| 40 |
+
Natural Language Processing (NLP) is a field of artificial intelligence that focuses on the interaction
|
| 41 |
+
between computers and human language. The goal is to enable machines to understand, interpret, and generate
|
| 42 |
+
human language in a way that is meaningful.
|
| 43 |
+
|
| 44 |
+
NLP has a wide range of applications, including:
|
| 45 |
+
- **Text Classification:** Categorizing text into predefined categories (e.g., spam detection).
|
| 46 |
+
- **Sentiment Analysis:** Determining the sentiment (positive, negative, neutral) of a piece of text.
|
| 47 |
+
- **Machine Translation:** Translating text from one language to another (e.g., Google Translate).
|
| 48 |
+
- **Named Entity Recognition (NER):** Identifying entities like names, dates, or locations in text.
|
| 49 |
+
- **Question Answering:** Answering questions based on input text or documents.
|
| 50 |
+
"""
|
| 51 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Tokenization
|
| 54 |
+
elif menu == "Tokenization":
|
| 55 |
+
st.header("π Tokenization")
|
| 56 |
+
st.write(
|
| 57 |
+
"""
|
| 58 |
+
Tokenization is the process of breaking a text into smaller units, called tokens. These tokens can be words,
|
| 59 |
+
phrases, or characters.
|
| 60 |
+
"""
|
| 61 |
+
)
|
| 62 |
+
text_input = st.text_area("Enter some text to tokenize:", "Natural Language Processing is fascinating!")
|
| 63 |
+
if text_input:
|
| 64 |
+
tokens = text_input.split()
|
| 65 |
+
st.write("**Tokens:**", tokens)
|
| 66 |
|
| 67 |
+
# Word Frequency Analysis
|
| 68 |
+
elif menu == "Word Frequency Analysis":
|
| 69 |
+
st.header("π Word Frequency Analysis")
|
| 70 |
+
st.write(
|
| 71 |
+
"""
|
| 72 |
+
Word frequency analysis involves counting the occurrences of each word in a text. This is useful for
|
| 73 |
+
understanding the most common words in a document or dataset.
|
| 74 |
+
"""
|
| 75 |
+
)
|
| 76 |
+
text_input = st.text_area("Enter some text to analyze:", "NLP is fun. NLP can help with many applications.")
|
| 77 |
+
if text_input:
|
| 78 |
+
words = re.findall(r'\w+', text_input.lower())
|
| 79 |
+
word_counts = Counter(words)
|
| 80 |
+
st.write("**Word Frequency:**")
|
| 81 |
+
st.write(word_counts)
|
| 82 |
|
| 83 |
+
# Stop Words
|
| 84 |
+
elif menu == "Stop Words":
|
| 85 |
+
st.header("π« Stop Words")
|
| 86 |
+
st.write(
|
| 87 |
+
"""
|
| 88 |
+
Stop words are common words that usually carry less meaning and are often removed from text analysis
|
| 89 |
+
(e.g., 'is', 'and', 'the'). This helps in focusing on the meaningful parts of the text.
|
| 90 |
+
"""
|
| 91 |
+
)
|
| 92 |
+
text_input = st.text_area("Enter some text to remove stop words:", "The quick brown fox jumps over the lazy dog.")
|
| 93 |
+
stop_words = {"the", "is", "and", "in", "to", "a", "of", "on", "for"}
|
| 94 |
+
if text_input:
|
| 95 |
+
words = text_input.split()
|
| 96 |
+
filtered_words = [word for word in words if word.lower() not in stop_words]
|
| 97 |
+
st.write("**Filtered Text:**", " ".join(filtered_words))
|
| 98 |
|
| 99 |
+
# Footer
|
| 100 |
+
st.sidebar.info("Select a topic to learn about NLP!")
|