Ronio Jerico Roque commited on
Commit
7ad08a5
·
1 Parent(s): 434b4b7

Add DF Overview analysis functionality and integrate into the analyzing page with concurrent execution

Browse files
classes/response_df_overview.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import StringIO
2
+ from urllib.parse import urlparse
3
+ import streamlit as st
4
+ import requests
5
+ from dotenv import load_dotenv
6
+ import os
7
+ from helper.upload_response import upload_response
8
+ from helper.upload_File import uploadFile
9
+ import json
10
+ from pymongo import MongoClient
11
+
12
+ class dfOverview:
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('DF Overview Analyst...', 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' : 'DF Overview Analyst', 'result': payload_txt_model}
63
+ upload_response(debug_info)
64
+
65
+ st.session_state['target_market'] = ''
66
+ count = 0
67
+ except Exception as e:
68
+ pass
69
+ st.session_state['analyzing'] = False
70
+
71
+ def row1(self):
72
+ st.session_state['analyzing'] = False
73
+ self.payload = ""
74
+ count = 0
75
+ try:
76
+ session_client_summary = st.session_state['client_summary']
77
+ if session_client_summary == 'uploaded':
78
+ count += 1
79
+ self.payload += self.fetch_data("Client Summary")
80
+ self.payload += self.fetch_data("Client Name")
81
+
82
+ except Exception as e:
83
+ pass
84
+
85
+ if count >= 1:
86
+ self.process()
87
+
88
+
89
+ if __name__ == "__main__":
90
+ st.set_page_config(layout="wide")
91
+
92
+ upload = uploadFile()
pages/analyzing_page.py CHANGED
@@ -13,6 +13,7 @@ from classes.response_content import Content
13
  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
  from classes.response_snapshot import Snapshot
18
 
@@ -29,6 +30,7 @@ def run_analysis():
29
  sem_ppc = st.empty()
30
  marketplace = st.empty()
31
  target_market = st.empty()
 
32
  executive_summary_status = st.empty()
33
  snapshot_status = st.empty()
34
 
@@ -141,6 +143,16 @@ def run_analysis():
141
  except Exception as e:
142
  target_market.error(f"Target Market Analysis failed: {e}")
143
  return None
 
 
 
 
 
 
 
 
 
 
144
 
145
  # Create threads for concurrent execution
146
  off_page_thread = threading.Thread(target=run_off_page_analysis)
@@ -154,6 +166,7 @@ def run_analysis():
154
  content_sem_ppc_thread = threading.Thread(target=run_sem_ppc_analysis)
155
  marketplace_thread = threading.Thread(target=run_marketplace_analysis)
156
  target_market_thread = threading.Thread(target=run_target_market_analysis)
 
157
 
158
  # Attach Streamlit context to threads
159
  add_script_run_ctx(off_page_thread)
@@ -167,6 +180,7 @@ def run_analysis():
167
  add_script_run_ctx(content_sem_ppc_thread)
168
  add_script_run_ctx(marketplace_thread)
169
  add_script_run_ctx(target_market_thread)
 
170
 
171
  # Start threads
172
  off_page_thread.start()
@@ -180,6 +194,7 @@ def run_analysis():
180
  content_sem_ppc_thread.start()
181
  marketplace_thread.start()
182
  target_market_thread.start()
 
183
 
184
  # Wait for threads to complete
185
  off_page_thread.join()
@@ -193,6 +208,7 @@ def run_analysis():
193
  content_sem_ppc_thread.join()
194
  marketplace_thread.join()
195
  target_market_thread.join()
 
196
 
197
  st.markdown("---")
198
  snapshot_status.info("Starting Snapshot by Channel Analysis...")
 
13
  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_df_overview import dfOverview
17
  from classes.response_executive_summary import ExecutiveSummary
18
  from classes.response_snapshot import Snapshot
19
 
 
30
  sem_ppc = st.empty()
31
  marketplace = st.empty()
32
  target_market = st.empty()
33
+ df_overview_status = st.empty()
34
  executive_summary_status = st.empty()
35
  snapshot_status = st.empty()
36
 
 
143
  except Exception as e:
144
  target_market.error(f"Target Market Analysis failed: {e}")
145
  return None
146
+
147
+ def df_overview_analysis():
148
+ try:
149
+ df_overview_status.info("DF Overview Analysis...")
150
+ result = dfOverview(os.getenv('Model_DF_Overview_Analyst'))
151
+ df_overview_status.success("DF Overview Analysis completed successfully.")
152
+ return result
153
+ except Exception as e:
154
+ df_overview_status.error(f"DF Overview Analysis failed: {e}")
155
+ return None
156
 
157
  # Create threads for concurrent execution
158
  off_page_thread = threading.Thread(target=run_off_page_analysis)
 
166
  content_sem_ppc_thread = threading.Thread(target=run_sem_ppc_analysis)
167
  marketplace_thread = threading.Thread(target=run_marketplace_analysis)
168
  target_market_thread = threading.Thread(target=run_target_market_analysis)
169
+ df_overview_thread = threading.Thread(target=df_overview_analysis)
170
 
171
  # Attach Streamlit context to threads
172
  add_script_run_ctx(off_page_thread)
 
180
  add_script_run_ctx(content_sem_ppc_thread)
181
  add_script_run_ctx(marketplace_thread)
182
  add_script_run_ctx(target_market_thread)
183
+ add_script_run_ctx(df_overview_thread)
184
 
185
  # Start threads
186
  off_page_thread.start()
 
194
  content_sem_ppc_thread.start()
195
  marketplace_thread.start()
196
  target_market_thread.start()
197
+ df_overview_thread.start()
198
 
199
  # Wait for threads to complete
200
  off_page_thread.join()
 
208
  content_sem_ppc_thread.join()
209
  marketplace_thread.join()
210
  target_market_thread.join()
211
+ df_overview_thread.join()
212
 
213
  st.markdown("---")
214
  snapshot_status.info("Starting Snapshot by Channel Analysis...")