Spaces:
Runtime error
Runtime error
Ronio Jerico Roque commited on
Commit ·
cfac8f5
1
Parent(s): fd22ee2
refactor: update analyst page navigation and import structure; add file upload functionality
Browse files- app.py +1 -1
- classes/Seo.py +234 -0
- classes/Seo_Off_Page.py +157 -0
- classes/Seo_On_Page.py +160 -0
- helper/upload_File.py +32 -0
- helper/upload_traffic.py +33 -0
- pages/EDA.py +0 -85
- pages/Traffic_EDA.py +0 -105
- pages/agent_1.py +1 -2
- pages/agent_2.py +1 -2
- pages/agent_3.py +1 -1
- EDA.py → pages/save_state/EDA.py +0 -0
- pages/{image.py → save_state/image.py} +0 -0
- pages/{save_state.py → save_state/save_state.py} +0 -0
- parent.py +0 -507
app.py
CHANGED
|
@@ -81,7 +81,7 @@ def row2():
|
|
| 81 |
st.header("Analyst 7")
|
| 82 |
st.write("")
|
| 83 |
if st.button("Load Analyst", key=7, disabled=True):
|
| 84 |
-
st.switch_page("pages/
|
| 85 |
|
| 86 |
with col4:
|
| 87 |
st.header("Analyst 8")
|
|
|
|
| 81 |
st.header("Analyst 7")
|
| 82 |
st.write("")
|
| 83 |
if st.button("Load Analyst", key=7, disabled=True):
|
| 84 |
+
st.switch_page("pages/image.py")
|
| 85 |
|
| 86 |
with col4:
|
| 87 |
st.header("Analyst 8")
|
classes/Seo.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio import JSON
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import pymupdf
|
| 8 |
+
import time
|
| 9 |
+
import chardet
|
| 10 |
+
import json
|
| 11 |
+
from telemetry import collect_telemetry
|
| 12 |
+
from helper.upload_File import uploadFile
|
| 13 |
+
from helper.upload_traffic import upload_traffic
|
| 14 |
+
|
| 15 |
+
class SeoAnalyst:
|
| 16 |
+
def __init__(self, model_url, analyst_name, data_src, analyst_description):
|
| 17 |
+
self.uploaded_files = []
|
| 18 |
+
self.file_dict = {}
|
| 19 |
+
self.model_url = model_url
|
| 20 |
+
self.analyst_name = analyst_name
|
| 21 |
+
self.data_src = data_src
|
| 22 |
+
self.analyst_description = analyst_description
|
| 23 |
+
self.initialize()
|
| 24 |
+
self.initialize_analyze_session()
|
| 25 |
+
self.row1()
|
| 26 |
+
|
| 27 |
+
def initialize(self):
|
| 28 |
+
# FOR ENV
|
| 29 |
+
load_dotenv()
|
| 30 |
+
|
| 31 |
+
# AGENT NAME
|
| 32 |
+
st.header(self.analyst_name)
|
| 33 |
+
|
| 34 |
+
# EVALUATION FORM LINK
|
| 35 |
+
url = os.getenv('Link')
|
| 36 |
+
st.write('Evaluation Form: [Link](%s)' % url)
|
| 37 |
+
|
| 38 |
+
# RETURN BUTTON
|
| 39 |
+
try:
|
| 40 |
+
if st.button("Return", type='primary'):
|
| 41 |
+
st.switch_page("./app.py")
|
| 42 |
+
except Exception:
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
def request_model(self, payload_txt):
|
| 46 |
+
response = requests.post(self.model_url, json=payload_txt)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
output = response.json()
|
| 49 |
+
output_dict = {key: value for key, value in output.items()}
|
| 50 |
+
output_str = json.dumps(output_dict, indent=8, sort_keys=False)
|
| 51 |
+
|
| 52 |
+
categories = []
|
| 53 |
+
current_footprint = []
|
| 54 |
+
number_of_backlinks = []
|
| 55 |
+
|
| 56 |
+
for key, value in output.items():
|
| 57 |
+
if key == 'json':
|
| 58 |
+
for item in value:
|
| 59 |
+
categories.append(item.get('category', 'N/A').replace('_', ' ').title())
|
| 60 |
+
current_footprint.append(item.get('current_footprint', 'N/A'))
|
| 61 |
+
number_of_backlinks.append(item.get('best_of_breed_solution', 'N/A'))
|
| 62 |
+
|
| 63 |
+
output = ""
|
| 64 |
+
for i in range(len(categories)):
|
| 65 |
+
output += f"\n\n---\n **Category:** {categories[i]}"
|
| 66 |
+
output += f"\n\n **Current Footprint:** {current_footprint[i]}\n\n"
|
| 67 |
+
output += f"**Number of Backlinks:** {number_of_backlinks[i]}"
|
| 68 |
+
|
| 69 |
+
return output
|
| 70 |
+
|
| 71 |
+
def initialize_analyze_session(self):
|
| 72 |
+
if 'analyzing' not in st.session_state:
|
| 73 |
+
st.session_state['analyzing'] = False
|
| 74 |
+
return st.session_state.get('analyzing', False)
|
| 75 |
+
|
| 76 |
+
def hide_button(self):
|
| 77 |
+
if st.session_state['analyzing'] == True:
|
| 78 |
+
st.markdown(
|
| 79 |
+
"""
|
| 80 |
+
<style>
|
| 81 |
+
.element-container:nth-of-type(5) button {
|
| 82 |
+
display: none;
|
| 83 |
+
}
|
| 84 |
+
</style>
|
| 85 |
+
""",
|
| 86 |
+
unsafe_allow_html=True,
|
| 87 |
+
)
|
| 88 |
+
elif st.session_state['analyzing'] == False:
|
| 89 |
+
st.markdown(
|
| 90 |
+
"""
|
| 91 |
+
<style>
|
| 92 |
+
element-container:nth-of-type(5) button {
|
| 93 |
+
display: inline;
|
| 94 |
+
}
|
| 95 |
+
</style>
|
| 96 |
+
""",
|
| 97 |
+
unsafe_allow_html=True,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
def detect_encoding(self, uploaded_file):
|
| 101 |
+
result = chardet.detect(uploaded_file.read(100000))
|
| 102 |
+
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 103 |
+
return result['encoding']
|
| 104 |
+
|
| 105 |
+
def keyword_ranking(self, df_seo):
|
| 106 |
+
keyword_ranking = df_seo
|
| 107 |
+
st.session_state['keyword_ranking'] = keyword_ranking
|
| 108 |
+
|
| 109 |
+
keywords_ranking_sorted = keyword_ranking.sort_values("Position", ascending=True)
|
| 110 |
+
|
| 111 |
+
keywords_ranking_top_10 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 10].shape[0]
|
| 112 |
+
keywords_ranking_top_100 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 100].shape[0]
|
| 113 |
+
|
| 114 |
+
keyword_ranking = {
|
| 115 |
+
'Keyword_top_10': keywords_ranking_top_10,
|
| 116 |
+
'Keyword_top_100': keywords_ranking_top_100
|
| 117 |
+
}
|
| 118 |
+
st.session_state['keyword_ranking'] = keyword_ranking
|
| 119 |
+
def traffic_files(self, df):
|
| 120 |
+
traffic_channels = df
|
| 121 |
+
traffic_channels.rename(columns={traffic_channels.columns[0]: 'date'}, inplace=True)
|
| 122 |
+
traffic_channels['date'] = pd.to_datetime(traffic_channels['date'], format='mixed')
|
| 123 |
+
|
| 124 |
+
traffic_channels_sort = traffic_channels.sort_values("date", ascending=False)
|
| 125 |
+
|
| 126 |
+
organic_traffic = traffic_channels_sort['Organic Search'].values[0]
|
| 127 |
+
paid_traffic = traffic_channels_sort['Paid Search'].values[0]
|
| 128 |
+
direct_traffic = traffic_channels_sort['Direct'].values[0]
|
| 129 |
+
referral_traffic = traffic_channels_sort['Referral'].values[0]
|
| 130 |
+
|
| 131 |
+
st.session_state['organic_traffic'] = organic_traffic
|
| 132 |
+
st.session_state['paid_traffic'] = paid_traffic
|
| 133 |
+
st.session_state['direct_traffic'] = direct_traffic
|
| 134 |
+
st.session_state['referral_traffic'] = referral_traffic
|
| 135 |
+
|
| 136 |
+
def row1(self):
|
| 137 |
+
col1, col2 = st.columns(2, gap="medium")
|
| 138 |
+
with col1:
|
| 139 |
+
st.write("") # FOR SPACING
|
| 140 |
+
st.write(self.data_src)
|
| 141 |
+
self.uploaded_files = st.file_uploader(self.analyst_description, type=['pdf', 'csv'], accept_multiple_files=True)
|
| 142 |
+
if self.uploaded_files:
|
| 143 |
+
upload.multiple_upload_file(self.uploaded_files)
|
| 144 |
+
self.file_dict = upload.file_dict
|
| 145 |
+
|
| 146 |
+
self.uploaded_file = st.file_uploader("Upload Traffic Files CSV", type='csv')
|
| 147 |
+
if self.uploaded_file:
|
| 148 |
+
encoding = self.detect_encoding(self.uploaded_file)
|
| 149 |
+
st.session_state['df'] = pd.read_csv(self.uploaded_file, encoding=encoding, low_memory=False)
|
| 150 |
+
|
| 151 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 152 |
+
self.uploaded_file_seo = st.file_uploader("Upload SEO Keywords CSV", type='csv', key=3)
|
| 153 |
+
if self.uploaded_file_seo:
|
| 154 |
+
encoding_seo = self.detect_encoding(self.uploaded_file_seo)
|
| 155 |
+
st.session_state['df_seo'] = pd.read_csv(self.uploaded_file_seo, encoding=encoding_seo, low_memory=False)
|
| 156 |
+
|
| 157 |
+
with col2:
|
| 158 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 159 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 160 |
+
st.write("AI Analyst Output: ")
|
| 161 |
+
st.session_state['analyzing'] = False
|
| 162 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 163 |
+
analyze_button = st.button("Analyze", disabled=self.initialize_analyze_session())
|
| 164 |
+
start_time = time.time()
|
| 165 |
+
if analyze_button:
|
| 166 |
+
st.session_state['analyzing'] = True
|
| 167 |
+
self.hide_button()
|
| 168 |
+
if self.uploaded_files:
|
| 169 |
+
combined_text = ""
|
| 170 |
+
with st.spinner('Analyzing...', show_time=True):
|
| 171 |
+
st.write('')
|
| 172 |
+
for file_info in st.session_state['uploaded_files'].values():
|
| 173 |
+
if file_info['type'] == 'pdf':
|
| 174 |
+
combined_text += file_info['content'] + "\n"
|
| 175 |
+
elif file_info['type'] == 'csv':
|
| 176 |
+
combined_text += file_info['content'].to_csv(index=True) + "\n"
|
| 177 |
+
|
| 178 |
+
# INITIALIZING SESSIONS
|
| 179 |
+
try:
|
| 180 |
+
df = st.session_state['df']
|
| 181 |
+
df_seo = st.session_state['df_seo']
|
| 182 |
+
self.keyword_ranking(df_seo)
|
| 183 |
+
self.traffic_files(df)
|
| 184 |
+
keyword_ranking = st.session_state['keyword_ranking']
|
| 185 |
+
organic_traffic = st.session_state['organic_traffic']
|
| 186 |
+
paid_traffic = st.session_state['paid_traffic']
|
| 187 |
+
direct_traffic = st.session_state['direct_traffic']
|
| 188 |
+
referral_traffic = st.session_state['referral_traffic']
|
| 189 |
+
|
| 190 |
+
combined_text += df.to_csv(index=True)
|
| 191 |
+
combined_text += f"\nKeyword Ranking Top 10: {keyword_ranking['Keyword_top_10']}"
|
| 192 |
+
combined_text += f"\nKeyword Ranking Top 100: {keyword_ranking['Keyword_top_100']}\n\n"
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
combined_text += df_seo.to_csv(index=True)
|
| 196 |
+
combined_text += f"\nOrganic Traffic: {organic_traffic}"
|
| 197 |
+
combined_text += f"\nPaid Traffic: {paid_traffic}"
|
| 198 |
+
combined_text += f"\nDirect Traffic: {direct_traffic}"
|
| 199 |
+
combined_text += f"\nReferral Traffic: {referral_traffic}"
|
| 200 |
+
except KeyError:
|
| 201 |
+
pass
|
| 202 |
+
|
| 203 |
+
# OUTPUT FOR SEO ANALYST
|
| 204 |
+
payload_txt = {"question": combined_text}
|
| 205 |
+
result = self.request_model(payload_txt)
|
| 206 |
+
|
| 207 |
+
end_time = time.time()
|
| 208 |
+
time_lapsed = end_time - start_time
|
| 209 |
+
debug_info = {'analyst': self.analyst_name,'url_uuid': self.model_url.split("-")[-1],'time_lapsed' : time_lapsed, 'files': [*st.session_state['uploaded_files'],],'payload': payload_txt, 'result': result}
|
| 210 |
+
|
| 211 |
+
collect_telemetry(debug_info)
|
| 212 |
+
|
| 213 |
+
with st.expander("AI Analysis", expanded=True, icon="🤖"):
|
| 214 |
+
st.write(debug_info.pop("result"))
|
| 215 |
+
|
| 216 |
+
with st.expander("Debug information", icon="⚙"):
|
| 217 |
+
st.write(debug_info)
|
| 218 |
+
|
| 219 |
+
for df in st.session_state.keys():
|
| 220 |
+
del st.session_state[df]
|
| 221 |
+
for df_seo in st.session_state.keys():
|
| 222 |
+
del st.session_state[df_seo]
|
| 223 |
+
|
| 224 |
+
st.session_state['analyzing'] = False
|
| 225 |
+
else:
|
| 226 |
+
st.info("Please upload CSV or PDF files first.")
|
| 227 |
+
st.session_state['analyzing'] = False
|
| 228 |
+
self.hide_button()
|
| 229 |
+
|
| 230 |
+
if __name__ == "__main__":
|
| 231 |
+
st.set_page_config(layout="wide")
|
| 232 |
+
|
| 233 |
+
upload = uploadFile()
|
| 234 |
+
traffic = upload_traffic()
|
classes/Seo_Off_Page.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import chardet
|
| 7 |
+
from telemetry import collect_telemetry
|
| 8 |
+
from helper.upload_File import uploadFile
|
| 9 |
+
|
| 10 |
+
class SeoOffPageAnalyst:
|
| 11 |
+
def __init__(self, model_url, analyst_name, data_src, analyst_description):
|
| 12 |
+
self.uploaded_files = []
|
| 13 |
+
self.file_dict = {}
|
| 14 |
+
self.model_url = model_url
|
| 15 |
+
self.analyst_name = analyst_name
|
| 16 |
+
self.data_src = data_src
|
| 17 |
+
self.analyst_description = analyst_description
|
| 18 |
+
self.initialize()
|
| 19 |
+
self.initialize_analyze_session()
|
| 20 |
+
self.row1()
|
| 21 |
+
|
| 22 |
+
def initialize(self):
|
| 23 |
+
# FOR ENV
|
| 24 |
+
load_dotenv()
|
| 25 |
+
|
| 26 |
+
# AGENT NAME
|
| 27 |
+
st.header(self.analyst_name)
|
| 28 |
+
|
| 29 |
+
# EVALUATION FORM LINK
|
| 30 |
+
url = os.getenv('Link')
|
| 31 |
+
st.write('Evaluation Form: [Link](%s)' % url)
|
| 32 |
+
|
| 33 |
+
# RETURN BUTTON
|
| 34 |
+
try:
|
| 35 |
+
if st.button("Return", type='primary'):
|
| 36 |
+
st.switch_page("./app.py")
|
| 37 |
+
except Exception:
|
| 38 |
+
pass
|
| 39 |
+
|
| 40 |
+
def request_model(self, payload_txt):
|
| 41 |
+
response = requests.post(self.model_url, json=payload_txt)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
output = response.json()
|
| 44 |
+
|
| 45 |
+
categories = []
|
| 46 |
+
current_footprint = []
|
| 47 |
+
number_of_backlinks = []
|
| 48 |
+
|
| 49 |
+
for key, value in output.items():
|
| 50 |
+
if key == 'json':
|
| 51 |
+
for item in value:
|
| 52 |
+
categories.append(item.get('elements', 'N/A').replace('_', ' ').title())
|
| 53 |
+
current_footprint.append(item.get('remarks', 'N/A'))
|
| 54 |
+
number_of_backlinks.append(item.get('count', 'N/A'))
|
| 55 |
+
|
| 56 |
+
output = ""
|
| 57 |
+
for i in range(len(categories)):
|
| 58 |
+
output += f"\n\n---\n **Category:** {categories[i]}"
|
| 59 |
+
output += f"\n\n **Remarks:** {current_footprint[i]}\n\n"
|
| 60 |
+
output += f"**Count:** {number_of_backlinks[i]}"
|
| 61 |
+
|
| 62 |
+
return output
|
| 63 |
+
|
| 64 |
+
def initialize_analyze_session(self):
|
| 65 |
+
if 'analyzing' not in st.session_state:
|
| 66 |
+
st.session_state['analyzing'] = False
|
| 67 |
+
return st.session_state.get('analyzing', False)
|
| 68 |
+
|
| 69 |
+
def hide_button(self):
|
| 70 |
+
if st.session_state['analyzing'] == True:
|
| 71 |
+
st.markdown(
|
| 72 |
+
"""
|
| 73 |
+
<style>
|
| 74 |
+
.element-container:nth-of-type(5) button {
|
| 75 |
+
display: none;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
""",
|
| 79 |
+
unsafe_allow_html=True,
|
| 80 |
+
)
|
| 81 |
+
elif st.session_state['analyzing'] == False:
|
| 82 |
+
st.markdown(
|
| 83 |
+
"""
|
| 84 |
+
<style>
|
| 85 |
+
element-container:nth-of-type(5) button {
|
| 86 |
+
display: inline;
|
| 87 |
+
}
|
| 88 |
+
</style>
|
| 89 |
+
""",
|
| 90 |
+
unsafe_allow_html=True,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
def detect_encoding(self, uploaded_file):
|
| 94 |
+
result = chardet.detect(uploaded_file.read(100000))
|
| 95 |
+
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 96 |
+
return result['encoding']
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def row1(self):
|
| 100 |
+
col1, col2 = st.columns(2, gap="medium")
|
| 101 |
+
with col1:
|
| 102 |
+
st.write("") # FOR SPACING
|
| 103 |
+
st.write(self.data_src)
|
| 104 |
+
self.uploaded_files = st.file_uploader(self.analyst_description, type=['pdf', 'csv'], accept_multiple_files=True)
|
| 105 |
+
if self.uploaded_files:
|
| 106 |
+
upload.multiple_upload_file(self.uploaded_files)
|
| 107 |
+
|
| 108 |
+
with col2:
|
| 109 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 110 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 111 |
+
st.write("AI Analyst Output: ")
|
| 112 |
+
st.session_state['analyzing'] = False
|
| 113 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 114 |
+
analyze_button = st.button("Analyze", disabled=self.initialize_analyze_session())
|
| 115 |
+
start_time = time.time()
|
| 116 |
+
if analyze_button:
|
| 117 |
+
st.session_state['analyzing'] = True
|
| 118 |
+
self.hide_button()
|
| 119 |
+
if self.uploaded_files:
|
| 120 |
+
combined_text = ""
|
| 121 |
+
with st.spinner('Analyzing...', show_time=True):
|
| 122 |
+
st.write('')
|
| 123 |
+
for file_info in st.session_state['uploaded_files'].values():
|
| 124 |
+
if file_info['type'] == 'pdf':
|
| 125 |
+
combined_text += file_info['content'] + "\n"
|
| 126 |
+
elif file_info['type'] == 'csv':
|
| 127 |
+
combined_text += file_info['content'].to_csv(index=True) + "\n"
|
| 128 |
+
|
| 129 |
+
# OUTPUT FOR SEO ANALYST
|
| 130 |
+
payload_txt = {"question": combined_text}
|
| 131 |
+
result = self.request_model(payload_txt)
|
| 132 |
+
|
| 133 |
+
end_time = time.time()
|
| 134 |
+
time_lapsed = end_time - start_time
|
| 135 |
+
|
| 136 |
+
debug_info = {'analyst': self.analyst_name,'url_uuid': self.model_url.split("-")[-1],'time_lapsed' : time_lapsed, 'files': [*st.session_state['uploaded_files']],'payload': payload_txt, 'result': result}
|
| 137 |
+
|
| 138 |
+
collect_telemetry(debug_info)
|
| 139 |
+
|
| 140 |
+
with st.expander("AI Analysis", expanded=True, icon="🤖"):
|
| 141 |
+
st.write(debug_info.pop("result"))
|
| 142 |
+
|
| 143 |
+
with st.expander("Debug information", icon="⚙"):
|
| 144 |
+
st.write(debug_info)
|
| 145 |
+
|
| 146 |
+
st.session_state['analyzing'] = False
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
else:
|
| 150 |
+
st.info("Please upload CSV or PDF files first.")
|
| 151 |
+
st.session_state['analyzing'] = False
|
| 152 |
+
self.hide_button()
|
| 153 |
+
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
st.set_page_config(layout="wide")
|
| 156 |
+
|
| 157 |
+
upload = uploadFile()
|
classes/Seo_On_Page.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio import JSON
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
+
import chardet
|
| 8 |
+
from telemetry import collect_telemetry
|
| 9 |
+
from helper.upload_File import uploadFile
|
| 10 |
+
|
| 11 |
+
class SeoOnPageAnalyst:
|
| 12 |
+
def __init__(self, model_url, analyst_name, data_src, analyst_description):
|
| 13 |
+
self.uploaded_files = []
|
| 14 |
+
self.file_dict = {}
|
| 15 |
+
self.model_url = model_url
|
| 16 |
+
self.analyst_name = analyst_name
|
| 17 |
+
self.data_src = data_src
|
| 18 |
+
self.analyst_description = analyst_description
|
| 19 |
+
self.initialize()
|
| 20 |
+
self.initialize_analyze_session()
|
| 21 |
+
self.row1()
|
| 22 |
+
|
| 23 |
+
def initialize(self):
|
| 24 |
+
# FOR ENV
|
| 25 |
+
load_dotenv()
|
| 26 |
+
|
| 27 |
+
# AGENT NAME
|
| 28 |
+
st.header(self.analyst_name)
|
| 29 |
+
|
| 30 |
+
# EVALUATION FORM LINK
|
| 31 |
+
url = os.getenv('Link')
|
| 32 |
+
st.write('Evaluation Form: [Link](%s)' % url)
|
| 33 |
+
|
| 34 |
+
# RETURN BUTTON
|
| 35 |
+
try:
|
| 36 |
+
if st.button("Return", type='primary'):
|
| 37 |
+
st.switch_page("./app.py")
|
| 38 |
+
except Exception:
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
def request_model(self, payload_txt):
|
| 42 |
+
response = requests.post(self.model_url, json=payload_txt)
|
| 43 |
+
response.raise_for_status()
|
| 44 |
+
output = response.json()
|
| 45 |
+
|
| 46 |
+
categories = []
|
| 47 |
+
current_footprint = []
|
| 48 |
+
number_of_backlinks = []
|
| 49 |
+
|
| 50 |
+
for key, value in output.items():
|
| 51 |
+
if key == 'json':
|
| 52 |
+
for item in value:
|
| 53 |
+
categories.append(item.get('elements', 'N/A').replace('_', ' ').title())
|
| 54 |
+
current_footprint.append(item.get('remarks', 'N/A'))
|
| 55 |
+
|
| 56 |
+
output = ""
|
| 57 |
+
for i in range(len(categories)):
|
| 58 |
+
output += f"\n\n---\n **Category:** {categories[i]}"
|
| 59 |
+
output += f"\n\n **Remarks:** {current_footprint[i]}\n\n"
|
| 60 |
+
|
| 61 |
+
return output
|
| 62 |
+
|
| 63 |
+
def initialize_analyze_session(self):
|
| 64 |
+
if 'analyzing' not in st.session_state:
|
| 65 |
+
st.session_state['analyzing'] = False
|
| 66 |
+
return st.session_state.get('analyzing', False)
|
| 67 |
+
|
| 68 |
+
def hide_button(self):
|
| 69 |
+
if st.session_state['analyzing'] == True:
|
| 70 |
+
st.markdown(
|
| 71 |
+
"""
|
| 72 |
+
<style>
|
| 73 |
+
.element-container:nth-of-type(5) button {
|
| 74 |
+
display: none;
|
| 75 |
+
}
|
| 76 |
+
</style>
|
| 77 |
+
""",
|
| 78 |
+
unsafe_allow_html=True,
|
| 79 |
+
)
|
| 80 |
+
elif st.session_state['analyzing'] == False:
|
| 81 |
+
st.markdown(
|
| 82 |
+
"""
|
| 83 |
+
<style>
|
| 84 |
+
element-container:nth-of-type(5) button {
|
| 85 |
+
display: inline;
|
| 86 |
+
}
|
| 87 |
+
</style>
|
| 88 |
+
""",
|
| 89 |
+
unsafe_allow_html=True,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
def detect_encoding(self, uploaded_file):
|
| 93 |
+
result = chardet.detect(uploaded_file.read(100000))
|
| 94 |
+
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 95 |
+
return result['encoding']
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def row1(self):
|
| 99 |
+
col1, col2 = st.columns(2, gap="medium")
|
| 100 |
+
with col1:
|
| 101 |
+
st.write("") # FOR SPACING
|
| 102 |
+
st.write(self.data_src)
|
| 103 |
+
self.uploaded_files = st.file_uploader(self.analyst_description, type=['pdf', 'csv'], accept_multiple_files=True)
|
| 104 |
+
if self.uploaded_files:
|
| 105 |
+
upload.multiple_upload_file(self.uploaded_files)
|
| 106 |
+
self.file_dict = upload.file_dict
|
| 107 |
+
|
| 108 |
+
with col2:
|
| 109 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 110 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 111 |
+
st.write("AI Analyst Output: ")
|
| 112 |
+
st.session_state['analyzing'] = False
|
| 113 |
+
st.write("") # FOR THE HIDE BUTTON
|
| 114 |
+
analyze_button = st.button("Analyze", disabled=self.initialize_analyze_session())
|
| 115 |
+
if analyze_button:
|
| 116 |
+
st.session_state['analyzing'] = True
|
| 117 |
+
self.hide_button()
|
| 118 |
+
start_time = time.time()
|
| 119 |
+
if self.uploaded_files:
|
| 120 |
+
combined_text = ""
|
| 121 |
+
with st.spinner('Analyzing...', show_time=True):
|
| 122 |
+
st.write('')
|
| 123 |
+
for file_info in st.session_state['uploaded_files'].values():
|
| 124 |
+
if file_info['type'] == 'pdf':
|
| 125 |
+
combined_text += file_info['content'] + "\n"
|
| 126 |
+
elif file_info['type'] == 'csv':
|
| 127 |
+
combined_text += file_info['content'].to_csv(index=True) + "\n"
|
| 128 |
+
|
| 129 |
+
# OUTPUT FOR SEO ANALYST
|
| 130 |
+
payload_txt = {"question": combined_text}
|
| 131 |
+
result = self.request_model(payload_txt)
|
| 132 |
+
end_time = time.time()
|
| 133 |
+
time_lapsed = end_time - start_time
|
| 134 |
+
|
| 135 |
+
debug_info = {'analyst': self.analyst_name,'url_uuid': self.model_url.split("-")[-1],'time_lapsed' : time_lapsed, 'files': [*st.session_state['uploaded_files']],'payload': payload_txt, 'result': result}
|
| 136 |
+
|
| 137 |
+
collect_telemetry(debug_info)
|
| 138 |
+
|
| 139 |
+
with st.expander("AI Analysis", expanded=True, icon="🤖"):
|
| 140 |
+
st.write(debug_info.pop("result"))
|
| 141 |
+
|
| 142 |
+
with st.expander("Debug information", icon="⚙"):
|
| 143 |
+
st.write(debug_info)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
st.session_state['analyzing'] = False
|
| 147 |
+
|
| 148 |
+
for df_seo in st.session_state.keys():
|
| 149 |
+
del st.session_state[df_seo]
|
| 150 |
+
self.file_dict.popitem()
|
| 151 |
+
|
| 152 |
+
else:
|
| 153 |
+
st.info("Please upload CSV or PDF files first.")
|
| 154 |
+
st.session_state['analyzing'] = False
|
| 155 |
+
self.hide_button()
|
| 156 |
+
|
| 157 |
+
if __name__ == "__main__":
|
| 158 |
+
st.set_page_config(layout="wide")
|
| 159 |
+
|
| 160 |
+
upload = uploadFile()
|
helper/upload_File.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio import JSON
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pymupdf
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class uploadFile:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.file_dict = {}
|
| 10 |
+
|
| 11 |
+
def multiple_upload_file(self, uploaded_files):
|
| 12 |
+
for _ in range(len(self.file_dict)):
|
| 13 |
+
self.file_dict.popitem()
|
| 14 |
+
|
| 15 |
+
for uploaded_file in uploaded_files:
|
| 16 |
+
if uploaded_file.type == "application/pdf":
|
| 17 |
+
with pymupdf.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
| 18 |
+
text = chr(12).join([page.get_text() for page in doc])
|
| 19 |
+
self.file_dict[uploaded_file.name] = {'type': 'pdf', 'content': text}
|
| 20 |
+
elif uploaded_file.type == "text/csv":
|
| 21 |
+
try:
|
| 22 |
+
df = pd.read_csv(uploaded_file)
|
| 23 |
+
self.file_dict[uploaded_file.name] = {'type': 'csv', 'content': df}
|
| 24 |
+
except Exception as e:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
st.session_state['uploaded_files'] = self.file_dict
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
app = uploadFile()
|
| 31 |
+
st.set_page_config(layout="wide")
|
| 32 |
+
|
helper/upload_traffic.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio import JSON
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pymupdf
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class upload_traffic:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.file_dict = {}
|
| 11 |
+
|
| 12 |
+
def multiple_upload_file(self, uploaded_files):
|
| 13 |
+
for _ in range(len(self.file_dict)):
|
| 14 |
+
self.file_dict.popitem()
|
| 15 |
+
|
| 16 |
+
for uploaded_file in uploaded_files:
|
| 17 |
+
if uploaded_file.type == "application/pdf":
|
| 18 |
+
with pymupdf.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
| 19 |
+
text = chr(12).join([page.get_text() for page in doc])
|
| 20 |
+
self.file_dict[uploaded_file.name] = {'type': 'pdf', 'content': text}
|
| 21 |
+
elif uploaded_file.type == "text/csv":
|
| 22 |
+
try:
|
| 23 |
+
df = pd.read_csv(uploaded_file)
|
| 24 |
+
self.file_dict[uploaded_file.name] = {'type': 'csv', 'content': df}
|
| 25 |
+
except Exception as e:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
st.session_state['uploaded_files'] = self.file_dict
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
app = upload_traffic()
|
| 32 |
+
st.set_page_config(layout="wide")
|
| 33 |
+
|
pages/EDA.py
DELETED
|
@@ -1,85 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from st_img_pastebutton import paste
|
| 3 |
-
from streamlit_paste_button import paste_image_button as pbutton
|
| 4 |
-
import requests
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
import os
|
| 7 |
-
import chardet
|
| 8 |
-
import pandas as pd
|
| 9 |
-
|
| 10 |
-
class EDAApp:
|
| 11 |
-
def __init__(self):
|
| 12 |
-
self.uploaded_file = None
|
| 13 |
-
self.img_b64 = None
|
| 14 |
-
self.initialize()
|
| 15 |
-
|
| 16 |
-
def initialize(self):
|
| 17 |
-
# FOR ENV
|
| 18 |
-
load_dotenv()
|
| 19 |
-
|
| 20 |
-
# FOR PAGE LAYOUT
|
| 21 |
-
st.set_page_config(layout="wide")
|
| 22 |
-
|
| 23 |
-
st.header('EDA')
|
| 24 |
-
if st.button("Return", key=1):
|
| 25 |
-
st.switch_page("./app.py")
|
| 26 |
-
|
| 27 |
-
def request_model(self, payload_txt):
|
| 28 |
-
response = requests.post(os.getenv('MODEL_Image_Describer'), json=payload_txt)
|
| 29 |
-
response.raise_for_status()
|
| 30 |
-
output = response.json()
|
| 31 |
-
result = f"{output['text']}"
|
| 32 |
-
return result
|
| 33 |
-
|
| 34 |
-
def detect_encoding(self, uploaded_file):
|
| 35 |
-
result = chardet.detect(uploaded_file.read(100000))
|
| 36 |
-
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 37 |
-
return result['encoding']
|
| 38 |
-
|
| 39 |
-
def summarize_data(self, df):
|
| 40 |
-
info = df.info()
|
| 41 |
-
st.session_state['info'] = info
|
| 42 |
-
|
| 43 |
-
summary_stats = df.describe(include='all')
|
| 44 |
-
st.session_state['summary_stats'] = summary_stats
|
| 45 |
-
|
| 46 |
-
def print_data_distribution(self, df):
|
| 47 |
-
distributions = {}
|
| 48 |
-
for column in df.select_dtypes(include=['number']).columns:
|
| 49 |
-
distribution = df[column].describe()
|
| 50 |
-
distributions[column] = distribution
|
| 51 |
-
st.session_state['distribution'] = distributions
|
| 52 |
-
|
| 53 |
-
def row1(self):
|
| 54 |
-
col1, col2 = st.columns(2, gap="medium")
|
| 55 |
-
with col1:
|
| 56 |
-
self.uploaded_file = st.file_uploader("Upload CSV", type='csv')
|
| 57 |
-
if self.uploaded_file is not None:
|
| 58 |
-
encoding = self.detect_encoding(self.uploaded_file)
|
| 59 |
-
df = pd.read_csv(self.uploaded_file, encoding=encoding, low_memory=False)
|
| 60 |
-
st.session_state['df'] = df
|
| 61 |
-
|
| 62 |
-
with col2:
|
| 63 |
-
st.write("AI Analyst Output: ")
|
| 64 |
-
if st.button("Analyze", key=2):
|
| 65 |
-
if 'df' in st.session_state:
|
| 66 |
-
df = st.session_state['df']
|
| 67 |
-
self.summarize_data(df)
|
| 68 |
-
self.print_data_distribution(df)
|
| 69 |
-
|
| 70 |
-
info = st.session_state['info']
|
| 71 |
-
summary_stats = st.session_state['summary_stats']
|
| 72 |
-
distribution = st.session_state['distribution']
|
| 73 |
-
|
| 74 |
-
payload_txt = {
|
| 75 |
-
"question": f"{info}, {summary_stats}, {distribution}",
|
| 76 |
-
"chatId": "some-session-id",
|
| 77 |
-
}
|
| 78 |
-
result = self.request_model(payload_txt)
|
| 79 |
-
st.write(result)
|
| 80 |
-
else:
|
| 81 |
-
st.write("Please upload a CSV file first.")
|
| 82 |
-
|
| 83 |
-
if __name__ == "__main__":
|
| 84 |
-
app = EDAApp()
|
| 85 |
-
app.row1()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pages/Traffic_EDA.py
DELETED
|
@@ -1,105 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from st_img_pastebutton import paste
|
| 3 |
-
from streamlit_paste_button import paste_image_button as pbutton
|
| 4 |
-
import requests
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
import os
|
| 7 |
-
import chardet
|
| 8 |
-
import pandas as pd
|
| 9 |
-
|
| 10 |
-
class EDAApp:
|
| 11 |
-
def __init__(self):
|
| 12 |
-
self.uploaded_file = None
|
| 13 |
-
self.img_b64 = None
|
| 14 |
-
self.initialize()
|
| 15 |
-
|
| 16 |
-
def initialize(self):
|
| 17 |
-
# FOR PAGE LAYOUT
|
| 18 |
-
st.set_page_config(layout="wide")
|
| 19 |
-
|
| 20 |
-
st.header('EDA')
|
| 21 |
-
if st.button("Return", key=1):
|
| 22 |
-
st.switch_page("./app.py")
|
| 23 |
-
|
| 24 |
-
def detect_encoding(self, uploaded_file):
|
| 25 |
-
result = chardet.detect(uploaded_file.read(100000))
|
| 26 |
-
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 27 |
-
return result['encoding']
|
| 28 |
-
|
| 29 |
-
def keyword_ranking(self, df_seo):
|
| 30 |
-
keyword_ranking = df_seo
|
| 31 |
-
st.session_state['keyword_ranking'] = keyword_ranking
|
| 32 |
-
|
| 33 |
-
keywords_ranking_sorted = keyword_ranking.sort_values("Position", ascending=True)
|
| 34 |
-
|
| 35 |
-
keywords_ranking_top_10 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 10].shape[0]
|
| 36 |
-
keywords_ranking_top_100 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 100].shape[0]
|
| 37 |
-
|
| 38 |
-
keyword_ranking = {
|
| 39 |
-
'Keyword_top_10': keywords_ranking_top_10,
|
| 40 |
-
'Keyword_top_100': keywords_ranking_top_100
|
| 41 |
-
}
|
| 42 |
-
st.session_state['keyword_ranking'] = keyword_ranking
|
| 43 |
-
|
| 44 |
-
def traffic_files(self, df):
|
| 45 |
-
traffic_channels = df
|
| 46 |
-
traffic_channels.rename(columns={traffic_channels.columns[0]: 'date'}, inplace=True)
|
| 47 |
-
traffic_channels['date'] = pd.to_datetime(traffic_channels['date'], format='mixed')
|
| 48 |
-
|
| 49 |
-
traffic_channels_sort = traffic_channels.sort_values("date", ascending=False)
|
| 50 |
-
|
| 51 |
-
organic_traffic = traffic_channels_sort['Organic Search'].values[0]
|
| 52 |
-
paid_traffic = traffic_channels_sort['Paid Search'].values[0]
|
| 53 |
-
direct_traffic = traffic_channels_sort['Direct'].values[0]
|
| 54 |
-
referral_traffic = traffic_channels_sort['Referral'].values[0]
|
| 55 |
-
|
| 56 |
-
st.session_state['organic_traffic'] = organic_traffic
|
| 57 |
-
st.session_state['paid_traffic'] = paid_traffic
|
| 58 |
-
st.session_state['direct_traffic'] = direct_traffic
|
| 59 |
-
st.session_state['referral_traffic'] = referral_traffic
|
| 60 |
-
|
| 61 |
-
def row1(self):
|
| 62 |
-
col1, col2 = st.columns(2, gap="medium")
|
| 63 |
-
with col1:
|
| 64 |
-
self.uploaded_file = st.file_uploader("Upload Traffic Files CSV", type='csv')
|
| 65 |
-
if self.uploaded_file is not None:
|
| 66 |
-
encoding = self.detect_encoding(self.uploaded_file)
|
| 67 |
-
df = pd.read_csv(self.uploaded_file, encoding=encoding, low_memory=False)
|
| 68 |
-
st.session_state['df'] = df
|
| 69 |
-
|
| 70 |
-
self.uploaded_file_seo = st.file_uploader("Upload SEO Keywords CSV", type='csv', key=3)
|
| 71 |
-
if self.uploaded_file_seo is not None:
|
| 72 |
-
encoding_seo = self.detect_encoding(self.uploaded_file_seo)
|
| 73 |
-
df_seo = pd.read_csv(self.uploaded_file_seo, encoding=encoding_seo, low_memory=False)
|
| 74 |
-
st.session_state['df_seo'] = df_seo
|
| 75 |
-
|
| 76 |
-
with col2:
|
| 77 |
-
st.write("AI Analyst Output: ")
|
| 78 |
-
if st.button("Analyze", key=2):
|
| 79 |
-
if 'df' in st.session_state or 'df_seo' in st.session_state:
|
| 80 |
-
df = st.session_state['df']
|
| 81 |
-
df_seo = st.session_state['df_seo']
|
| 82 |
-
self.keyword_ranking(df_seo)
|
| 83 |
-
self.traffic_files(df)
|
| 84 |
-
|
| 85 |
-
keyword_ranking = st.session_state['keyword_ranking']
|
| 86 |
-
organic_traffic = st.session_state['organic_traffic']
|
| 87 |
-
paid_traffic = st.session_state['paid_traffic']
|
| 88 |
-
direct_traffic = st.session_state['direct_traffic']
|
| 89 |
-
referral_traffic = st.session_state['referral_traffic']
|
| 90 |
-
|
| 91 |
-
output_10 = keyword_ranking['Keyword_top_10']
|
| 92 |
-
output_100 = keyword_ranking['Keyword_top_100']
|
| 93 |
-
|
| 94 |
-
st.write("Total Keywords Ranking Top 10: ", output_10)
|
| 95 |
-
st.write("Total Keywords Ranking Top 100: ", output_100)
|
| 96 |
-
st.write("Organic Traffic: ", organic_traffic)
|
| 97 |
-
st.write("Paid Traffic: ", paid_traffic)
|
| 98 |
-
st.write("Direct Traffic: ", direct_traffic)
|
| 99 |
-
st.write("Referral Traffic: ", referral_traffic)
|
| 100 |
-
else:
|
| 101 |
-
st.write("Please upload a CSV file first.")
|
| 102 |
-
|
| 103 |
-
if __name__ == "__main__":
|
| 104 |
-
app = EDAApp()
|
| 105 |
-
app.row1()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pages/agent_1.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
-
from parent import SeoOffPageAnalyst
|
| 3 |
from app import Analysts, Analysts_Description, Data_Source
|
| 4 |
-
|
| 5 |
|
| 6 |
# SPECIFY API URL & ANALYST NAME
|
| 7 |
start = SeoOffPageAnalyst(os.getenv('MODEL_Off_Page_Analyst'), Analysts['Analyst 1'], Data_Source['Analyst_Src 1'],Analysts_Description['Analyst_Desc 1'])
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from app import Analysts, Analysts_Description, Data_Source
|
| 3 |
+
from classes.Seo_Off_Page import SeoOffPageAnalyst
|
| 4 |
|
| 5 |
# SPECIFY API URL & ANALYST NAME
|
| 6 |
start = SeoOffPageAnalyst(os.getenv('MODEL_Off_Page_Analyst'), Analysts['Analyst 1'], Data_Source['Analyst_Src 1'],Analysts_Description['Analyst_Desc 1'])
|
pages/agent_2.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
-
from parent import SeoOnPageAnalyst
|
| 3 |
from app import Analysts, Analysts_Description, Data_Source
|
| 4 |
-
|
| 5 |
|
| 6 |
# SPECIFY API URL ANALYST NAME, DATA SOURCE & DESCRIPTION
|
| 7 |
start = SeoOnPageAnalyst(os.getenv('MODEL_On_Page_Analyst'), Analysts['Analyst 2'], Data_Source['Analyst_Src 2'],Analysts_Description['Analyst_Desc 2'])
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from app import Analysts, Analysts_Description, Data_Source
|
| 3 |
+
from classes.Seo_On_Page import SeoOnPageAnalyst
|
| 4 |
|
| 5 |
# SPECIFY API URL ANALYST NAME, DATA SOURCE & DESCRIPTION
|
| 6 |
start = SeoOnPageAnalyst(os.getenv('MODEL_On_Page_Analyst'), Analysts['Analyst 2'], Data_Source['Analyst_Src 2'],Analysts_Description['Analyst_Desc 2'])
|
pages/agent_3.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
-
from parent import SeoAnalyst
|
| 3 |
from app import Analysts, Analysts_Description, Data_Source
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
|
| 6 |
# SPECIFY API URL & ANALYST NAME
|
| 7 |
start = SeoAnalyst(os.getenv('MODEL_SEO_Analyst'), Analysts['Analyst 3'], Data_Source['Analyst_Src 3'],Analysts_Description['Analyst_Desc 3'])
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from app import Analysts, Analysts_Description, Data_Source
|
| 3 |
import streamlit as st
|
| 4 |
+
from classes.Seo import SeoAnalyst
|
| 5 |
|
| 6 |
# SPECIFY API URL & ANALYST NAME
|
| 7 |
start = SeoAnalyst(os.getenv('MODEL_SEO_Analyst'), Analysts['Analyst 3'], Data_Source['Analyst_Src 3'],Analysts_Description['Analyst_Desc 3'])
|
EDA.py → pages/save_state/EDA.py
RENAMED
|
File without changes
|
pages/{image.py → save_state/image.py}
RENAMED
|
File without changes
|
pages/{save_state.py → save_state/save_state.py}
RENAMED
|
File without changes
|
parent.py
CHANGED
|
@@ -145,513 +145,6 @@ if __name__ == "__main__":
|
|
| 145 |
app = uploadFile()
|
| 146 |
st.set_page_config(layout="wide")
|
| 147 |
|
| 148 |
-
class SeoAnalyst:
|
| 149 |
-
def __init__(self, model_url, analyst_name, data_src, analyst_description):
|
| 150 |
-
self.uploaded_files = []
|
| 151 |
-
self.file_dict = {}
|
| 152 |
-
self.model_url = model_url
|
| 153 |
-
self.analyst_name = analyst_name
|
| 154 |
-
self.data_src = data_src
|
| 155 |
-
self.analyst_description = analyst_description
|
| 156 |
-
self.initialize()
|
| 157 |
-
self.initialize_analyze_session()
|
| 158 |
-
self.row1()
|
| 159 |
-
|
| 160 |
-
def initialize(self):
|
| 161 |
-
# FOR ENV
|
| 162 |
-
load_dotenv()
|
| 163 |
-
|
| 164 |
-
# AGENT NAME
|
| 165 |
-
st.header(self.analyst_name)
|
| 166 |
-
|
| 167 |
-
# EVALUATION FORM LINK
|
| 168 |
-
url = os.getenv('Link')
|
| 169 |
-
st.write('Evaluation Form: [Link](%s)' % url)
|
| 170 |
-
|
| 171 |
-
# RETURN BUTTON
|
| 172 |
-
try:
|
| 173 |
-
if st.button("Return", type='primary'):
|
| 174 |
-
st.switch_page("./app.py")
|
| 175 |
-
except Exception:
|
| 176 |
-
pass
|
| 177 |
-
|
| 178 |
-
def request_model(self, payload_txt):
|
| 179 |
-
response = requests.post(self.model_url, json=payload_txt)
|
| 180 |
-
response.raise_for_status()
|
| 181 |
-
output = response.json()
|
| 182 |
-
output_dict = {key: value for key, value in output.items()}
|
| 183 |
-
output_str = json.dumps(output_dict, indent=8, sort_keys=False)
|
| 184 |
-
|
| 185 |
-
categories = []
|
| 186 |
-
current_footprint = []
|
| 187 |
-
number_of_backlinks = []
|
| 188 |
-
|
| 189 |
-
for key, value in output.items():
|
| 190 |
-
if key == 'json':
|
| 191 |
-
for item in value:
|
| 192 |
-
categories.append(item.get('category', 'N/A').replace('_', ' ').title())
|
| 193 |
-
current_footprint.append(item.get('current_footprint', 'N/A'))
|
| 194 |
-
number_of_backlinks.append(item.get('best_of_breed_solution', 'N/A'))
|
| 195 |
-
|
| 196 |
-
output = ""
|
| 197 |
-
for i in range(len(categories)):
|
| 198 |
-
output += f"\n\n---\n **Category:** {categories[i]}"
|
| 199 |
-
output += f"\n\n **Current Footprint:** {current_footprint[i]}\n\n"
|
| 200 |
-
output += f"**Number of Backlinks:** {number_of_backlinks[i]}"
|
| 201 |
-
|
| 202 |
-
return output
|
| 203 |
-
|
| 204 |
-
def initialize_analyze_session(self):
|
| 205 |
-
if 'analyzing' not in st.session_state:
|
| 206 |
-
st.session_state['analyzing'] = False
|
| 207 |
-
return st.session_state.get('analyzing', False)
|
| 208 |
-
|
| 209 |
-
def hide_button(self):
|
| 210 |
-
if st.session_state['analyzing'] == True:
|
| 211 |
-
st.markdown(
|
| 212 |
-
"""
|
| 213 |
-
<style>
|
| 214 |
-
.element-container:nth-of-type(5) button {
|
| 215 |
-
display: none;
|
| 216 |
-
}
|
| 217 |
-
</style>
|
| 218 |
-
""",
|
| 219 |
-
unsafe_allow_html=True,
|
| 220 |
-
)
|
| 221 |
-
elif st.session_state['analyzing'] == False:
|
| 222 |
-
st.markdown(
|
| 223 |
-
"""
|
| 224 |
-
<style>
|
| 225 |
-
element-container:nth-of-type(5) button {
|
| 226 |
-
display: inline;
|
| 227 |
-
}
|
| 228 |
-
</style>
|
| 229 |
-
""",
|
| 230 |
-
unsafe_allow_html=True,
|
| 231 |
-
)
|
| 232 |
-
|
| 233 |
-
def detect_encoding(self, uploaded_file):
|
| 234 |
-
result = chardet.detect(uploaded_file.read(100000))
|
| 235 |
-
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 236 |
-
return result['encoding']
|
| 237 |
-
|
| 238 |
-
def keyword_ranking(self, df_seo):
|
| 239 |
-
keyword_ranking = df_seo
|
| 240 |
-
st.session_state['keyword_ranking'] = keyword_ranking
|
| 241 |
-
|
| 242 |
-
keywords_ranking_sorted = keyword_ranking.sort_values("Position", ascending=True)
|
| 243 |
-
|
| 244 |
-
keywords_ranking_top_10 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 10].shape[0]
|
| 245 |
-
keywords_ranking_top_100 = keywords_ranking_sorted[keywords_ranking_sorted["Position"] <= 100].shape[0]
|
| 246 |
-
|
| 247 |
-
keyword_ranking = {
|
| 248 |
-
'Keyword_top_10': keywords_ranking_top_10,
|
| 249 |
-
'Keyword_top_100': keywords_ranking_top_100
|
| 250 |
-
}
|
| 251 |
-
st.session_state['keyword_ranking'] = keyword_ranking
|
| 252 |
-
|
| 253 |
-
def traffic_files(self, df):
|
| 254 |
-
traffic_channels = df
|
| 255 |
-
traffic_channels.rename(columns={traffic_channels.columns[0]: 'date'}, inplace=True)
|
| 256 |
-
traffic_channels['date'] = pd.to_datetime(traffic_channels['date'], format='mixed')
|
| 257 |
-
|
| 258 |
-
traffic_channels_sort = traffic_channels.sort_values("date", ascending=False)
|
| 259 |
-
|
| 260 |
-
organic_traffic = traffic_channels_sort['Organic Search'].values[0]
|
| 261 |
-
paid_traffic = traffic_channels_sort['Paid Search'].values[0]
|
| 262 |
-
direct_traffic = traffic_channels_sort['Direct'].values[0]
|
| 263 |
-
referral_traffic = traffic_channels_sort['Referral'].values[0]
|
| 264 |
-
|
| 265 |
-
st.session_state['organic_traffic'] = organic_traffic
|
| 266 |
-
st.session_state['paid_traffic'] = paid_traffic
|
| 267 |
-
st.session_state['direct_traffic'] = direct_traffic
|
| 268 |
-
st.session_state['referral_traffic'] = referral_traffic
|
| 269 |
-
|
| 270 |
-
def row1(self):
|
| 271 |
-
col1, col2 = st.columns(2, gap="medium")
|
| 272 |
-
with col1:
|
| 273 |
-
st.write("") # FOR SPACING
|
| 274 |
-
st.write(self.data_src)
|
| 275 |
-
self.uploaded_files = st.file_uploader(self.analyst_description, type=['pdf', 'csv'], accept_multiple_files=True)
|
| 276 |
-
if self.uploaded_files:
|
| 277 |
-
upload.multiple_upload_file(self.uploaded_files)
|
| 278 |
-
self.file_dict = upload.file_dict
|
| 279 |
-
|
| 280 |
-
self.uploaded_file = st.file_uploader("Upload Traffic Files CSV", type='csv')
|
| 281 |
-
if self.uploaded_file is not None:
|
| 282 |
-
encoding = self.detect_encoding(self.uploaded_file)
|
| 283 |
-
st.session_state['df'] = pd.read_csv(self.uploaded_file, encoding=encoding, low_memory=False)
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 287 |
-
self.uploaded_file_seo = st.file_uploader("Upload SEO Keywords CSV", type='csv', key=3)
|
| 288 |
-
if self.uploaded_file_seo is not None:
|
| 289 |
-
encoding_seo = self.detect_encoding(self.uploaded_file_seo)
|
| 290 |
-
st.session_state['df_seo'] = pd.read_csv(self.uploaded_file_seo, encoding=encoding_seo, low_memory=False)
|
| 291 |
-
|
| 292 |
-
with col2:
|
| 293 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 294 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 295 |
-
st.write("AI Analyst Output: ")
|
| 296 |
-
st.session_state['analyzing'] = False
|
| 297 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 298 |
-
analyze_button = st.button("Analyze", disabled=self.initialize_analyze_session())
|
| 299 |
-
start_time = time.time()
|
| 300 |
-
if analyze_button:
|
| 301 |
-
st.session_state['analyzing'] = True
|
| 302 |
-
self.hide_button()
|
| 303 |
-
if self.uploaded_files:
|
| 304 |
-
combined_text = ""
|
| 305 |
-
with st.spinner('Analyzing...', show_time=True):
|
| 306 |
-
st.write('')
|
| 307 |
-
for file_info in st.session_state['uploaded_files'].values():
|
| 308 |
-
if file_info['type'] == 'pdf':
|
| 309 |
-
combined_text += file_info['content'] + "\n"
|
| 310 |
-
elif file_info['type'] == 'csv':
|
| 311 |
-
combined_text += file_info['content'].to_csv(index=True) + "\n"
|
| 312 |
-
# INITIALIZING SESSIONS
|
| 313 |
-
try:
|
| 314 |
-
df = st.session_state['df']
|
| 315 |
-
df_seo = st.session_state['df_seo']
|
| 316 |
-
self.keyword_ranking(df_seo)
|
| 317 |
-
self.traffic_files(df)
|
| 318 |
-
keyword_ranking = st.session_state['keyword_ranking']
|
| 319 |
-
organic_traffic = st.session_state['organic_traffic']
|
| 320 |
-
paid_traffic = st.session_state['paid_traffic']
|
| 321 |
-
direct_traffic = st.session_state['direct_traffic']
|
| 322 |
-
referral_traffic = st.session_state['referral_traffic']
|
| 323 |
-
|
| 324 |
-
combined_text += df.to_csv(index=True)
|
| 325 |
-
combined_text += f"\nKeyword Ranking Top 10: {keyword_ranking['Keyword_top_10']}"
|
| 326 |
-
combined_text += f"\nKeyword Ranking Top 100: {keyword_ranking['Keyword_top_100']}\n\n"
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
combined_text += df_seo.to_csv(index=True)
|
| 330 |
-
combined_text += f"\nOrganic Traffic: {organic_traffic}"
|
| 331 |
-
combined_text += f"\nPaid Traffic: {paid_traffic}"
|
| 332 |
-
combined_text += f"\nDirect Traffic: {direct_traffic}"
|
| 333 |
-
combined_text += f"\nReferral Traffic: {referral_traffic}"
|
| 334 |
-
except KeyError:
|
| 335 |
-
pass
|
| 336 |
-
|
| 337 |
-
# OUTPUT FOR SEO ANALYST
|
| 338 |
-
payload_txt = {"question": combined_text}
|
| 339 |
-
result = self.request_model(payload_txt)
|
| 340 |
-
|
| 341 |
-
end_time = time.time()
|
| 342 |
-
time_lapsed = end_time - start_time
|
| 343 |
-
debug_info = {'analyst': self.analyst_name,'url_uuid': self.model_url.split("-")[-1],'time_lapsed' : time_lapsed, 'files': [*st.session_state['uploaded_files'],],'payload': payload_txt, 'result': result}
|
| 344 |
-
|
| 345 |
-
collect_telemetry(debug_info)
|
| 346 |
-
|
| 347 |
-
with st.expander("AI Analysis", expanded=True, icon="🤖"):
|
| 348 |
-
st.write(debug_info.pop("result"))
|
| 349 |
-
|
| 350 |
-
with st.expander("Debug information", icon="⚙"):
|
| 351 |
-
st.write(debug_info)
|
| 352 |
-
|
| 353 |
-
st.session_state['analyzing'] = False
|
| 354 |
-
|
| 355 |
-
else:
|
| 356 |
-
st.info("Please upload CSV or PDF files first.")
|
| 357 |
-
st.session_state['analyzing'] = False
|
| 358 |
-
self.hide_button()
|
| 359 |
-
|
| 360 |
-
if __name__ == "__main__":
|
| 361 |
-
st.set_page_config(layout="wide")
|
| 362 |
-
|
| 363 |
-
class SeoOnPageAnalyst:
|
| 364 |
-
def __init__(self, model_url, analyst_name, data_src, analyst_description):
|
| 365 |
-
self.uploaded_files = []
|
| 366 |
-
self.file_dict = {}
|
| 367 |
-
self.model_url = model_url
|
| 368 |
-
self.analyst_name = analyst_name
|
| 369 |
-
self.data_src = data_src
|
| 370 |
-
self.analyst_description = analyst_description
|
| 371 |
-
self.initialize()
|
| 372 |
-
self.initialize_analyze_session()
|
| 373 |
-
self.row1()
|
| 374 |
-
|
| 375 |
-
def initialize(self):
|
| 376 |
-
# FOR ENV
|
| 377 |
-
load_dotenv()
|
| 378 |
-
|
| 379 |
-
# AGENT NAME
|
| 380 |
-
st.header(self.analyst_name)
|
| 381 |
-
|
| 382 |
-
# EVALUATION FORM LINK
|
| 383 |
-
url = os.getenv('Link')
|
| 384 |
-
st.write('Evaluation Form: [Link](%s)' % url)
|
| 385 |
-
|
| 386 |
-
# RETURN BUTTON
|
| 387 |
-
try:
|
| 388 |
-
if st.button("Return", type='primary'):
|
| 389 |
-
st.switch_page("./app.py")
|
| 390 |
-
except Exception:
|
| 391 |
-
pass
|
| 392 |
-
|
| 393 |
-
def request_model(self, payload_txt):
|
| 394 |
-
response = requests.post(self.model_url, json=payload_txt)
|
| 395 |
-
response.raise_for_status()
|
| 396 |
-
output = response.json()
|
| 397 |
-
|
| 398 |
-
categories = []
|
| 399 |
-
current_footprint = []
|
| 400 |
-
number_of_backlinks = []
|
| 401 |
-
|
| 402 |
-
for key, value in output.items():
|
| 403 |
-
if key == 'json':
|
| 404 |
-
for item in value:
|
| 405 |
-
categories.append(item.get('elements', 'N/A').replace('_', ' ').title())
|
| 406 |
-
current_footprint.append(item.get('remarks', 'N/A'))
|
| 407 |
-
|
| 408 |
-
output = ""
|
| 409 |
-
for i in range(len(categories)):
|
| 410 |
-
output += f"\n\n---\n **Category:** {categories[i]}"
|
| 411 |
-
output += f"\n\n **Remarks:** {current_footprint[i]}\n\n"
|
| 412 |
-
|
| 413 |
-
return output
|
| 414 |
-
|
| 415 |
-
def initialize_analyze_session(self):
|
| 416 |
-
if 'analyzing' not in st.session_state:
|
| 417 |
-
st.session_state['analyzing'] = False
|
| 418 |
-
return st.session_state.get('analyzing', False)
|
| 419 |
-
|
| 420 |
-
def hide_button(self):
|
| 421 |
-
if st.session_state['analyzing'] == True:
|
| 422 |
-
st.markdown(
|
| 423 |
-
"""
|
| 424 |
-
<style>
|
| 425 |
-
.element-container:nth-of-type(5) button {
|
| 426 |
-
display: none;
|
| 427 |
-
}
|
| 428 |
-
</style>
|
| 429 |
-
""",
|
| 430 |
-
unsafe_allow_html=True,
|
| 431 |
-
)
|
| 432 |
-
elif st.session_state['analyzing'] == False:
|
| 433 |
-
st.markdown(
|
| 434 |
-
"""
|
| 435 |
-
<style>
|
| 436 |
-
element-container:nth-of-type(5) button {
|
| 437 |
-
display: inline;
|
| 438 |
-
}
|
| 439 |
-
</style>
|
| 440 |
-
""",
|
| 441 |
-
unsafe_allow_html=True,
|
| 442 |
-
)
|
| 443 |
-
|
| 444 |
-
def detect_encoding(self, uploaded_file):
|
| 445 |
-
result = chardet.detect(uploaded_file.read(100000))
|
| 446 |
-
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 447 |
-
return result['encoding']
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
def row1(self):
|
| 451 |
-
col1, col2 = st.columns(2, gap="medium")
|
| 452 |
-
with col1:
|
| 453 |
-
st.write("") # FOR SPACING
|
| 454 |
-
st.write(self.data_src)
|
| 455 |
-
self.uploaded_files = st.file_uploader(self.analyst_description, type=['pdf', 'csv'], accept_multiple_files=True)
|
| 456 |
-
if self.uploaded_files:
|
| 457 |
-
upload.multiple_upload_file(self.uploaded_files)
|
| 458 |
-
self.file_dict = upload.file_dict
|
| 459 |
-
|
| 460 |
-
with col2:
|
| 461 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 462 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 463 |
-
st.write("AI Analyst Output: ")
|
| 464 |
-
st.session_state['analyzing'] = False
|
| 465 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 466 |
-
analyze_button = st.button("Analyze", disabled=self.initialize_analyze_session())
|
| 467 |
-
if analyze_button:
|
| 468 |
-
st.session_state['analyzing'] = True
|
| 469 |
-
self.hide_button()
|
| 470 |
-
start_time = time.time()
|
| 471 |
-
if self.uploaded_files:
|
| 472 |
-
combined_text = ""
|
| 473 |
-
with st.spinner('Analyzing...', show_time=True):
|
| 474 |
-
st.write('')
|
| 475 |
-
for file_info in st.session_state['uploaded_files'].values():
|
| 476 |
-
if file_info['type'] == 'pdf':
|
| 477 |
-
combined_text += file_info['content'] + "\n"
|
| 478 |
-
elif file_info['type'] == 'csv':
|
| 479 |
-
combined_text += file_info['content'].to_csv(index=True) + "\n"
|
| 480 |
-
|
| 481 |
-
# OUTPUT FOR SEO ANALYST
|
| 482 |
-
payload_txt = {"question": combined_text}
|
| 483 |
-
result = self.request_model(payload_txt)
|
| 484 |
-
end_time = time.time()
|
| 485 |
-
time_lapsed = end_time - start_time
|
| 486 |
-
|
| 487 |
-
debug_info = {'analyst': self.analyst_name,'url_uuid': self.model_url.split("-")[-1],'time_lapsed' : time_lapsed, 'files': [*st.session_state['uploaded_files']],'payload': payload_txt, 'result': result}
|
| 488 |
-
|
| 489 |
-
collect_telemetry(debug_info)
|
| 490 |
-
|
| 491 |
-
with st.expander("AI Analysis", expanded=True, icon="🤖"):
|
| 492 |
-
st.write(debug_info.pop("result"))
|
| 493 |
-
|
| 494 |
-
with st.expander("Debug information", icon="⚙"):
|
| 495 |
-
st.write(debug_info)
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
st.session_state['analyzing'] = False
|
| 499 |
-
|
| 500 |
-
else:
|
| 501 |
-
st.info("Please upload CSV or PDF files first.")
|
| 502 |
-
st.session_state['analyzing'] = False
|
| 503 |
-
self.hide_button()
|
| 504 |
-
|
| 505 |
-
if __name__ == "__main__":
|
| 506 |
-
st.set_page_config(layout="wide")
|
| 507 |
-
|
| 508 |
-
class SeoOffPageAnalyst:
|
| 509 |
-
def __init__(self, model_url, analyst_name, data_src, analyst_description):
|
| 510 |
-
self.uploaded_files = []
|
| 511 |
-
self.file_dict = {}
|
| 512 |
-
self.model_url = model_url
|
| 513 |
-
self.analyst_name = analyst_name
|
| 514 |
-
self.data_src = data_src
|
| 515 |
-
self.analyst_description = analyst_description
|
| 516 |
-
self.initialize()
|
| 517 |
-
self.initialize_analyze_session()
|
| 518 |
-
self.row1()
|
| 519 |
-
|
| 520 |
-
def initialize(self):
|
| 521 |
-
# FOR ENV
|
| 522 |
-
load_dotenv()
|
| 523 |
-
|
| 524 |
-
# AGENT NAME
|
| 525 |
-
st.header(self.analyst_name)
|
| 526 |
-
|
| 527 |
-
# EVALUATION FORM LINK
|
| 528 |
-
url = os.getenv('Link')
|
| 529 |
-
st.write('Evaluation Form: [Link](%s)' % url)
|
| 530 |
-
|
| 531 |
-
# RETURN BUTTON
|
| 532 |
-
try:
|
| 533 |
-
if st.button("Return", type='primary'):
|
| 534 |
-
st.switch_page("./app.py")
|
| 535 |
-
except Exception:
|
| 536 |
-
pass
|
| 537 |
-
|
| 538 |
-
def request_model(self, payload_txt):
|
| 539 |
-
response = requests.post(self.model_url, json=payload_txt)
|
| 540 |
-
response.raise_for_status()
|
| 541 |
-
output = response.json()
|
| 542 |
-
|
| 543 |
-
categories = []
|
| 544 |
-
current_footprint = []
|
| 545 |
-
number_of_backlinks = []
|
| 546 |
-
|
| 547 |
-
for key, value in output.items():
|
| 548 |
-
if key == 'json':
|
| 549 |
-
for item in value:
|
| 550 |
-
categories.append(item.get('elements', 'N/A').replace('_', ' ').title())
|
| 551 |
-
current_footprint.append(item.get('remarks', 'N/A'))
|
| 552 |
-
number_of_backlinks.append(item.get('count', 'N/A'))
|
| 553 |
-
|
| 554 |
-
output = ""
|
| 555 |
-
for i in range(len(categories)):
|
| 556 |
-
output += f"\n\n---\n **Category:** {categories[i]}"
|
| 557 |
-
output += f"\n\n **Remarks:** {current_footprint[i]}\n\n"
|
| 558 |
-
output += f"**Count:** {number_of_backlinks[i]}"
|
| 559 |
-
|
| 560 |
-
return output
|
| 561 |
-
|
| 562 |
-
def initialize_analyze_session(self):
|
| 563 |
-
if 'analyzing' not in st.session_state:
|
| 564 |
-
st.session_state['analyzing'] = False
|
| 565 |
-
return st.session_state.get('analyzing', False)
|
| 566 |
-
|
| 567 |
-
def hide_button(self):
|
| 568 |
-
if st.session_state['analyzing'] == True:
|
| 569 |
-
st.markdown(
|
| 570 |
-
"""
|
| 571 |
-
<style>
|
| 572 |
-
.element-container:nth-of-type(5) button {
|
| 573 |
-
display: none;
|
| 574 |
-
}
|
| 575 |
-
</style>
|
| 576 |
-
""",
|
| 577 |
-
unsafe_allow_html=True,
|
| 578 |
-
)
|
| 579 |
-
elif st.session_state['analyzing'] == False:
|
| 580 |
-
st.markdown(
|
| 581 |
-
"""
|
| 582 |
-
<style>
|
| 583 |
-
element-container:nth-of-type(5) button {
|
| 584 |
-
display: inline;
|
| 585 |
-
}
|
| 586 |
-
</style>
|
| 587 |
-
""",
|
| 588 |
-
unsafe_allow_html=True,
|
| 589 |
-
)
|
| 590 |
-
|
| 591 |
-
def detect_encoding(self, uploaded_file):
|
| 592 |
-
result = chardet.detect(uploaded_file.read(100000))
|
| 593 |
-
uploaded_file.seek(0) # Reset file pointer to the beginning
|
| 594 |
-
return result['encoding']
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
def row1(self):
|
| 598 |
-
col1, col2 = st.columns(2, gap="medium")
|
| 599 |
-
with col1:
|
| 600 |
-
st.write("") # FOR SPACING
|
| 601 |
-
st.write(self.data_src)
|
| 602 |
-
self.uploaded_files = st.file_uploader(self.analyst_description, type=['pdf', 'csv'], accept_multiple_files=True)
|
| 603 |
-
if self.uploaded_files:
|
| 604 |
-
upload.multiple_upload_file(self.uploaded_files)
|
| 605 |
-
self.file_dict = upload.file_dict
|
| 606 |
-
|
| 607 |
-
with col2:
|
| 608 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 609 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 610 |
-
st.write("AI Analyst Output: ")
|
| 611 |
-
st.session_state['analyzing'] = False
|
| 612 |
-
st.write("") # FOR THE HIDE BUTTON
|
| 613 |
-
analyze_button = st.button("Analyze", disabled=self.initialize_analyze_session())
|
| 614 |
-
start_time = time.time()
|
| 615 |
-
if analyze_button:
|
| 616 |
-
st.session_state['analyzing'] = True
|
| 617 |
-
self.hide_button()
|
| 618 |
-
if self.uploaded_files:
|
| 619 |
-
combined_text = ""
|
| 620 |
-
with st.spinner('Analyzing...', show_time=True):
|
| 621 |
-
st.write('')
|
| 622 |
-
for file_info in st.session_state['uploaded_files'].values():
|
| 623 |
-
if file_info['type'] == 'pdf':
|
| 624 |
-
combined_text += file_info['content'] + "\n"
|
| 625 |
-
elif file_info['type'] == 'csv':
|
| 626 |
-
combined_text += file_info['content'].to_csv(index=True) + "\n"
|
| 627 |
-
|
| 628 |
-
# OUTPUT FOR SEO ANALYST
|
| 629 |
-
payload_txt = {"question": combined_text}
|
| 630 |
-
result = self.request_model(payload_txt)
|
| 631 |
-
|
| 632 |
-
end_time = time.time()
|
| 633 |
-
time_lapsed = end_time - start_time
|
| 634 |
-
|
| 635 |
-
debug_info = {'analyst': self.analyst_name,'url_uuid': self.model_url.split("-")[-1],'time_lapsed' : time_lapsed, 'files': [*st.session_state['uploaded_files']],'payload': payload_txt, 'result': result}
|
| 636 |
-
|
| 637 |
-
collect_telemetry(debug_info)
|
| 638 |
-
|
| 639 |
-
with st.expander("AI Analysis", expanded=True, icon="🤖"):
|
| 640 |
-
st.write(debug_info.pop("result"))
|
| 641 |
-
|
| 642 |
-
with st.expander("Debug information", icon="⚙"):
|
| 643 |
-
st.write(debug_info)
|
| 644 |
-
|
| 645 |
-
st.session_state['analyzing'] = False
|
| 646 |
-
|
| 647 |
-
else:
|
| 648 |
-
st.info("Please upload CSV or PDF files first.")
|
| 649 |
-
st.session_state['analyzing'] = False
|
| 650 |
-
self.hide_button()
|
| 651 |
-
|
| 652 |
-
if __name__ == "__main__":
|
| 653 |
-
st.set_page_config(layout="wide")
|
| 654 |
-
|
| 655 |
# # INITIALIZATION
|
| 656 |
# model_url = os.getenv('MODEL_On_Page_Analyst')
|
| 657 |
# analyst_name = Analysts['Analyst 1']
|
|
|
|
| 145 |
app = uploadFile()
|
| 146 |
st.set_page_config(layout="wide")
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
# # INITIALIZATION
|
| 149 |
# model_url = os.getenv('MODEL_On_Page_Analyst')
|
| 150 |
# analyst_name = Analysts['Analyst 1']
|