maraalbulescu's picture
Create app.py
0996202 verified
import gradio as gr
import pandas as pd
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import requests
# Load results
df = pd.read_csv('final_results.csv')
analyzer = SentimentIntensityAnalyzer()
# Your n8n webhook URL
N8N_WEBHOOK_URL = "https://andreeaalbulescu.app.n8n.cloud/webhook-test/book-price-optimizer"
def analyze_book(book_title):
match = df[df['title'].str.contains(book_title, case=False, na=False)]
if len(match) > 0:
row = match.iloc[0]
sentiment = row['avg_sentiment']
recommendation = row['recommendation']
current_price = row['price']
recommended_price = row['recommended_price']
rating = row['rating']
if sentiment > 0.2:
sentiment_label = "Positive 😊"
elif sentiment < -0.1:
sentiment_label = "Negative 😞"
else:
sentiment_label = "Neutral 😐"
result = f"""
πŸ“š BOOK ANALYSIS RESULTS
========================
Title: {row['title']}
Current Price: Β£{current_price}
Rating: {'⭐' * int(rating)}
πŸ“Š SENTIMENT ANALYSIS
Sentiment: {sentiment_label}
Score: {round(sentiment, 3)}
πŸ’° PRICING RECOMMENDATION
Action: {recommendation}
Recommended Price: Β£{recommended_price}
"""
# Send to n8n webhook
try:
requests.post(N8N_WEBHOOK_URL, json={
"book": book_title,
"sentiment": round(float(sentiment), 3),
"recommendation": recommendation,
"current_price": float(current_price),
"recommended_price": float(recommended_price)
}, timeout=5)
except:
pass
else:
score = analyzer.polarity_scores(book_title)['compound']
if score > 0.2:
result = f"Book not in database.\nSentiment: Positive (score: {round(score,3)})\nSuggestion: Consider maintaining or increasing price."
else:
result = f"Book not in database.\nSentiment: Neutral/Negative (score: {round(score,3)})\nSuggestion: Consider reviewing your pricing."
# Send to n8n webhook
try:
requests.post(N8N_WEBHOOK_URL, json={
"book": book_title,
"sentiment": round(score, 3),
"recommendation": "UNKNOWN - not in database",
"current_price": None,
"recommended_price": None
}, timeout=5)
except:
pass
return result
iface = gr.Interface(
fn=analyze_book,
inputs=gr.Textbox(
label="Enter Book Title",
placeholder="e.g. A Light in the Attic"
),
outputs=gr.Textbox(label="Analysis Results"),
title="πŸ“š Book Price Optimizer",
description="Enter a book title to get sentiment analysis and AI-powered pricing recommendations. Results are automatically sent to our n8n automation pipeline.",
examples=[
["A Light in the Attic"],
["Tipping the Velvet"],
["Sharp Objects"]
]
)
iface.launch()