Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
return pd.DataFrame(competition_data)
|
89 |
-
|
90 |
-
def analyze_coloring_books(region):
|
91 |
-
analyzer = ColoringBookAnalyzer()
|
92 |
|
93 |
-
|
94 |
-
trends_df = analyzer.analyze_trends(geo=region)
|
95 |
|
96 |
# Create visualization
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
#
|
100 |
-
|
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 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
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 |
-
|
118 |
-
- Focus on
|
119 |
-
- Create
|
120 |
-
-
|
121 |
-
-
|
122 |
-
- Use relevant keywords in title and description
|
123 |
"""
|
124 |
|
125 |
-
return
|
126 |
|
127 |
# Create Gradio interface
|
128 |
iface = gr.Interface(
|
129 |
-
fn=
|
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
|
145 |
theme="default"
|
146 |
)
|
147 |
|
148 |
-
#
|
149 |
-
|
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()
|
|
|
|