Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
from pytrends.request import TrendReq
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import time
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
pytrends = TrendReq(hl=
|
10 |
|
11 |
-
#
|
12 |
-
pytrends.build_payload([
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
color='value', color_continuous_scale='Viridis'
|
30 |
-
)
|
31 |
-
fig.update_layout(xaxis_tickangle=-45)
|
32 |
-
|
33 |
-
# Convert dataframe to a string for display
|
34 |
-
top_keywords_str = df.to_string(index=False)
|
35 |
-
|
36 |
-
# Return the plot and the keyword list
|
37 |
-
return fig, top_keywords_str
|
38 |
|
39 |
-
|
40 |
-
return None, "No data found for the query in this region."
|
41 |
|
42 |
-
#
|
43 |
iface = gr.Interface(
|
44 |
-
fn=
|
45 |
-
inputs=
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
placeholder="e.g. adult coloring books, mandala coloring, etc."
|
50 |
-
),
|
51 |
-
gr.Dropdown(
|
52 |
-
choices=['US', 'UK', 'CA', 'AU', 'DE', 'FR', 'ES', 'IT', 'JP', 'BR', 'IN'],
|
53 |
-
label="Select Region",
|
54 |
-
value="US" # Set default region
|
55 |
-
)
|
56 |
-
],
|
57 |
-
outputs=[
|
58 |
-
gr.Plot(label="Trending Keywords Visualization"),
|
59 |
-
gr.Textbox(label="Top 10 Trending Keywords", lines=10)
|
60 |
-
],
|
61 |
-
title="Google Trends Keyword Analyzer",
|
62 |
-
description="Analyze trending keywords for any search term in various regions using Google Trends",
|
63 |
-
theme="default"
|
64 |
)
|
65 |
|
66 |
-
# Launch the interface
|
67 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from pytrends.request import TrendReq
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import os
|
|
|
5 |
|
6 |
+
def get_trends(keyword):
|
7 |
+
# Initialize the pytrends object
|
8 |
+
pytrends = TrendReq(hl="en-US", tz=360)
|
9 |
|
10 |
+
# Build the payload and fetch the trends data
|
11 |
+
pytrends.build_payload(kw_list=[keyword])
|
12 |
+
df = pytrends.interest_over_time()
|
13 |
|
14 |
+
# If there is no data, return a message
|
15 |
+
if df.empty:
|
16 |
+
return "No data available for this keyword. Please try a different one."
|
17 |
|
18 |
+
# Create a plot
|
19 |
+
plt.figure(figsize=(20, 10))
|
20 |
+
df[keyword].plot()
|
21 |
+
plt.title(f"Google Trends for '{keyword}'", fontsize=20)
|
22 |
+
plt.xlabel("Date", fontsize=16)
|
23 |
+
plt.ylabel("Interest over time", fontsize=16)
|
24 |
+
plt.grid(True)
|
25 |
+
|
26 |
+
# Save the plot to a temporary file
|
27 |
+
plot_file = "/tmp/trends_plot.png"
|
28 |
+
plt.savefig(plot_file)
|
29 |
+
plt.close() # Close the plot to avoid display issues
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
return plot_file # Return the file path for Gradio to display the image
|
|
|
32 |
|
33 |
+
# Set up the Gradio interface
|
34 |
iface = gr.Interface(
|
35 |
+
fn=get_trends,
|
36 |
+
inputs=gr.Textbox(lines=1, label="Enter a keyword"),
|
37 |
+
outputs=gr.Image(type="file"), # Output is now an image file
|
38 |
+
title="Google Trends Visualizer",
|
39 |
+
description="Enter a keyword to see its Google Trends data over time."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
)
|
41 |
|
|
|
42 |
iface.launch()
|