Ronio Jerico Roque commited on
Commit
e190b12
Β·
1 Parent(s): 76e7594

Refactor analysis page to include Snapshot analysis and streamline threading for concurrent execution

Browse files
classes/response_executive_summary.py CHANGED
@@ -1,11 +1,9 @@
1
- from urllib.parse import urlparse
2
  import streamlit as st
3
  import requests
4
  from dotenv import load_dotenv
5
  import os
6
  from helper.upload_response import upload_response
7
  from helper.upload_File import uploadFile
8
- import json
9
  from pymongo import MongoClient
10
  from helper.data_field import get_analyst_response
11
 
 
 
1
  import streamlit as st
2
  import requests
3
  from dotenv import load_dotenv
4
  import os
5
  from helper.upload_response import upload_response
6
  from helper.upload_File import uploadFile
 
7
  from pymongo import MongoClient
8
  from helper.data_field import get_analyst_response
9
 
classes/response_snapshot.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from dotenv import load_dotenv
4
+ import os
5
+ from helper.upload_response import upload_response
6
+ from helper.upload_File import uploadFile
7
+ from pymongo import MongoClient
8
+ from helper.data_field import get_analyst_response
9
+ import json
10
+
11
+
12
+ class Snapshot:
13
+ def __init__(self, model_url):
14
+ self.uploaded_files = []
15
+ self.file_dict = {}
16
+ self.model_url = model_url
17
+ #self.analyst_name = analyst_name
18
+ #self.data_src = data_src
19
+ #self.analyst_description = analyst_description
20
+ self.initialize()
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
+ def request_model(self, payload_txt, headers):
31
+ response = requests.post(self.model_url, json=payload_txt, headers=headers)
32
+ response.raise_for_status()
33
+ output = response.json()
34
+ #st.write(output)
35
+ text = output["outputs"][0]["outputs"][0]["results"]["text"]["data"]["text"]
36
+ text = json.loads(text)
37
+ #st.write(text)
38
+ return text
39
+
40
+ def fetch_data(self, data_field):
41
+ mongodb_uri = os.getenv("MONGODB_URI")
42
+ myclient = MongoClient(mongodb_uri)
43
+ mydb = myclient.get_database()
44
+ mycol = mydb["df_data"]
45
+
46
+ # Sort by timestamp field in descending order
47
+ x = mycol.find_one(
48
+ {"data_field": data_field},
49
+ sort=[("timestamp", -1)]
50
+ )
51
+
52
+ x = x["result"]
53
+ return x
54
+
55
+ def process(self):
56
+ with st.spinner('Snapshot...', show_time=True):
57
+ st.write('')
58
+ headers = {"Content-Type": "application/json", "x-api-key": f"{os.getenv('x-api-key')}"}
59
+ try:
60
+ payload_txt = {"input_value": self.payload, "output_type": "text", "input_type": "chat"}
61
+ payload_txt_model = self.request_model(payload_txt, headers)
62
+ debug_info = {'data_field' : 'Snapshot Analyst', 'result': payload_txt_model}
63
+ upload_response(debug_info)
64
+
65
+ except Exception as e:
66
+ pass
67
+ st.session_state['analyzing'] = False
68
+
69
+ def row1(self):
70
+ st.session_state['analyzing'] = False
71
+ self.payload = ""
72
+
73
+ self.website_and_tools_data = get_analyst_response("Website and Tools Analyst")
74
+ self.sem_data = get_analyst_response("SEM/PPC Analyst")
75
+ self.seo_data = get_analyst_response("SEO Analyst")
76
+ self.social_media_data = get_analyst_response("Social Media Analyst")
77
+ self.content_data = get_analyst_response("Content Analyst")
78
+ self.marketpalce_data = get_analyst_response("Marketplace Analyst")
79
+
80
+ analyst_data_dict = {
81
+ "Website and Tools": self.website_and_tools_data,
82
+ "SEM/PPC": self.sem_data,
83
+ "SEO": self.seo_data,
84
+ "Social Media": self.social_media_data,
85
+ "Content": self.content_data,
86
+ "Marketplace": self.marketpalce_data,
87
+ }
88
+
89
+ for analyst_name, data in analyst_data_dict.items():
90
+ self.payload += f"\n\n--- {analyst_name} Analysis ---\n"
91
+ if isinstance(data, list):
92
+ self.payload += "\n".join(map(str, data))
93
+ else:
94
+ self.payload += str(data)
95
+
96
+ self.process()
97
+
98
+ if __name__ == "__main__":
99
+ st.set_page_config(layout="wide")
100
+
101
+ upload = uploadFile()
pages/analyzing_page.py CHANGED
@@ -14,6 +14,7 @@ from classes.response_sem_ppc import Sem_PPC
14
  from classes.response_marketplace import Marketplace
