File size: 5,464 Bytes
ee3e666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import streamlit as st 


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.core.os_manager import ChromeType

import time
import sys
import re
import transformers
import pandas as pd 
import torch
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
import io
import plotly.express as px
import zipfile
from streamlit_extras.stylable_container import stylable_container


# sidebar
with st.sidebar:
    with stylable_container(
        key="test_button",
        css_styles="""
        button {
            background-color: yellow;
            border: 1px solid black;
            padding: 5px;
            color: black;
        }
        """,
    ):
        st.button("DEMO APP")
   

    expander = st.expander("**Important notes on the Google Maps Reviews Sentiment Analysis App**")
    expander.write('''
    
    
    **How to Use**
        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.
    
    
    **Usage Limits**
    You can perform sentiment analysis on Google Maps Reviews up to 5 times.
    
    
    **Subscription Management**
    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]
    
    
    **Customization**
    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.
    
    
    **File Handling and Errors**
    For any errors or inquiries, please contact us at [email protected]
   
    
    
''')
    
tokenizer = DistilBertTokenizer.from_pretrained("tabularisai/robust-sentiment-analysis")
model = DistilBertForSequenceClassification.from_pretrained("tabularisai/robust-sentiment-analysis")



def scroll_and_check_for_new_reviews(driver, current_review_count):
    """Scrolls down the page and checks if new reviews have loaded."""
    try:
        last_review = driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf')[-1]
        driver.execute_script("arguments[0].scrollIntoView(true);", last_review)
        time.sleep(3)  # Increased sleep time to allow for loading
        new_review_count = len(driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf'))
        return new_review_count > current_review_count
    except Exception as e:
        st.error(f"Error during scrolling: {e}")
        return False

def scrape_google_reviews(url):
    """Scrapes Google reviews from the given URL and performs sentiment analysis."""
    try:
        options = Options()
        options.add_argument("--headless")
        options.add_argument("--disable-gpu")
        options.add_argument("--no-sandbox")
        options.add_argument("--disable-dev-shm-usage")
        options.add_argument("--start-maximized")
        service = Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
        driver = webdriver.Chrome(service=service, options=options)
        driver.get(url)

       

        current_review_count = 0
        while scroll_and_check_for_new_reviews(driver, current_review_count):
            current_review_count = len(driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf'))
            st.write(f"Total reviews loaded: {current_review_count}")
        
        

        reviews = driver.find_elements(By.CSS_SELECTOR, 'div.jftiEf')
        review_data = []
        for review_elem in reviews:
            try:
                reviewer_name = review_elem.find_element(By.CSS_SELECTOR, '.d4r55').text.strip()
            except Exception:
                reviewer_name = 'No name'
            try:
                review_text = review_elem.find_element(By.CSS_SELECTOR, '.wiI7pd').text.strip()
            except Exception:
                review_text = 'No review text'
            rating = 0
            try:
                reviews_element = review_elem.find_element(By.CSS_SELECTOR, "span[role='img']")
                reviews_text = reviews_element.get_attribute("aria-label")
                match = re.search(r'(\d+(?:\.\d+)?) stars', reviews_text)
                if match:
                    rating = float(match.group(1))
            except Exception:
                pass
            try:
                date_elem = review_elem.find_element(By.CSS_SELECTOR, '.rsqaWe')
                review_date = date_elem.text.strip()
            except Exception:
                review_date = 'No date'
            review_data.append({
                'reviewer_name': reviewer_name,
                'review_text': review_text,
                'rating': rating,
                'review_date': review_date,
            })
        driver.quit()
        df = pd.DataFrame(review_data)
        df[df["review_text"].str.contains("No review text")==False]
        st.dataframe(df)