Besimplestudio commited on
Commit
70882e5
·
verified ·
1 Parent(s): 2737a05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,14 +1,15 @@
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
 
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',
@@ -17,19 +18,24 @@ def analyze_coloring_trends(region='US'):
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({
@@ -37,15 +43,24 @@ def analyze_coloring_trends(region='US'):
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',
@@ -56,10 +71,11 @@ def analyze_coloring_trends(region='US'):
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]}
 
1
  import gradio as gr
2
  from pytrends.request import TrendReq
3
  import pandas as pd
 
 
4
  from collections import defaultdict
5
  import plotly.express as px
6
+ import time
7
 
8
  def analyze_coloring_trends(region='US'):
9
+ # Initialize Google Trends connection
10
  pytrends = TrendReq(hl='en-US', tz=360)
11
 
12
+ # Define categories to analyze
13
  coloring_categories = [
14
  'adult coloring books', 'mandala coloring',
15
  'animal coloring', 'flower coloring',
 
18
 
19
  trending_keywords = defaultdict(int)
20
  results = []
21
+
22
  for category in coloring_categories:
23
  try:
24
+ print(f"Fetching data for category: {category}")
25
+
26
+ # Build payload with extended timeframe to capture more trends
27
  pytrends.build_payload(
28
  kw_list=[category],
29
  cat=0,
30
+ timeframe='today 12-m', # Extended timeframe for better results
31
  geo=region
32
  )
33
 
34
+ # Fetch related queries
35
  related_queries = pytrends.related_queries()
36
 
37
+ # Ensure data exists for the category
38
+ if related_queries and related_queries[category]['top'] is not None:
39
  for row in related_queries[category]['top'].itertuples():
40
  trending_keywords[row.query] += row.value
41
  results.append({
 
43
  'score': row.value,
44
  'category': category
45
  })
46
+ else:
47
+ print(f"No data for category: {category}")
48
 
49
+ time.sleep(1) # Pause to respect Google Trends rate limits
50
+
51
  except Exception as e:
52
+ print(f"Error fetching data for {category}: {e}")
53
  continue
54
 
55
+ # Create DataFrame from results
56
  df = pd.DataFrame(results)
57
 
58
+ # Check if DataFrame has any data
59
+ if df.empty:
60
+ print("No data was retrieved.")
61
+ return "No data available for the selected categories in this region.", "", ""
62
+
63
+ # Create bar chart visualization
64
  fig = px.bar(
65
  df.nlargest(15, 'score'),
66
  x='keyword',
 
71
  )
72
  fig.update_layout(xaxis_tickangle=-45)
73
 
74
+ # Prepare top 10 keywords for display
75
  top_keywords = df.nlargest(10, 'score')[['keyword', 'score', 'category']]
76
+ top_keywords_str = top_keywords.to_string(index=False)
77
 
78
+ # Recommendations based on the analysis
79
  recommendations = f"""
80
  Top Trending Categories in {region}:
81
  1. {coloring_categories[0]}