15
  from classes.response_target_market import TargetMarket
16
  from classes.response_executive_summary import ExecutiveSummary
 
17
 
18
  def run_analysis():
19
  # Placeholders for status updates
@@ -25,10 +26,11 @@ def run_analysis():
25
  lld_pm_ln_status = st.empty()
26
  pull_through_offers_status = st.empty()
27
  content_status = st.empty()
28
- sem_ppc = st.empty()
29
- marketplace = st.empty()
30
- target_market = st.empty()
31
  executive_summary_status = st.empty()
 
32
 
33
  def run_off_page_analysis():
34
  try:
@@ -57,7 +59,7 @@ def run_analysis():
57
  website_and_tools_status.success("Website and Tools completed successfully.")
58
  return result
59
  except Exception as e:
60
- on_page_status.error(f"Website and Tools Analysis failed: {e}")
61
  return None
62
 
63
  def run_seo_analysis():
@@ -112,111 +114,109 @@ def run_analysis():
112
 
113
  def run_sem_ppc_analysis():
114
  try:
115
- sem_ppc.info("Starting SEM/PPC Analysis...")
116
  result = Sem_PPC(os.getenv('Model_SEM_PPC_Analyst'))
117
- sem_ppc.success("SEM/PPC Analysis completed successfully.")
118
  return result
119
  except Exception as e:
120
- sem_ppc.error(f"SEM/PPC Analysis failed: {e}")
121
  return None
122
 
123
  def run_marketplace_analysis():
124
  try:
125
- marketplace.info("Starting Marketplace Analysis...")
126
  result = Marketplace(os.getenv('Model_SEM_PPC_Analyst'))
127
- marketplace.success("Marketplace Analysis completed successfully.")
128
  return result
129
  except Exception as e:
130
- marketplace.error(f"Marketplace Analysis failed: {e}")
131
  return None
132
 
133
  def run_target_market_analysis():
134
  try:
135
- target_market.info("Starting Target Market Analysis...")
136
  result = TargetMarket(os.getenv('Model_Target_Market_Analyst'))
137
- target_market.success("Target Market Analysis completed successfully.")
138
  return result
139
  except Exception as e:
140
- target_market.error(f"Target Market Analysis failed: {e}")
141
  return None
142
-
 
 
 
 
 
 
 
 
 
 
143
  def run_executive_summary_analysis():
144
  try:
145
- executive_summary = st.empty().info("Starting Executive Summary Analysis...")
146
  result = ExecutiveSummary(os.getenv('Model_Executive_Summary_Analyst'))
147
- executive_summary = st.empty().success("Executive Summary Analysis completed successfully.")
148
  return result
149
  except Exception as e:
