Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213142 unmodified lines171819202122232425262728293031323334351 unmodified line3738394041424344454647484950514 unmodified lines56575859606162636465import gradio as grfrom pytrends.request import TrendReqimport pandas as pdfrom datetime import datetimeimport timefrom collections import defaultdictimport plotly.express as pxdef analyze_coloring_trends(region='US'):pytrends = TrendReq(hl='en-US', tz=360)coloring_categories = ['adult coloring books', 'mandala coloring','animal coloring', 'flower coloring',2 unmodified linestrending_keywords = defaultdict(int)results = []for category in coloring_categories:try:pytrends.build_payload(kw_list=[category],cat=0,timeframe='today 3-m',geo=region)related_queries = pytrends.related_queries()if related_queries[category]['top'] is not None:for row in related_queries[category]['top'].itertuples():trending_keywords[row.query] += row.valueresults.append({1 unmodified line'score': row.value,'category': category})time.sleep(1)except Exception as e:continuedf = pd.DataFrame(results)# Create visualizationfig = px.bar(df.nlargest(15, 'score'),x='keyword',4 unmodified lines)fig.update_layout(xaxis_tickangle=-45)# Format text resultstop_keywords = df.nlargest(10, 'score')[['keyword', 'score', 'category']]top_keywords_str = top_keywords.to_string()recommendations = f"""Top Trending Categories in {region}:1. {coloring_categories[0]}1234567891011121314152 unmodified lines1819202122232425262728293031323334353637383940411 unmodified line4344454647484950515253545556575859606162636465664 unmodified lines7172737475767778798081import gradio as grfrom pytrends.request import TrendReqimport pandas as pdfrom collections import defaultdictimport plotly.express as pximport timedef analyze_coloring_trends(region='US'):# Initialize Google Trends connectionpytrends = TrendReq(hl='en-US', tz=360)# Define categories to analyzecoloring_categories = ['adult coloring books', 'mandala coloring','animal coloring', 'flower coloring',2 unmodified linestrending_keywords = defaultdict(int)results = []for category in coloring_categories:try:print(f"Fetching data for category: {category}")# Build payload with extended timeframe to capture more trendspytrends.build_payload(kw_list=[category],cat=0,timeframe='today 12-m', # Extended timeframe for better resultsgeo=region)# Fetch related queriesrelated_queries = pytrends.related_queries()# Ensure data exists for the categoryif related_queries and related_queries[category]['top'] is not None:for row in related_queries[category]['top'].itertuples():trending_keywords[row.query] += row.valueresults.append({1 unmodified line'score': row.value,'category': category})else:print(f"No data for category: {category}")time.sleep(1) # Pause to respect Google Trends rate limitsexcept Exception as e:print(f"Error fetching data for {category}: {e}")continue# Create DataFrame from resultsdf = pd.DataFrame(results)# Check if DataFrame has any dataif df.empty:print("No data was retrieved.")return "No data available for the selected categories in this region.", "", ""# Create bar chart visualizationfig = px.bar(df.nlargest(15, 'score'),x='keyword',4 unmodified lines)fig.update_layout(xaxis_tickangle=-45)# Prepare top 10 keywords for displaytop_keywords = df.nlargest(10, 'score')[['keyword', 'score', 'category']]top_keywords_str = top_keywords.to_string(index=False)# Recommendations based on the analysisrecommendations = f"""Top Trending Categories in {region}:1. {coloring_categories[0]}
app.py+26-10