Phani1008 commited on
Commit
507b9f3
Β·
verified Β·
1 Parent(s): 38c2018

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -65
app.py CHANGED
@@ -1,89 +1,100 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
 
4
  # Custom CSS for styling
5
  st.markdown(
6
  """
7
  <style>
8
  .stApp {
9
- background: linear-gradient(120deg, #2C5364, #203A43, #0F2027);
10
- color: #F0F0F0;
11
  }
12
  .stTitle {
13
  text-align: center;
14
- color: #FFD700;
15
  }
16
- .css-1d391kg p, .css-1v0mbdj p {
17
- color: #F0F0F0;
18
- }
19
- .stButton > button:hover {
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("🧠 NLP Tool with Hugging Face", anchor=False)
30
-
31
- st.sidebar.subheader("πŸ” Explore NLP Features")
32
 
33
- # Dropdown to select NLP task
34
- nlp_task = st.sidebar.selectbox(
35
- "Select an NLP Task:",
36
- ["Text Classification", "Sentiment Analysis", "Question Answering", "Summarization", "Text Generation"]
37
  )
38
 
39
- # Input text area
40
- user_input = st.text_area("Enter your text here:", "", height=150)
41
-
42
- # Functionality for each task
43
- if user_input:
44
- if nlp_task == "Text Classification":
45
- st.subheader("πŸ“‹ Text Classification")
46
- classifier = pipeline("text-classification")
47
- result = classifier(user_input)
48
- st.write("**Classification Results:**")
49
- for res in result:
50
- st.write(f"- **Label**: {res['label']}, **Score**: {res['score']:.2f}")
51
-
52
- elif nlp_task == "Sentiment Analysis":
53
- st.subheader("😊 Sentiment Analysis")
54
- sentiment_analyzer = pipeline("sentiment-analysis")
55
- result = sentiment_analyzer(user_input)
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
- elif nlp_task == "Summarization":
69
- st.subheader("βœ‚οΈ Summarization")
70
- summarizer = pipeline("summarization")
71
- summary = summarizer(user_input, max_length=50, min_length=25, do_sample=False)
72
- st.write("**Summary:**")
73
- st.write(summary[0]['summary_text'])
 
 
 
 
 
 
 
74
 
75
- elif nlp_task == "Text Generation":
76
- st.subheader("πŸ“ Text Generation")
77
- generator = pipeline("text-generation")
78
- generated_text = generator(user_input, max_length=50, num_return_sequences=1)
79
- st.write("**Generated Text:**")
80
- st.write(generated_text[0]['generated_text'])
 
 
 
 
 
 
 
 
 
81
 
82
- # Space for connecting this app to another space
83
- st.sidebar.markdown("---")
84
- st.sidebar.subheader("πŸ”— Connect to Other Spaces")
85
- other_space_url = st.sidebar.text_input("Enter the URL of another Streamlit app or Hugging Face space:")
86
- if other_space_url:
87
- st.sidebar.markdown(f"[Go to Connected Space]({other_space_url})")
 
 
 
 
 
 
 
 
 
88
 
89
- st.sidebar.success("Explore NLP tasks or connect to another space!")
 
 
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!")