150
- executive_summary = st.empty().error(f"Executive Summary Analysis failed: {e}")
151
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
- # Create threads for concurrent execution
154
- off_page_thread = threading.Thread(target=run_off_page_analysis)
155
- on_page_thread = threading.Thread(target=run_on_page_analysis)
156
- website_and_tools_thread = threading.Thread(target=run_website_and_tools_analysis)
157
- seo_thread = threading.Thread(target=run_seo_analysis)
158
- social_media_thread = threading.Thread(target=run_social_media_analysis)
159
- llm_pm_ln_thread = threading.Thread(target=run_lld_pm_ln)
160
- pull_through_offers_thread = threading.Thread(target=run_pull_through_offers)
161
- content_thread = threading.Thread(target=run_content)
162
- content_sem_ppc_thread = threading.Thread(target=run_sem_ppc_analysis)
163
- marketplace_thread = threading.Thread(target=run_marketplace_analysis)
164
- target_market_thread = threading.Thread(target=run_target_market_analysis)
165
-
166
- # Attach Streamlit context to threads
167
- add_script_run_ctx(off_page_thread)
168
- add_script_run_ctx(on_page_thread)
169
- add_script_run_ctx(website_and_tools_thread)
170
- add_script_run_ctx(seo_thread)
171
- add_script_run_ctx(social_media_thread)
172
- add_script_run_ctx(llm_pm_ln_thread)
173
- add_script_run_ctx(pull_through_offers_thread)
174
- add_script_run_ctx(content_thread)
175
- add_script_run_ctx(content_sem_ppc_thread)
176
- add_script_run_ctx(marketplace_thread)
177
- add_script_run_ctx(target_market_thread)
178
-
179
- # Start threads
180
- off_page_thread.start()
181
- on_page_thread.start()
182
- website_and_tools_thread.start()
183
- seo_thread.start()
184
- social_media_thread.start()
185
- llm_pm_ln_thread.start()
186
- pull_through_offers_thread.start()
187
- content_thread.start()
188
- content_sem_ppc_thread.start()
189
- marketplace_thread.start()
190
- target_market_thread.start()
191
-
192
- # Wait for threads to complete
193
- off_page_thread.join()
194
- on_page_thread.join()
195
- website_and_tools_thread.join()
196
- seo_thread.join()
197
- social_media_thread.join()
198
- llm_pm_ln_thread.join()
199
- pull_through_offers_thread.join()
200
- content_thread.join()
201
- content_sem_ppc_thread.join()
202
- marketplace_thread.join()
203
- target_market_thread.join()
204
 
205
  st.markdown("---")
206
- executive_summary_status.info("Starting Executive Summary Analysis...")
207
- try:
208
- executive_summary = ExecutiveSummary(os.getenv('Model_Executive_Summary_Analyst'))
209
- executive_summary_status.success("Executive Summary Analysis completed successfully.")
210
- except Exception as e:
211
- executive_summary_status.error(f"Executive Summary Analysis failed: {e}")
212
- st.success("πŸŽ‰ All analyses completed!") # Final success message
213
-
 
 
 
 
 
 
 
 
 
 
 
 
214
  st.success("πŸŽ‰ All analyses completed!") # Final success message
 
215
  # --- Display Button After Completion ---
216
  if st.button("View Results", icon="πŸ“ƒ"):
217
  st.switch_page("pages/output.py")
218
 
219
  # Execute the analysis
220
  if st.button("Back"):
221
- st.switch_page("pages/home.py")
222
- run_analysis()
 
14
  from classes.response_marketplace import Marketplace
15
  from classes.response_target_market import TargetMarket
16
  from classes.response_executive_summary import ExecutiveSummary
17
+ from classes.response_snapshot import Snapshot
18
 
19
  def run_analysis():
20
  # Placeholders for status updates
 
26
  lld_pm_ln_status = st.empty()
27
  pull_through_offers_status = st.empty()
28
  content_status = st.empty()
29
+ sem_ppc_status = st.empty()
30
+ marketplace_status = st.empty()
31
+ target_market_status = st.empty()
32
  executive_summary_status = st.empty()
33
+ snapshot_status = st.empty()
34
 
35
  def run_off_page_analysis():
36
  try:
 
59
  website_and_tools_status.success("Website and Tools completed successfully.")
60
  return result
61
  except Exception as e:
62
+ website_and_tools_status.error(f"Website and Tools Analysis failed: {e}")
63
  return None
64
 
65
  def run_seo_analysis():
 
114
 
115
  def run_sem_ppc_analysis():
116
  try:
117
+ sem_ppc_status.info("Starting SEM/PPC Analysis...")
118
  result = Sem_PPC(os.getenv('Model_SEM_PPC_Analyst'))
119
+ sem_ppc_status.success("SEM/PPC Analysis completed successfully.")
120
  return result
121
  except Exception as e:
122
+ sem_ppc_status.error(f"SEM/PPC Analysis failed: {e}")
123
  return None
