Spaces:
Build error
Build error
File size: 15,048 Bytes
13e4835 b438494 13e4835 986cd95 b438494 986cd95 13e4835 986cd95 13e4835 986cd95 13e4835 0ec76d2 a822090 13e4835 986cd95 13e4835 986cd95 0ec76d2 13e4835 986cd95 13e4835 a822090 0ec76d2 a822090 986cd95 b438494 986cd95 13e4835 986cd95 b438494 986cd95 b438494 986cd95 b438494 986cd95 b438494 986cd95 b438494 986cd95 13e4835 986cd95 2a933b6 986cd95 13e4835 | 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | import streamlit as st
import requests
from dotenv import load_dotenv
import os
import pandas as pd
import pandas._libs.tslibs.parsing
import time
import chardet
from helper.telemetry import collect_telemetry
from helper.upload_File import uploadFile
from helper.button_behaviour import hide_button
from helper.initialize_analyze_session import initialize_analyze_session
class Seo:
def __init__(self, model_url):
self.uploaded_files = []
self.file_dict = {}
self.model_url = model_url
#self.analyst_name = analyst_name
#self.data_src = data_src
#self.analyst_description = analyst_description
self.initialize()
self.row1()
def initialize(self):
# FOR ENV
load_dotenv()
# AGENT NAME
#st.header(self.analyst_name)
# EVALUATION FORM LINK
'''url = os.getenv('Link')
st.write('Evaluation Form: [Link](%s)' % url)
# RETURN BUTTON
try:
if st.button("Return", type='primary'):
st.switch_page("./pages/home.py")
except Exception:
pass'''
if 'bounce_rate' not in st.session_state:
st.session_state['bounce_rate'] = ''
if 'page_index' not in st.session_state:
st.session_state['page_index'] = ''
if 'others' not in st.session_state:
st.session_state['others'] = ''
if 'df_traffic' not in st.session_state:
st.session_state['df_traffic'] = ''
if 'df_seo' not in st.session_state:
st.session_state['df_seo'] = ''
def detect_encoding(self, uploaded_file):
result = chardet.detect(uploaded_file.read(100000))
uploaded_file.seek(0) # Reset file pointer to the beginning
return result['encoding']
def keyword_ranking(self, df_seo):
keyword_ranking = df_seo
st.session_state['keyword_ranking'] = keyword_ranking
keywords_ranking_sorted = keyword_ranking.sort_values("Position", ascending=True)
keywords_ranking_top_10 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 10].shape[0]
keywords_ranking_top_100 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 100].shape[0]
keyword_ranking = {
'Keyword_top_10': keywords_ranking_top_10,
'Keyword_top_100': keywords_ranking_top_100
}
st.session_state['keyword_ranking'] = keyword_ranking
def traffic_files(self, df_traffic):
traffic_channels = df_traffic
try:
traffic_channels.rename(columns={traffic_channels.columns[0]: 'date'}, inplace=True)
traffic_channels['date'] = pd.to_datetime(traffic_channels['date'], format='mixed')
except pandas._libs.tslibs.parsing.DateParseError:
pass
traffic_channels_sort = traffic_channels.sort_values("date", ascending=False)
organic_traffic = traffic_channels_sort['Organic Search'].values[0]
paid_traffic = traffic_channels_sort['Paid Search'].values[0]
direct_traffic = traffic_channels_sort['Direct'].values[0]
referral_traffic = traffic_channels_sort['Referral'].values[0]
st.session_state['organic_traffic'] = organic_traffic
st.session_state['paid_traffic'] = paid_traffic
st.session_state['direct_traffic'] = direct_traffic
st.session_state['referral_traffic'] = referral_traffic
def ga4_traffic(self, others):
st.session_state['others'] = others
ga4_paid_social = others['Sessions'].values[0]
ga4_organic_traffic = others['Sessions'].values[4]
ga4_direct_traffic = others['Sessions'].values[2]
ga4_referral_traffic = others['Sessions'].values[3]
st.session_state['ga4_paid_social'] = ga4_paid_social
st.session_state['ga4_organic_traffic'] = ga4_organic_traffic
st.session_state['ga4_direct_traffic'] = ga4_direct_traffic
st.session_state['ga4_referral_traffic'] = ga4_referral_traffic
def delete_sessions(self):
try:
del st.session_state['df_traffic']
del st.session_state['others']
del st.session_state['df_seo']
del st.session_state['keyword_ranking']
del st.session_state['ga4_paid_social']
del st.session_state['ga4_organic_traffic']
del st.session_state['ga4_direct_traffic']
del st.session_state['ga4_referral_traffic']
del st.session_state['organic_traffic']
del st.session_state['paid_traffic']
del st.session_state['direct_traffic']
del st.session_state['referral_traffic']
except KeyError:
pass
def process (self):
session = st.session_state.analyze
if ((self.uploaded_file or self.others or self.uploaded_file_seo) or (self.page_index or self.bounce_rate)) and session == 'clicked':
seo_keywords = ""
traffic_channels = ""
traffic_aqcuisition = ""
pages_index = ""
bounce_rate = ""
with st.spinner('Seo Analyst...', show_time=True):
st.write('')
# INITIALIZING SESSIONS
pages_index += f"Pages Indexed: {self.page_index}\n"
bounce_rate += f"Bounce Rate: {self.bounce_rate}\n"
'''
try:
backlink_files = self.file_dict
combined_text += f"Number of backlinks {backlink_files}\n\n"
except KeyError:
pass
'''
try:
df_traffic = st.session_state['df_traffic']
self.traffic_files(df_traffic)
organic_traffic = st.session_state['organic_traffic']
paid_traffic = st.session_state['paid_traffic']
direct_traffic = st.session_state['direct_traffic']
referral_traffic = st.session_state['referral_traffic']
traffic_channels += f"\nOrganic Traffic: {organic_traffic}"
traffic_channels += f"\nPaid Traffic: {paid_traffic}"
traffic_channels += f"\nDirect Traffic: {direct_traffic}"
traffic_channels += f"\nReferral Traffic: {referral_traffic}"
traffic_channels += df_traffic.to_csv(index=True)
except AttributeError:
pass
except KeyError as e:
# Check if 'df_traffic' is the missing key (no file uploaded)
if self.uploaded_file_seo:
pass
else:
# This would be triggered if df_traffic exists but the other keys are missing
st.info("Incorrect Traffic Channels SEMRush format. Please upload a valid SEMRush file.")
try:
df_seo = st.session_state['df_seo']
self.keyword_ranking(df_seo)
keyword_ranking = st.session_state['keyword_ranking']
seo_keywords += f"\nKeyword Ranking Top 10: {keyword_ranking['Keyword_top_10']}"
seo_keywords += f"\nKeyword Ranking Top 100: {keyword_ranking['Keyword_top_100']}\n\n"
seo_keywords += df_seo.to_csv(index=True)
except AttributeError:
pass
except KeyError:
pass
try:
others = st.session_state['others']
self.ga4_traffic(others)
ga4_paid_social = st.session_state['ga4_paid_social']
ga4_organic_traffic = st.session_state['ga4_organic_traffic']
ga4_direct_traffic = st.session_state['ga4_direct_traffic']
ga4_referral_traffic = st.session_state['ga4_referral_traffic']
traffics = ga4_direct_traffic + ga4_organic_traffic + ga4_paid_social + ga4_referral_traffic
traffic_aqcuisition += f"Traffics: {traffics}"
traffic_aqcuisition += f"\nPaid Traffic: {ga4_paid_social}\nOrganic Traffic: {ga4_organic_traffic}\nDirect Traffic: {ga4_direct_traffic}\nReferral Traffic: {ga4_referral_traffic}"
except KeyError:
if self.others:
pass
else:
# This would be triggered if df_traffic exists but the other keys are missing
st.info("Incorrect Traffic Acquisition GA4 format. Please upload a valid GA4 file.")
except TypeError:
st.info("Incorrect Traffic Acquisition GA4 format. Please upload a valid GA4 file.")
#result = self.request_model(payload_txt_seo_keywords)
#end_time = time.time()
#time_lapsed = end_time - start_time
debug_info_seo_keywords = {'data_field' : 'SEO Keywords', 'result': seo_keywords}
debug_info_traffic_channels = {'data_field' : 'Traffic Channels', 'result': traffic_channels}
debug_info_traffic_aqcuisition = {'data_field' : 'Traffic Acquisition', 'result': traffic_aqcuisition}
debug_info_pages_index = {'data_field' : 'Pages Indexed', 'result': pages_index}
debug_info_bounce_rate = {'data_field' : 'Bounce Rate', 'result': bounce_rate}
'''
debug_info = {
#'analyst': self.analyst_name,
'url_uuid': self.model_url.split("-")[-1],
'time_lapsed': time_lapsed,
#'backlink_files': [*st.session_state['uploaded_files']],
#'seo_file': [self.uploaded_file_seo.name] if self.uploaded_file_seo else ['Not available'],
'payload': payload_txt,
'result': result,
}
'''
if self.bounce_rate:
st.session_state['bounce_rate'] = 'uploaded'
collect_telemetry(debug_info_bounce_rate)
if self.page_index:
st.session_state['pages_index'] = 'uploaded'
collect_telemetry(debug_info_pages_index)
if self.others:
st.session_state['others'] = 'uploaded'
collect_telemetry(debug_info_traffic_aqcuisition)
if self.uploaded_file:
st.session_state['df_traffic'] = 'uploaded'
collect_telemetry(debug_info_traffic_channels)
if self.uploaded_file_seo:
st.session_state['df_seo'] = 'uploaded'
collect_telemetry(debug_info_seo_keywords)
#with st.expander("Debug information", icon="⚙"):
# st.write(debug_info)
#del st.session_state[df_traffic]
#del st.session_state[df_seo]
#del st.session_state[others]
st.session_state['analyzing'] = False
def row1(self):
#st.write("") # FOR SPACINGs
#st.write("") # FOR SPACINGs
'''
self.uploaded_files = st.file_uploader("Backlinks (SEO)", type=['pdf', 'csv'], accept_multiple_files=True, key="seo1")
if self.uploaded_files:
self.delete_sessions()
upload.upload_file_seo(self.uploaded_files)
self.file_dict = upload.file_dict
'''
#st.write("") # FOR THE HIDE BUTTON
self.uploaded_file_seo = st.file_uploader("SEO Keywords - SEMRush", type='csv', key="seo2")
if self.uploaded_file_seo:
self.delete_sessions()
try:
encoding_seo = self.detect_encoding(self.uploaded_file_seo)
st.session_state['df_seo'] = pd.read_csv(self.uploaded_file_seo, encoding=encoding_seo, low_memory=False)
except Exception:
pass
self.uploaded_file = st.file_uploader("Traffic Channels - SEMRush", type='csv')
if self.uploaded_file:
self.delete_sessions()
try:
encoding = self.detect_encoding(self.uploaded_file)
st.session_state['df_traffic'] = pd.read_csv(self.uploaded_file, encoding=encoding, low_memory=False)
except Exception:
pass
st.write("") # FOR THE HIDE BUTTON
self.others = st.file_uploader("Traffic Acquisition - GA4", type='csv', key="seo5")
if self.others:
try:
st.session_state['others'] = pd.read_csv(self.others, skiprows=9)
except Exception:
pass
self.page_index = st.text_input("Pages Indexed - Google Search Console:", placeholder='Enter Pages Indexed')
self.bounce_rate = st.text_input("Bounce Rate - GA4:", placeholder='Enter Bounce Rate')
followers = {
'Pages Indexed': self.page_index if self.page_index else 'N/A',
'Bounce Rate': self.bounce_rate if self.bounce_rate else 'N/A'
}
#st.write("") # FOR THE HIDE BUTTON
#st.write("") # FOR THE HIDE BUTTON
#st.write("AI Analyst Output: ")
st.session_state['analyzing'] = False
#st.write("") # FOR THE HIDE BUTTON
#analyze_button = st.button("Analyze", disabled=initialize_analyze_session())
self.process()
if __name__ == "__main__":
st.set_page_config(layout="wide")
upload = uploadFile()
|