Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadd review sentiment analysis for product
app.py
CHANGED
|
@@ -89,6 +89,54 @@ def sig_expander(sig: str) -> str:
|
|
| 89 |
}
|
| 90 |
|
| 91 |
return sig_map.get(sig.lower(), "❌ No direct match found. Please clarify.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
@tool
|
| 93 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 94 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
|
@@ -133,7 +181,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 133 |
|
| 134 |
agent = CodeAgent(
|
| 135 |
model=model,
|
| 136 |
-
tools=[final_answer,
|
| 137 |
max_steps=6,
|
| 138 |
verbosity_level=1,
|
| 139 |
grammar=None,
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
return sig_map.get(sig.lower(), "❌ No direct match found. Please clarify.")
|
| 92 |
+
|
| 93 |
+
def scrape_amazon_reviews(product_url):
|
| 94 |
+
"""Scrapes reviews from an Amazon product page (basic version)."""
|
| 95 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 96 |
+
response = requests.get(product_url, headers=headers)
|
| 97 |
+
if response.status_code != 200:
|
| 98 |
+
return f"❌ Error fetching Amazon reviews: {response.status_code}"
|
| 99 |
+
|
| 100 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 101 |
+
reviews = [r.text.strip() for r in soup.find_all("span", {"data-hook": "review-body"})]
|
| 102 |
+
return reviews[:10] # Return the first 10 reviews
|
| 103 |
+
|
| 104 |
+
def analyze_sentiment(reviews):
|
| 105 |
+
"""Analyzes the sentiment of a list of reviews."""
|
| 106 |
+
results = {"Positive": 0, "Neutral": 0, "Negative": 0}
|
| 107 |
+
for review in reviews:
|
| 108 |
+
sentiment_score = TextBlob(review).sentiment.polarity
|
| 109 |
+
if sentiment_score > 0.2:
|
| 110 |
+
results["Positive"] += 1
|
| 111 |
+
elif sentiment_score < -0.2:
|
| 112 |
+
results["Negative"] += 1
|
| 113 |
+
else:
|
| 114 |
+
results["Neutral"] += 1
|
| 115 |
+
return results
|
| 116 |
+
|
| 117 |
+
def generate_summary(results):
|
| 118 |
+
"""Generates a summary report of sentiment analysis."""
|
| 119 |
+
total = sum(results.values())
|
| 120 |
+
if total == 0:
|
| 121 |
+
return "No reviews available for analysis."
|
| 122 |
+
return (
|
| 123 |
+
f"🔍 **Sentiment Analysis Report:**\n"
|
| 124 |
+
f"✅ Positive: {results['Positive']} ({(results['Positive']/total)*100:.1f}%)\n"
|
| 125 |
+
f"⚖️ Neutral: {results['Neutral']} ({(results['Neutral']/total)*100:.1f}%)\n"
|
| 126 |
+
f"❌ Negative: {results['Negative']} ({(results['Negative']/total)*100:.1f}%)"
|
| 127 |
+
)
|
| 128 |
+
@tool
|
| 129 |
+
def review_sentiment_tool(product_url: str) -> str:
|
| 130 |
+
"""Agent tool: Scrapes reviews and performs sentiment analysis.
|
| 131 |
+
Args:
|
| 132 |
+
product_url: the amazon url for product to be evaluated
|
| 133 |
+
"""
|
| 134 |
+
reviews = scrape_amazon_reviews(product_url)
|
| 135 |
+
if isinstance(reviews, str): # Error message
|
| 136 |
+
return reviews
|
| 137 |
+
sentiment_results = analyze_sentiment(reviews)
|
| 138 |
+
return generate_summary(sentiment_results)
|
| 139 |
+
|
| 140 |
@tool
|
| 141 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 142 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
|
|
|
| 181 |
|
| 182 |
agent = CodeAgent(
|
| 183 |
model=model,
|
| 184 |
+
tools=[final_answer, review_sentiment_tool], ## add your tools here (don't remove final answer)
|
| 185 |
max_steps=6,
|
| 186 |
verbosity_level=1,
|
| 187 |
grammar=None,
|