blazingbunny commited on
Commit
089e8f2
1 Parent(s): 259eeb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -42
app.py CHANGED
@@ -2,16 +2,34 @@ import json
2
  import streamlit as st
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
5
- import requests
6
- from bs4 import BeautifulSoup
7
- import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def sample_classify_text(text_content):
10
  try:
 
11
  service_account_info = json.loads(st.secrets["google_nlp"])
12
  except json.JSONDecodeError:
13
  st.error("Invalid or empty JSON in 'google_nlp' secret.")
14
- return []
15
 
16
  credentials = service_account.Credentials.from_service_account_info(
17
  service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
@@ -32,44 +50,17 @@ def sample_classify_text(text_content):
32
  }
33
  )
34
 
35
- return [(category.name, category.confidence) for category in response.categories]
36
-
37
- def fetch_urls_from_sitemap(sitemap_url):
38
- response = requests.get(sitemap_url)
39
- soup = BeautifulSoup(response.content, 'xml')
40
- urls = [element.text for element in soup.find_all('loc')]
41
- return urls
42
-
43
- def fetch_text_from_url(url):
44
- response = requests.get(url)
45
- soup = BeautifulSoup(response.content, 'html.parser')
46
- text = soup.get_text()
47
- normalized_text = ' '.join(text.split())
48
- return normalized_text
49
-
50
- def main():
51
- st.title("URL Sitemap Analyzer")
52
 
53
- sitemap_url = st.text_input("Enter the sitemap URL")
54
- if st.button("Analyze Sitemap"):
55
- if sitemap_url:
56
- st.write("Fetching URLs from sitemap...")
57
- urls = fetch_urls_from_sitemap(sitemap_url)
58
- st.write(f"Found {len(urls)} URLs")
59
-
60
- results = []
61
- for url in urls:
62
- st.write(f"Analyzing URL: {url}")
63
- text_content = fetch_text_from_url(url)
64
- categories = sample_classify_text(text_content)
65
- for category, confidence in categories:
66
- results.append({"URL": url, "Category": category, "Confidence": confidence})
67
 
68
- if results:
69
- df = pd.DataFrame(results)
70
- df.to_csv('url_classification_results.csv', index=False)
71
- st.write("Analysis complete. Results saved to 'url_classification_results.csv'")
72
- st.dataframe(df)
73
 
74
- if __name__ == "__main__":
75
- main()
 
 
2
  import streamlit as st
3
  from google.oauth2 import service_account
4
  from google.cloud import language_v1
5
+
6
+ # About This Tool Section
7
+ st.sidebar.title("About This Tool")
8
+
9
+ # Descriptive Introduction
10
+ st.sidebar.markdown("#### Descriptive Introduction")
11
+ st.sidebar.markdown("The Google Cloud NLP Text Classifier is designed to analyze and classify a given text into predefined categories. It leverages Google's Natural Language Processing technology to achieve this. The tool is particularly useful for understanding the context or subject matter of the text.")
12
+
13
+ # Step-by-Step Guide
14
+ st.sidebar.markdown("#### Step-by-Step Guide")
15
+ st.sidebar.markdown("""
16
+ 1. **Open the Tool**: Navigate to the URL where the Google Cloud NLP Text Classifier is hosted.
17
+ 2. **User Input**: Upon arriving at the homepage, you will find a text area labeled 'Enter text to classify.' Here, you can paste or type the text that you want to classify.
18
+ 3. **Analyze**: After entering the text, click the button labeled 'Analyze'. The tool will then process the text and classify it into various categories.
19
+ 4. **View Results**: Once the analysis is complete, the tool will display the classified categories and their corresponding confidence scores.
20
+ """)
21
+
22
+ # Google Cloud NLP Text Classifier Description
23
+ st.title("Google Cloud NLP Text Classifier")
24
+ st.write("This tool is designed to classify text into predefined categories using Google's Natural Language Processing (NLP) technology. By using this tool, you can gain insights into the themes or subjects that the text covers, which can be especially useful for tasks like content analysis, market research, and SEO (Search Engine Optimization).")
25
 
26
  def sample_classify_text(text_content):
27
  try:
28
+ # Assuming service_account_info is set in your Streamlit secrets
29
  service_account_info = json.loads(st.secrets["google_nlp"])
30
  except json.JSONDecodeError:
31
  st.error("Invalid or empty JSON in 'google_nlp' secret.")
32
+ return
33
 
34
  credentials = service_account.Credentials.from_service_account_info(
35
  service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
 
50
  }
51
  )
52
 
53
+ st.write(f"### We found {len(response.categories)} categories")
54
+ st.write("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ for category in response.categories:
57
+ st.write(f"Category Name: {category.name}")
58
+ st.write(f"Confidence Score: {category.confidence}")
59
+ st.write("---")
 
 
 
 
 
 
 
 
 
 
60
 
61
+ # User input for text analysis
62
+ user_input = st.text_area("Enter text to classify")
 
 
 
63
 
64
+ if st.button("Analyze"):
65
+ if user_input:
66
+ sample_classify_text(user_input)