124
 
125
  def run_marketplace_analysis():
126
  try:
127
+ marketplace_status.info("Starting Marketplace Analysis...")
128
  result = Marketplace(os.getenv('Model_SEM_PPC_Analyst'))
129
+ marketplace_status.success("Marketplace Analysis completed successfully.")
130
  return result
131
  except Exception as e:
132
+ marketplace_status.error(f"Marketplace Analysis failed: {e}")
133
  return None
134
 
135
  def run_target_market_analysis():
136
  try:
137
+ target_market_status.info("Starting Target Market Analysis...")
138
  result = TargetMarket(os.getenv('Model_Target_Market_Analyst'))
139
+ target_market_status.success("Target Market Analysis completed successfully.")
140
  return result
141
  except Exception as e:
142
+ target_market_status.error(f"Target Market Analysis failed: {e}")
143
  return None
144
+
145
+ def run_snapshot_analysis():
146
+ try:
147
+ snapshot_status.info("Starting Snapshot by Channel Analysis...")
148
+ result = Snapshot(os.getenv('Model_Snapshot_by_Channel_Analyst'))
149
+ snapshot_status.success("Snapshot by Channel Analysis completed successfully.")
150
+ return result
151
+ except Exception as e:
152
+ snapshot_status.error(f"Snapshot by Channel Analysis failed: {e}")
153
+ return None
154
+
155
  def run_executive_summary_analysis():
156
  try:
157
+ executive_summary_status.info("Starting Executive Summary Analysis...")
158
  result = ExecutiveSummary(os.getenv('Model_Executive_Summary_Analyst'))
159
+ executive_summary_status.success("Executive Summary Analysis completed successfully.")
160
  return result
161
  except Exception as e:
162
+ executive_summary_status.error(f"Executive Summary Analysis failed: {e}")
163
  return None
164
+
165
+ # Create threads for first batch of concurrent execution
166
+ first_batch_threads = [
167
+ threading.Thread(target=run_off_page_analysis),
168
+ threading.Thread(target=run_on_page_analysis),
169
+ threading.Thread(target=run_website_and_tools_analysis),
170
+ threading.Thread(target=run_seo_analysis),
171
+ threading.Thread(target=run_social_media_analysis),
172
+ threading.Thread(target=run_lld_pm_ln),
173
+ threading.Thread(target=run_pull_through_offers),
174
+ threading.Thread(target=run_content),
175
+ threading.Thread(target=run_sem_ppc_analysis),
176
+ threading.Thread(target=run_marketplace_analysis),
177
+ threading.Thread(target=run_target_market_analysis)
178
+ ]
179
+
180
+ # Attach Streamlit context to first batch threads
181
+ for thread in first_batch_threads:
182
+ add_script_run_ctx(thread)
183
 
184
+ # Start first batch threads
185
+ for thread in first_batch_threads:
186
+ thread.start()
187
+
188
+ # Wait for first batch threads to complete
189
+ for thread in first_batch_threads:
190
+ thread.join()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
  st.markdown("---")
193
+ st.info("Initial analyses completed. Now generating Summary reports...")
194
+
195
+ # Create threads for second batch of concurrent execution (Snapshot and Executive Summary)
196
+ second_batch_threads = [
197
+ threading.Thread(target=run_snapshot_analysis),
198
+ threading.Thread(target=run_executive_summary_analysis)
199
+ ]
200
+
201
+ # Attach Streamlit context to second batch threads
202
+ for thread in second_batch_threads:
203
+ add_script_run_ctx(thread)
204
+
205
+ # Start second batch threads
206
+ for thread in second_batch_threads:
207
+ thread.start()
208
+
209
+ # Wait for second batch threads to complete
210
+ for thread in second_batch_threads:
211
+ thread.join()
212
+
213
  st.success("πŸŽ‰ All analyses completed!") # Final success message
214
+
215
  # --- Display Button After Completion ---
216
  if st.button("View Results", icon="πŸ“ƒ"):
217
  st.switch_page("pages/output.py")
218
 
219
  # Execute the analysis
220
  if st.button("Back"):
221
+ st.switch_page("pages/home.py")
222
+ run_analysis()