Besimplestudio commited on
Commit
677b1b8
·
verified ·
1 Parent(s): e421a95

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +151 -0
App.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pytrends.request import TrendReq
3
+ import pandas as pd
4
+ from datetime import datetime
5
+ import time
6
+ from collections import defaultdict
7
+ import plotly.express as px
8
+ import plotly.graph_objects as go
9
+
10
+ class ColoringBookAnalyzer:
11
+ def __init__(self):
12
+ self.pytrends = TrendReq(hl='en-US', tz=360)
13
+ self.coloring_categories = [
14
+ 'adult coloring books', 'mandala coloring', 'animal coloring',
15
+ 'flower coloring', 'kids coloring books', 'fantasy coloring',
16
+ 'anime coloring', 'pattern coloring', 'quotes coloring'
17
+ ]
18
+
19
+ def analyze_trends(self, timeframe='today 3-m', geo=''):
20
+ trending_keywords = defaultdict(int)
21
+ results = []
22
+
23
+ for category in self.coloring_categories:
24
+ try:
25
+ self.pytrends.build_payload(
26
+ kw_list=[category],
27
+ cat=0,
28
+ timeframe=timeframe,
29
+ geo=geo
30
+ )
31
+
32
+ related_queries = self.pytrends.related_queries()
33
+
34
+ if related_queries[category]['top'] is not None:
35
+ for row in related_queries[category]['top'].itertuples():
36
+ trending_keywords[row.query] += row.value
37
+ results.append({
38
+ 'keyword': row.query,
39
+ 'score': row.value,
40
+ 'category': category
41
+ })
42
+
43
+ time.sleep(1)
44
+
45
+ except Exception as e:
46
+ continue
47
+
48
+ return pd.DataFrame(results)
49
+
50
+ def create_trends_plot(self, df):
51
+ fig = px.bar(
52
+ df.nlargest(15, 'score'),
53
+ x='keyword',
54
+ y='score',
55
+ color='category',
56
+ title='Top 15 Trending Coloring Book Keywords',
57
+ labels={'keyword': 'Keyword', 'score': 'Popularity Score'}
58
+ )
59
+ fig.update_layout(xaxis_tickangle=-45)
60
+ return fig
61
+
62
+ def get_competition_analysis(self, df):
63
+ competition_data = []
64
+ for _, row in df.nlargest(5, 'score').iterrows():
65
+ try:
66
+ self.pytrends.build_payload(
67
+ kw_list=[row['keyword']],
68
+ cat=0,
69
+ timeframe='today 3-m'
70
+ )
71
+
72
+ interest_over_time = self.pytrends.interest_over_time()
73
+
74
+ if not interest_over_time.empty:
75
+ avg_interest = interest_over_time[row['keyword']].mean()
76
+ competition_level = "High" if avg_interest > 75 else "Medium" if avg_interest > 40 else "Low"
77
+ competition_data.append({
78
+ 'keyword': row['keyword'],
79
+ 'average_interest': avg_interest,
80
+ 'competition_level': competition_level
81
+ })
82
+
83
+ time.sleep(1)
84
+
85
+ except Exception as e:
86
+ continue
87
+
88
+ return pd.DataFrame(competition_data)
89
+
90
+ def analyze_coloring_books(region):
91
+ analyzer = ColoringBookAnalyzer()
92
+
93
+ # Get trends
94
+ trends_df = analyzer.analyze_trends(geo=region)
95
+
96
+ # Create visualization
97
+ trends_plot = analyzer.create_trends_plot(trends_df)
98
+
99
+ # Get competition analysis
100
+ competition_df = analyzer.get_competition_analysis(trends_df)
101
+
102
+ # Format results
103
+ top_keywords = trends_df.nlargest(10, 'score')[['keyword', 'score', 'category']]
104
+ top_keywords_str = top_keywords.to_string()
105
+
106
+ competition_str = competition_df.to_string()
107
+
108
+ # Generate recommendations
109
+ recommendations = """
110
+ Recommended Niches Based on Analysis:
111
+ 1. Complex Adult Coloring Books
112
+ 2. Simple Kids Coloring Books
113
+ 3. Mandala Collections
114
+ 4. Animal Themed Books
115
+ 5. Seasonal Coloring Books
116
+
117
+ Tips for Success:
118
+ - Focus on clear, high-quality designs
119
+ - Create both simple and complex versions
120
+ - Include 30-50 designs per book
121
+ - Price between $6.99 - $12.99
122
+ - Use relevant keywords in title and description
123
+ """
124
+
125
+ return trends_plot, top_keywords_str, competition_str, recommendations
126
+
127
+ # Create Gradio interface
128
+ iface = gr.Interface(
129
+ fn=analyze_coloring_books,
130
+ inputs=[
131
+ gr.Dropdown(
132
+ choices=['US', 'UK', 'CA', 'AU', 'DE', 'FR', 'ES', 'IT'],
133
+ label="Select Region",
134
+ default="US"
135
+ )
136
+ ],
137
+ outputs=[
138
+ gr.Plot(label="Trending Keywords Visualization"),
139
+ gr.Textbox(label="Top 10 Trending Keywords", lines=10),
140
+ gr.Textbox(label="Competition Analysis", lines=10),
141
+ gr.Textbox(label="Recommendations", lines=10)
142
+ ],
143
+ title="Coloring Book Trend Analyzer",
144
+ description="Analyze trending keywords and competition in the coloring book market",
145
+ theme="default"
146
+ )
147
+
148
+ # For local testing
149
+ if __name__ == "__main__":
150
+ iface.launch()
151
+