Spaces:
Sleeping
Sleeping
File size: 3,314 Bytes
46214cb 6f84854 46214cb 6f84854 46214cb 6f84854 46214cb 6f84854 46214cb 6f84854 46214cb 6f84854 46214cb 6f84854 46214cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from pytrends.request import TrendReq
# Set up pytrends
trends = TrendReq()
# Function to get data
def get_data(keywords, geo, timeframe):
trends.build_payload(kw_list=keywords, geo=geo, timeframe=timeframe)
data = trends.interest_over_time()
data = data.drop(columns=['isPartial'])
return data.reset_index()
# Streamlit app
st.title("Google Trends Analysis")
# User inputs
keywords = st.text_input("Enter keywords (comma-separated)", "yapay zeka, makine öğrenmesi, derin öğrenme").split(',')
geo = "TR" # Automatically set to TR
timeframe = st.text_input("Enter timeframe (e.g., today 5-y, 2022-01-01 2023-01-01)", "today 5-y")
# Display the selected geo
st.write(f"Selected location: Turkey (TR)")
# Get data
if st.button("Fetch Data"):
data = get_data(keywords, geo, timeframe)
st.session_state['data'] = data
st.success("Data fetched successfully!")
# Visualization options
if 'data' in st.session_state:
viz_options = [
"Line Plot",
"Heatmap",
"Bar Plot",
"Area Plot",
"Violin Plot",
"Box Plot",
"Scatter Plot",
"Pair Plot"
]
selected_viz = st.selectbox("Select Visualization", viz_options)
if selected_viz == "Line Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(data=st.session_state['data'].melt(id_vars=['date'], var_name='term', value_name='searches'),
x='date', y='searches', hue='term', ax=ax)
st.pyplot(fig)
elif selected_viz == "Heatmap":
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(st.session_state['data'].drop('date', axis=1).corr(), annot=True, cmap='coolwarm', ax=ax)
st.pyplot(fig)
elif selected_viz == "Bar Plot":
fig, ax = plt.subplots(figsize=(10, 6))
st.session_state['data'].drop('date', axis=1).mean().plot(kind='bar', ax=ax)
ax.set_ylabel('Average Search Interest')
st.pyplot(fig)
elif selected_viz == "Area Plot":
fig, ax = plt.subplots(figsize=(10, 6))
st.session_state['data'].set_index('date').plot.area(ax=ax)
ax.set_ylabel('Search Interest')
st.pyplot(fig)
elif selected_viz == "Violin Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.violinplot(data=st.session_state['data'].melt(id_vars='date', var_name='term', value_name='searches'),
x='term', y='searches', ax=ax)
st.pyplot(fig)
elif selected_viz == "Box Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.boxplot(data=st.session_state['data'].melt(id_vars='date', var_name='term', value_name='searches'),
x='term', y='searches', ax=ax)
st.pyplot(fig)
elif selected_viz == "Scatter Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.scatterplot(data=st.session_state['data'].melt(id_vars='date', var_name='term', value_name='searches'),
x='date', y='searches', hue='term', ax=ax)
st.pyplot(fig)
elif selected_viz == "Pair Plot":
fig = sns.pairplot(st.session_state['data'].drop('date', axis=1), diag_kind='kde')
st.pyplot(fig) |