Spaces:
Sleeping
Sleeping
Upload trail_7.py
Browse files- trail_7.py +104 -0
trail_7.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from streamlit_lottie import st_lottie
|
| 4 |
+
import requests
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 8 |
+
import smtplib
|
| 9 |
+
from email.mime.multipart import MIMEMultipart
|
| 10 |
+
from email.mime.text import MIMEText
|
| 11 |
+
# Define Lottie animation URLs
|
| 12 |
+
animation_url1 = "https://lottie.host/dd7f2ccb-f1a4-46ab-9367-ac7a766c382f/1mpGXTnenM.json"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Load Lottie animation data from URLs
|
| 16 |
+
def load_lottie_url(url):
|
| 17 |
+
r = requests.get(url)
|
| 18 |
+
if r.status_code != 200:
|
| 19 |
+
return None
|
| 20 |
+
return r.json()
|
| 21 |
+
|
| 22 |
+
st.header('Sentiment Analysis')
|
| 23 |
+
|
| 24 |
+
# Load RoBERTa model and tokenizer
|
| 25 |
+
roberta_model_name = "cardiffnlp/twitter-roberta-base-sentiment"
|
| 26 |
+
model = AutoModelForSequenceClassification.from_pretrained(roberta_model_name)
|
| 27 |
+
tokenizer = AutoTokenizer.from_pretrained(roberta_model_name)
|
| 28 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
| 29 |
+
|
| 30 |
+
# Define function for sentiment analysis using RoBERTa
|
| 31 |
+
def analyze_sentiment(text):
|
| 32 |
+
encoded_text = tokenizer(text, return_tensors='pt', truncation=False, padding=True)
|
| 33 |
+
output = model(**encoded_text)
|
| 34 |
+
scores = torch.softmax(output.logits, dim=1).detach().numpy()[0]
|
| 35 |
+
sentiment_index = scores.argmax()
|
| 36 |
+
sentiment_label = labels[sentiment_index]
|
| 37 |
+
return sentiment_label, scores[sentiment_index]
|
| 38 |
+
|
| 39 |
+
# Define function to scrape reviews from an Amazon product page
|
| 40 |
+
def scrape_amazon_reviews(product_url):
|
| 41 |
+
response = requests.get(product_url)
|
| 42 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 43 |
+
|
| 44 |
+
review_elements = soup.find_all("div", class_="a-section review aok-relative")
|
| 45 |
+
reviews = []
|
| 46 |
+
for review_element in review_elements[:5]:
|
| 47 |
+
|
| 48 |
+
review_text = review_element.find("span", class_="review-text").text.strip()
|
| 49 |
+
|
| 50 |
+
reviews.append(review_text)
|
| 51 |
+
|
| 52 |
+
sentiment_label, sentiment_score = analyze_sentiment(review_text)
|
| 53 |
+
if sentiment_label == 'Negative':
|
| 54 |
+
send_email_alert(review_text) # Send email alert for negative review
|
| 55 |
+
|
| 56 |
+
return reviews
|
| 57 |
+
|
| 58 |
+
# Function to send email alert for negative review
|
| 59 |
+
def send_email_alert(review_text):
|
| 60 |
+
|
| 61 |
+
sender_email = "vaibhavdeori1@gmail.com"
|
| 62 |
+
receiver_email = "vaibhavdeori01@gmail.com"
|
| 63 |
+
password = "ejcp fpdq reek ewer"
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
message = MIMEMultipart()
|
| 67 |
+
message["From"] = sender_email
|
| 68 |
+
message["To"] = receiver_email
|
| 69 |
+
message["Subject"] = "Negative Review Alert"
|
| 70 |
+
body = f"The following review is negative:\n\n{review_text}"
|
| 71 |
+
message.attach(MIMEText(body, "plain"))
|
| 72 |
+
|
| 73 |
+
# Establish a connection with the SMTP server
|
| 74 |
+
with smtplib.SMTP("smtp.gmail.com", 587) as server:
|
| 75 |
+
server.starttls()
|
| 76 |
+
server.login(sender_email, password)
|
| 77 |
+
server.sendmail(sender_email, receiver_email, message.as_string())
|
| 78 |
+
|
| 79 |
+
# Analyze Text
|
| 80 |
+
with st.expander('Analyze Text'):
|
| 81 |
+
text = st.text_input('Text here: ')
|
| 82 |
+
if text:
|
| 83 |
+
sentiment_label, sentiment_score = analyze_sentiment(text)
|
| 84 |
+
st.write('Sentiment:', sentiment_label)
|
| 85 |
+
st.write('Confidence Score:', sentiment_score)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Scrape Reviews from Amazon Product Page
|
| 89 |
+
with st.expander('URL of product'):
|
| 90 |
+
product_url = st.text_input('Enter the link to the Amazon product page:')
|
| 91 |
+
if product_url:
|
| 92 |
+
reviews = scrape_amazon_reviews(product_url)
|
| 93 |
+
if reviews:
|
| 94 |
+
st.write('Reviews scraped successfully:')
|
| 95 |
+
for review in reviews:
|
| 96 |
+
sentiment_label, sentiment_score = analyze_sentiment(review)
|
| 97 |
+
st.write('Review:', review)
|
| 98 |
+
st.write('Sentiment:', sentiment_label)
|
| 99 |
+
st.write('Confidence Score:', sentiment_score)
|
| 100 |
+
st.write('---')
|
| 101 |
+
else:
|
| 102 |
+
st.write('No reviews found.')
|
| 103 |
+
|
| 104 |
+
st_lottie(load_lottie_url(animation_url1), speed=1, height=200, key="lottie1")
|