Besimplestudio commited on
Commit
0b645dd
·
verified ·
1 Parent(s): ea658f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -114
app.py CHANGED
@@ -5,128 +5,79 @@ 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'],
@@ -137,15 +88,12 @@ iface = gr.Interface(
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
-
 
5
  import time
6
  from collections import defaultdict
7
  import plotly.express as px
 
8
 
9
+ def analyze_coloring_trends(region='US'):
10
+ pytrends = TrendReq(hl='en-US', tz=360)
11
+
12
+ coloring_categories = [
13
+ 'adult coloring books', 'mandala coloring',
14
+ 'animal coloring', 'flower coloring',
15
+ 'kids coloring books'
16
+ ]
17
+
18
+ trending_keywords = defaultdict(int)
19
+ results = []
20
+
21
+ for category in coloring_categories:
22
+ try:
23
+ pytrends.build_payload(
24
+ kw_list=[category],
25
+ cat=0,
26
+ timeframe='today 3-m',
27
+ geo=region
28
+ )
29
+
30
+ related_queries = pytrends.related_queries()
31
+
32
+ if related_queries[category]['top'] is not None:
33
+ for row in related_queries[category]['top'].itertuples():
34
+ trending_keywords[row.query] += row.value
35
+ results.append({
36
+ 'keyword': row.query,
37
+ 'score': row.value,
38
+ 'category': category
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  })
40
+
41
+ time.sleep(1)
42
+
43
+ except Exception as e:
44
+ continue
 
 
 
 
 
45
 
46
+ df = pd.DataFrame(results)
 
47
 
48
  # Create visualization
49
+ fig = px.bar(
50
+ df.nlargest(15, 'score'),
51
+ x='keyword',
52
+ y='score',
53
+ color='category',
54
+ title='Top 15 Trending Coloring Book Keywords',
55
+ labels={'keyword': 'Keyword', 'score': 'Popularity Score'}
56
+ )
57
+ fig.update_layout(xaxis_tickangle=-45)
58
 
59
+ # Format text results
60
+ top_keywords = df.nlargest(10, 'score')[['keyword', 'score', 'category']]
 
 
 
61
  top_keywords_str = top_keywords.to_string()
62
 
63
+ recommendations = f"""
64
+ Top Trending Categories in {region}:
65
+ 1. {coloring_categories[0]}
66
+ 2. {coloring_categories[1]}
67
+ 3. {coloring_categories[2]}
 
 
 
 
 
68
 
69
+ Recommended Actions:
70
+ - Focus on these trending keywords
71
+ - Create multiple variations
72
+ - Target different skill levels
73
+ - Consider seasonal themes
 
74
  """
75
 
76
+ return fig, top_keywords_str, recommendations
77
 
78
  # Create Gradio interface
79
  iface = gr.Interface(
80
+ fn=analyze_coloring_trends,
81
  inputs=[
82
  gr.Dropdown(
83
  choices=['US', 'UK', 'CA', 'AU', 'DE', 'FR', 'ES', 'IT'],
 
88
  outputs=[
89
  gr.Plot(label="Trending Keywords Visualization"),
90
  gr.Textbox(label="Top 10 Trending Keywords", lines=10),
 
91
  gr.Textbox(label="Recommendations", lines=10)
92
  ],
93
  title="Coloring Book Trend Analyzer",
94
+ description="Analyze trending keywords in the coloring book market",
95
  theme="default"
96
  )
97
 
98
+ # Launch the interface
99
+ iface.launch()