nlpblogs commited on
Commit
ee3e666
·
verified ·
1 Parent(s): eb162f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+
4
+ from selenium import webdriver
5
+ from selenium.webdriver.common.by import By
6
+ from selenium.webdriver.chrome.options import Options
7
+ from webdriver_manager.chrome import ChromeDriverManager
8
+ from selenium.webdriver.chrome.service import Service
9
+ from selenium.webdriver.chrome.service import Service as ChromeService
10
+ from webdriver_manager.core.os_manager import ChromeType
11
+
12
+ import time
13
+ import sys
14
+ import re
15
+ import transformers
16
+ import pandas as pd
17
+ import torch
18
+ from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
19
+ import io
20
+ import plotly.express as px
21
+ import zipfile
22
+ from streamlit_extras.stylable_container import stylable_container
23
+
24
+
25
+ # sidebar
26
+ with st.sidebar:
27
+ with stylable_container(
28
+ key="test_button",
29
+ css_styles="""
30
+ button {
31
+ background-color: yellow;
32
+ border: 1px solid black;
33
+ padding: 5px;
34
+ color: black;
35
+ }
36
+ """,
37
+ ):
38
+ st.button("DEMO APP")
39
+
40
+
41
+ expander = st.expander("**Important notes on the Google Maps Reviews Sentiment Analysis App**")
42
+ expander.write('''
43
+
44
+
45
+ **How to Use**
46
+ This app works with the URL of the Google Maps Reviews. Paste the URL and press the 'Sentiment Analysis' button to perform sentiment analysis on your Google Maps Reviews.
47
+
48
+
49
+ **Usage Limits**
50
+ You can perform sentiment analysis on Google Maps Reviews up to 5 times.
51
+
52
+
53
+ **Subscription Management**
54
+ This demo app offers a one-day subscription, expiring after 24 hours. If you are interested in building your own Google Maps Reviews Sentiment Analysis Web App, we invite you to explore our NLP Web App Store on our website. You can select your desired features, place your order, and we will deliver your custom app in five business days. If you wish to delete your Account with us, please contact us at [email protected]
55
+
56
+
57
+ **Customization**
58
+ To change the app's background color to white or black, click the three-dot menu on the right-hand side of your app, go to Settings and then Choose app theme, colors and fonts.
59
+
60
+
61
+ **File Handling and Errors**
62
+ For any errors or inquiries, please contact us at [email protected]
63
+
64
+
65
+
66
+ ''')
67
+
68
+ tokenizer = DistilBertTokenizer.from_pretrained("tabularisai/robust-sentiment-analysis")
69
+ model = DistilBertForSequenceClassification.from_pretrained("tabularisai/robust-sentiment-analysis")
70
+
71
+
72
+
73
+ def scroll_and_check_for_new_reviews(driver, current_review_count):
74
+ """Scrolls down the page and checks if new reviews have loaded."""
75
+ try:
76
+ last_review = driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf')[-1]
77
+ driver.execute_script("arguments[0].scrollIntoView(true);", last_review)
78
+ time.sleep(3) # Increased sleep time to allow for loading
79
+ new_review_count = len(driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf'))
80
+ return new_review_count > current_review_count
81
+ except Exception as e:
82
+ st.error(f"Error during scrolling: {e}")
83
+ return False
84
+
85
+ def scrape_google_reviews(url):
86
+ """Scrapes Google reviews from the given URL and performs sentiment analysis."""
87
+ try:
88
+ options = Options()
89
+ options.add_argument("--headless")
90
+ options.add_argument("--disable-gpu")
91
+ options.add_argument("--no-sandbox")
92
+ options.add_argument("--disable-dev-shm-usage")
93
+ options.add_argument("--start-maximized")
94
+ service = Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
95
+ driver = webdriver.Chrome(service=service, options=options)
96
+ driver.get(url)
97
+
98
+
99
+
100
+ current_review_count = 0
101
+ while scroll_and_check_for_new_reviews(driver, current_review_count):
102
+ current_review_count = len(driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf'))
103
+ st.write(f"Total reviews loaded: {current_review_count}")
104
+
105
+
106
+
107
+ reviews = driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf')
108
+ review_data = []
109
+ for review_elem in reviews:
110
+ try:
111
+ reviewer_name = review_elem.find_element(By.CSS_SELECTOR, '.d4r55').text.strip()
112
+ except Exception:
113
+ reviewer_name = 'No name'
114
+ try:
115
+ review_text = review_elem.find_element(By.CSS_SELECTOR, '.wiI7pd').text.strip()
116
+ except Exception:
117
+ review_text = 'No review text'
118
+ rating = 0
119
+ try:
120
+ reviews_element = review_elem.find_element(By.CSS_SELECTOR, "span[role='img']")
121
+ reviews_text = reviews_element.get_attribute("aria-label")
122
+ match = re.search(r'(\d+(?:\.\d+)?) stars', reviews_text)
123
+ if match:
124
+ rating = float(match.group(1))
125
+ except Exception:
126
+ pass
127
+ try:
128
+ date_elem = review_elem.find_element(By.CSS_SELECTOR, '.rsqaWe')
129
+ review_date = date_elem.text.strip()
130
+ except Exception:
131
+ review_date = 'No date'
132
+ review_data.append({
133
+ 'reviewer_name': reviewer_name,
134
+ 'review_text': review_text,
135
+ 'rating': rating,
136
+ 'review_date': review_date,
137
+ })
138
+ driver.quit()
139
+ df = pd.DataFrame(review_data)
140
+ df[df["review_text"].str.contains("No review text")==False]
141
+ st.dataframe(df)
142
+