maraalbulescu commited on
Commit
0996202
Β·
verified Β·
1 Parent(s): 6cca900

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
4
+ import requests
5
+
6
+ # Load results
7
+ df = pd.read_csv('final_results.csv')
8
+ analyzer = SentimentIntensityAnalyzer()
9
+
10
+ # Your n8n webhook URL
11
+ N8N_WEBHOOK_URL = "https://andreeaalbulescu.app.n8n.cloud/webhook-test/book-price-optimizer"
12
+
13
+ def analyze_book(book_title):
14
+ match = df[df['title'].str.contains(book_title, case=False, na=False)]
15
+
16
+ if len(match) > 0:
17
+ row = match.iloc[0]
18
+ sentiment = row['avg_sentiment']
19
+ recommendation = row['recommendation']
20
+ current_price = row['price']
21
+ recommended_price = row['recommended_price']
22
+ rating = row['rating']
23
+
24
+ if sentiment > 0.2:
25
+ sentiment_label = "Positive 😊"
26
+ elif sentiment < -0.1:
27
+ sentiment_label = "Negative 😞"
28
+ else:
29
+ sentiment_label = "Neutral 😐"
30
+
31
+ result = f"""
32
+ πŸ“š BOOK ANALYSIS RESULTS
33
+ ========================
34
+ Title: {row['title']}
35
+ Current Price: Β£{current_price}
36
+ Rating: {'⭐' * int(rating)}
37
+
38
+ πŸ“Š SENTIMENT ANALYSIS
39
+ Sentiment: {sentiment_label}
40
+ Score: {round(sentiment, 3)}
41
+
42
+ πŸ’° PRICING RECOMMENDATION
43
+ Action: {recommendation}
44
+ Recommended Price: Β£{recommended_price}
45
+ """
46
+
47
+ # Send to n8n webhook
48
+ try:
49
+ requests.post(N8N_WEBHOOK_URL, json={
50
+ "book": book_title,
51
+ "sentiment": round(float(sentiment), 3),
52
+ "recommendation": recommendation,
53
+ "current_price": float(current_price),
54
+ "recommended_price": float(recommended_price)
55
+ }, timeout=5)
56
+ except:
57
+ pass
58
+
59
+ else:
60
+ score = analyzer.polarity_scores(book_title)['compound']
61
+ if score > 0.2:
62
+ result = f"Book not in database.\nSentiment: Positive (score: {round(score,3)})\nSuggestion: Consider maintaining or increasing price."
63
+ else:
64
+ result = f"Book not in database.\nSentiment: Neutral/Negative (score: {round(score,3)})\nSuggestion: Consider reviewing your pricing."
65
+
66
+ # Send to n8n webhook
67
+ try:
68
+ requests.post(N8N_WEBHOOK_URL, json={
69
+ "book": book_title,
70
+ "sentiment": round(score, 3),
71
+ "recommendation": "UNKNOWN - not in database",
72
+ "current_price": None,
73
+ "recommended_price": None
74
+ }, timeout=5)
75
+ except:
76
+ pass
77
+
78
+ return result
79
+
80
+ iface = gr.Interface(
81
+ fn=analyze_book,
82
+ inputs=gr.Textbox(
83
+ label="Enter Book Title",
84
+ placeholder="e.g. A Light in the Attic"
85
+ ),
86
+ outputs=gr.Textbox(label="Analysis Results"),
87
+ title="πŸ“š Book Price Optimizer",
88
+ description="Enter a book title to get sentiment analysis and AI-powered pricing recommendations. Results are automatically sent to our n8n automation pipeline.",
89
+ examples=[
90
+ ["A Light in the Attic"],
91
+ ["Tipping the Velvet"],
92
+ ["Sharp Objects"]
93
+ ]
94
+ )
95
+
96
+ iface.launch()