Building a Google Trends Visualizer App with Python and Hugging Face
Indroduction
When I first stumbled upon the Python pytrends
library, I was immediately intrigued. As someone diving into SEO optimization, understanding search trends is crucial. Imagine having the power to visualize Google Trends data with just a few lines of code. I knew I had to explore this further and share it with others who might find it just as useful.
In this blog, I’ll walk you through how I created a simple yet powerful Google Trends Visualizer using Python, pytrends
, and Hugging Face. I’ll also show you how to deploy it as an interactive app on Hugging Face Spaces. Whether you’re into data analysis, SEO, or just love playing with trends, this tool could be a game-changer for you.
Why Google Trends?
Google Trends is an invaluable tool for understanding what the world is searching for. Whether you’re optimizing content, conducting market research, or just curious about global trends, this tool gives you insight into search behavior over time. But accessing this data programmatically allows you to automate and visualize trends in ways that can be far more powerful than manual searches.
How I Build Goggle Trends Visualizer App
I wanted to create this app but I have no knowledge to do so.Hugging Face Spaces was the perfect solution.
Hugging Face Spaces is an awesome platform to deploy machine learning models and data apps. To deploy your app:
-
Create a New Space: Go to Hugging Face and create a new Space. Choose the “Gradio” template since we’re using Gradio for the UI.
-
Upload Files: Upload your Python script and
requirements.txt
file to the Space. Hugging Face will automatically install the required libraries. -
Run the App: Once uploaded, the app should run automatically. You can share the link with others and start exploring trends.
The Heart of My App: app.py
Here’s a snippet of my app.py
file:
import gradio as gr
from pytrends.request import TrendReq
import pandas as pd
import matplotlib.pyplot as pltdef get_trends(keyword):
pytrends = TrendReq(hl=”en-US”, tz=360)
pytrends.build_payload(kw_list=[keyword])
df = pytrends.interest_over_time()if df.empty:
return Noneplt.figure(figsize=(20, 10))
df[keyword].plot()
plt.title(f”Google Trends for ‘{keyword}'”, fontsize=20)
plt.xlabel(“Date”, fontsize=16)
plt.ylabel(“Interest over time”, fontsize=16)
plt.grid(True)
return pltiface = gr.Interface(
fn=get_trends,
inputs=gr.Textbox(lines=1, label=”Enter a keyword”),
outputs=gr.Plot(),
title=”Google Trends Visualizer”,
description=”Enter a keyword to see its Google Trends data over time.”
)iface.launch()
Don’t Forget the requirements.txt!
I learned the hard way that Hugging Face needs a requirements.txt
file. Here’s what mine looked like:
gradio
pytrends
pandas
matplotlib
How the Code Works
-
Imports and Setup: We import necessary libraries like
gradio
,pytrends
,pandas
, andmatplotlib
. These will handle the interface, data fetching, and visualization. -
Fetching Trends: The
get_trends
function usespytrends
to request Google Trends data for the provided keyword. It then processes this data into a Pandas DataFrame. -
Visualization: If data is available, we plot it using
matplotlib
. The plot is styled and labeled to make it informative. -
Gradio Interface: We create an interface with
gr.Interface
. It includes a textbox for input, a plot for output, and some descriptions. -
Launching the App: Finally,
iface.launch()
starts the app. You can enter any keyword, and the app will display its trend over time.
Conclusion
Building this Google Trends Visualizer was not only educational but also practical. It’s a simple tool with powerful applications in SEO, content creation, and market analysis. If you’re as curious about trends as I am, give it a try!
This project taught me a lot about Python libraries and deploying apps on Hugging Face. If you’ve found this guide helpful, or have any questions, feel free to drop a comment. Happy coding, Happy chatgpting!
How to Register an Account on Binance
October 28, 2024 @ 2:01 pm
Thanks for this very informative article! For anyone looking for a detailed step-by-step guide on creating a Binance account, here’s a helpful resource I found: How to Register an Account on Binance. Hope it’s useful!
create binance account
November 2, 2024 @ 11:44 pm
How to Register an Account on Binance https://www.binance.com/en/square/post/15710503553490?ref=775587485