Besimplestudio commited on
Commit
2a5c292
·
verified ·
1 Parent(s): 97bb8e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -55
app.py CHANGED
@@ -1,67 +1,42 @@
1
  import gradio as gr
2
  from pytrends.request import TrendReq
3
- import pandas as pd
4
- import plotly.express as px
5
- import time
6
 
7
- # Function to fetch trends and plot data
8
- def fetch_trends(search_query='adult coloring books', region='US'):
9
- pytrends = TrendReq(hl='en-US', tz=360)
10
 
11
- # Set up the payload with the given query and region
12
- pytrends.build_payload([search_query], cat=0, timeframe='today 12-m', geo=region)
 
13
 
14
- # Fetch related queries
15
- related_queries = pytrends.related_queries()
 
16
 
17
- # Check if there are any top related queries
18
- if related_queries and related_queries.get(search_query, {}).get('top') is not None:
19
- # Process the top related queries into a dataframe
20
- df = related_queries[search_query]['top']
21
- df = df[['query', 'value']] # Filter only relevant columns
22
- df = df.nlargest(10, 'value') # Get top 10 trending queries
23
-
24
- # Create the Plotly bar chart for visualization
25
- fig = px.bar(
26
- df, x='query', y='value',
27
- title=f"Top 10 Trending Keywords for '{search_query}' in {region}",
28
- labels={'query': 'Keyword', 'value': 'Popularity Score'},
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
- else:
40
- return None, "No data found for the query in this region."
41
 
42
- # Create Gradio Interface
43
  iface = gr.Interface(
44
- fn=fetch_trends,
45
- inputs=[
46
- gr.Textbox(
47
- label="Enter Search Term",
48
- value="adult coloring books", # Default search term
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()