Ronio Jerico Roque commited on
Commit
211a2f2
·
1 Parent(s): 0ec76d2

Add social media upload handling and refactor analysis execution in analyzing_page

Browse files
classes/Linkedin.py CHANGED
@@ -38,6 +38,8 @@ class Linkedin:
38
  except Exception:
39
  pass
40
  '''
 
 
41
 
42
  def request_model(self, payload_txt):
43
  response = requests.post(self.model_url, json=payload_txt)
@@ -187,6 +189,7 @@ class Linkedin:
187
  }
188
  '''
189
  collect_telemetry(debug_info)
 
190
 
191
  #with st.expander("Debug information", icon="⚙"):
192
  # st.write(debug_info)
 
38
  except Exception:
39
  pass
40
  '''
41
+ if 'linkedin_upload' not in st.session_state:
42
+ st.session_state['linkedin_upload'] = ''
43
 
44
  def request_model(self, payload_txt):
45
  response = requests.post(self.model_url, json=payload_txt)
 
189
  }
190
  '''
191
  collect_telemetry(debug_info)
192
+ st.session_state['linkedin_upload'] = 'uploaded'
193
 
194
  #with st.expander("Debug information", icon="⚙"):
195
  # st.write(debug_info)
classes/Social_Media_FB.py CHANGED
@@ -39,6 +39,8 @@ class Facebook:
39
  except Exception:
40
  pass
41
  '''
 
 
42
 
43
  def request_model(self, payload_txt):
44
  response = requests.post(self.model_url, json=payload_txt)
@@ -142,7 +144,7 @@ class Facebook:
142
  def process(self):
143
  start_time = time.time()
144
  session = st.session_state.analyze
145
- if ((self.fb_organic_post and self.fb_organic_post.name) or (self.fb_ads_campaign and self.fb_ads_campaign.name) or self.facebooks or self.facebook_rr) and session == 'clicked':
146
  try:
147
  combined_text = ""
148
  with st.spinner('Social Media Analyst...', show_time=True):
@@ -201,7 +203,10 @@ class Facebook:
201
  'result': result,
202
  }
203
  '''
 
 
204
  collect_telemetry(debug_info)
 
205
 
206
  #with st.expander("Debug information", icon="⚙"):
207
  # st.write(debug_info)
 
39
  except Exception:
40
  pass
41
  '''
42
+ if 'fb_upload' not in st.session_state:
43
+ st.session_state['fb_upload'] = ''
44
 
45
  def request_model(self, payload_txt):
46
  response = requests.post(self.model_url, json=payload_txt)
 
144
  def process(self):
145
  start_time = time.time()
146
  session = st.session_state.analyze
147
+ if ((self.fb_organic_post and self.fb_organic_post.name) or (self.fb_ads_campaign and self.fb_ads_campaign.name) or (self.facebooks) or (self.facebook_rr)) and session == 'clicked':
148
  try:
149
  combined_text = ""
150
  with st.spinner('Social Media Analyst...', show_time=True):
 
203
  'result': result,
204
  }
205
  '''
206
+
207
+ st.session_state['fb_upload'] = 'uploaded'
208
  collect_telemetry(debug_info)
209
+ combined_text = ""
210
 
211
  #with st.expander("Debug information", icon="⚙"):
212
  # st.write(debug_info)
classes/Social_Media_IG.py CHANGED
@@ -38,6 +38,8 @@ class Instagram:
38
  except Exception:
39
  pass
40
  '''
 
 
41
 
42
  def request_model(self, payload_txt):
43
  response = requests.post(self.model_url, json=payload_txt)
@@ -135,6 +137,8 @@ class Instagram:
135
  }
136
  '''
137
  collect_telemetry(debug_info)
 
 
138
 
139
 
140
  except AttributeError:
 
38
  except Exception:
39
  pass
40
  '''
41
+ if 'ig_upload' not in st.session_state:
42
+ st.session_state['ig_upload'] = ''
43
 
44
  def request_model(self, payload_txt):
45
  response = requests.post(self.model_url, json=payload_txt)
 
137
  }
138
  '''
139
  collect_telemetry(debug_info)
140
+ st.session_state['ig_upload'] = 'uploaded'
141
+
142
 
143
 
144
  except AttributeError:
classes/Tiktok.py CHANGED
@@ -38,6 +38,8 @@ class Tiktok:
38
  except Exception:
39
  pass
40
  '''
 
 
41
 
42
  def request_model(self, payload_txt):
43
  response = requests.post(self.model_url, json=payload_txt)
@@ -101,6 +103,7 @@ class Tiktok:
101
  except Exception:
102
  pass
103
  return file_name
 
104
  def process(self):
105
  session = st.session_state.analyze
106
  if (self.tiktok_f or self.tiktok_er or self.tiktok_pf) and session == 'clicked':
@@ -134,6 +137,7 @@ class Tiktok:
134
  }
135
  '''
136
  collect_telemetry(debug_info)
 
137
 
138
  #with st.expander("Debug information", icon="⚙"):
139
  # st.write(debug_info)
 
38
  except Exception:
39
  pass
40
  '''
41
+ if 'tiktok_upload' not in st.session_state:
42
+ st.session_state['tiktok_upload'] = ''
43
 
44
  def request_model(self, payload_txt):
45
  response = requests.post(self.model_url, json=payload_txt)
 
103
  except Exception:
104
  pass
105
  return file_name
106
+
107
  def process(self):
108
  session = st.session_state.analyze
109
  if (self.tiktok_f or self.tiktok_er or self.tiktok_pf) and session == 'clicked':
 
137
  }
138
  '''
139
  collect_telemetry(debug_info)
140
+ st.session_state['tiktok_upload'] = 'uploaded'
141
 
142
  #with st.expander("Debug information", icon="⚙"):
143
  # st.write(debug_info)
classes/Twitter.py CHANGED
@@ -38,6 +38,8 @@ class Twitter:
38
  except Exception:
39
  pass
40
  '''
 
 
41
 
42
  def request_model(self, payload_txt):
43
  response = requests.post(self.model_url, json=payload_txt)
@@ -149,7 +151,7 @@ class Twitter:
149
  }
150
  '''
151
  collect_telemetry(debug_info)
152
-
153
  st.session_state['analyzing'] = False
154
  except AttributeError:
155
  st.info("Please upload CSV or PDF files first.")
 
38
  except Exception:
39
  pass
40
  '''
41
+ if 'twitter_upload' not in st.session_state:
42
+ st.session_state['twitter_upload'] = ''
43
 
44
  def request_model(self, payload_txt):
45
  response = requests.post(self.model_url, json=payload_txt)
 
151
  }
152
  '''
153
  collect_telemetry(debug_info)
154
+ st.session_state['twitter_upload'] = 'uploaded'
155
  st.session_state['analyzing'] = False
156
  except AttributeError:
157
  st.info("Please upload CSV or PDF files first.")
classes/Youtube.py CHANGED
@@ -38,6 +38,8 @@ class YouTube:
38
  except Exception:
39
  pass
40
  '''
 
 
41
 
42
  def request_model(self, payload_txt):
43
  response = requests.post(self.model_url, json=payload_txt)
@@ -104,7 +106,7 @@ class YouTube:
104
 
105
  def process(self):
106
  session = st.session_state.analyze
107
- if (self.youtube or self.youtube_er or self.youtube_pf) and session == 'clicked':
108
  try:
109
  combined_text = ""
110
  with st.spinner('Youtube...', show_time=True):
@@ -138,6 +140,7 @@ class YouTube:
138
  }
139
  '''
140
  collect_telemetry(debug_info)
 
141
 
142
  #with st.expander("Debug information", icon="⚙"):
143
  # st.write(debug_info)
 
38
  except Exception:
39
  pass
40
  '''
41
+ if 'youtube_upload' not in st.session_state:
42
+ st.session_state['youtube_upload'] = ''
43
 
44
  def request_model(self, payload_txt):
45
  response = requests.post(self.model_url, json=payload_txt)
 
106
 
107
  def process(self):
108
  session = st.session_state.analyze
109
+ if ((self.youtube or self.youtube_er or self.youtube_pf) and session) == 'clicked':
110
  try:
111
  combined_text = ""
112
  with st.spinner('Youtube...', show_time=True):
 
140
  }
141
  '''
142
  collect_telemetry(debug_info)
143
+ st.session_state['youtube_upload'] = 'uploaded'
144
 
145
  #with st.expander("Debug information", icon="⚙"):
146
  # st.write(debug_info)
classes/response_social_media.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from dotenv import load_dotenv
4
+ import os
5
+ import pandas as pd
6
+ import time
7
+ import chardet
8
+ from helper.telemetry import collect_telemetry
9
+ from helper.upload_File import uploadFile
10
+ from helper.button_behaviour import hide_button
11
+ from helper.initialize_analyze_session import initialize_analyze_session
12
+ from pymongo import MongoClient
13
+ import json
14
+ from helper.data_field import data_field
15
+ from helper.upload_response import upload_response
16
+
17
+ class SocialMedia:
18
+ def __init__(self, model_url):
19
+ self.file_dict = {}
20
+ self.model_url = model_url
21
+ #self.analyst_name = analyst_name
22
+ #self.data_src = data_src
23
+ #self.analyst_description = analyst_description
24
+ self.initialize()
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
+ '''
36
+ url = os.getenv('Link')
37
+ st.write('Evaluation Form: [Link](%s)' % url)
38
+
39
+ # RETURN BUTTON
40
+ try:
41
+ if st.button("Return", type='primary'):
42
+ st.switch_page("./pages/home.py")
43
+ except Exception:
44
+ pass
45
+ '''
46
+ if 'fb_upload' not in st.session_state:
47
+ st.session_state['fb_upload'] = ''
48
+
49
+ def request_model(self, payload_txt, headers):
50
+ response = requests.post(self.model_url, json=payload_txt, headers=headers)
51
+ response.raise_for_status()
52
+ output = response.json()
53
+ #st.write(output)
54
+ text = output["outputs"][0]["outputs"][0]["results"]["text"]["data"]["text"]
55
+ text = json.loads(text)
56
+ #st.write(text)
57
+ return text
58
+
59
+ def terminate_session(self, session):
60
+ try:
61
+ del st.session_state[session]
62
+ except KeyError:
63
+ pass
64
+
65
+ def fetch_data(self, data_field):
66
+ mongodb_uri = os.getenv("MONGODB_URI")
67
+ myclient = MongoClient(mongodb_uri)
68
+ mydb = myclient.get_database()
69
+ mycol = mydb["df_data"]
70
+
71
+ # Sort by timestamp field in descending order
72
+ x = mycol.find_one(
73
+ {"data_field": data_field},
74
+ sort=[("timestamp", -1)] # -1 means descending order
75
+ )
76
+
77
+ x = x["result"]
78
+ return x
79
+
80
+ def process(self):
81
+ with st.spinner('Social Media Analyst...', show_time=True):
82
+ st.write('')
83
+
84
+ headers = {"Content-Type": "application/json", "x-api-key": f"{os.getenv('x-api-key')}"}
85
+ payload = ""
86
+
87
+ try:
88
+ session_fb = st.session_state['fb_upload']
89
+ if session_fb == 'uploaded':
90
+ payload += self.fetch_data("Facebook")
91
+
92
+ except Exception as e:
93
+ pass
94
+ try:
95
+ session_ig = st.session_state['ig_upload']
96
+ if session_ig == 'uploaded':
97
+ print("running ig")
98
+ payload += self.fetch_data("Instagram")
99
+
100
+ except Exception as e:
101
+ pass
102
+ try:
103
+ session_twitter = st.session_state['twitter_upload']
104
+ if session_twitter == 'uploaded':
105
+ payload += self.fetch_data("Twitter")
106
+
107
+ except Exception as e:
108
+ pass
109
+ try:
110
+ session_yt = st.session_state['youtube_upload']
111
+ if session_yt == 'uploaded':
112
+ payload += self.fetch_data("YouTube")
113
+
114
+ except Exception as e:
115
+ pass
116
+ try:
117
+ session_linkedin = st.session_state['linkedin_upload']
118
+ if session_linkedin == 'uploaded':
119
+ payload += self.fetch_data("Linkedin")
120
+
121
+ except Exception as e:
122
+ pass
123
+ try:
124
+ session_tiktok = st.session_state['tiktok_upload']
125
+ if session_tiktok == 'uploaded':
126
+ payload += self.fetch_data("Tiktok")
127
+
128
+ except Exception as e:
129
+ pass
130
+ try:
131
+ if session_fb or session_ig or session_twitter or session_yt or session_linkedin or session_tiktok == 'uploaded':
132
+ payload_txt = {"input_value": payload, "output_type": "text", "input_type": "chat"}
133
+ payload_txt_model = self.request_model(payload_txt, headers)
134
+ debug_info = {'data_field' : 'Social Media Analyst', 'result': payload_txt_model}
135
+ upload_response(debug_info)
136
+
137
+ st.session_state['fb_upload'] = ''
138
+ st.session_state['ig_upload'] = ''
139
+ st.session_state['twitter_upload'] = ''
140
+ st.session_state['youtube_upload'] = ''
141
+ st.session_state['linkedin_upload'] = ''
142
+ st.session_state['tiktok_upload'] = ''
143
+
144
+ except Exception as e:
145
+ pass
146
+
147
+
148
+ def row1(self):
149
+ self.process()
150
+
151
+
152
+ if __name__ == "__main__":
153
+ st.set_page_config(layout="wide")
154
+
155
+ upload = uploadFile()
helper/telemetry.py CHANGED
@@ -19,7 +19,7 @@ def collect_telemetry(data):
19
  return
20
 
21
  try:
22
- '''
23
  # Get the current UTC time
24
  utc_now = datetime.now(timezone.utc)
25
 
@@ -29,7 +29,7 @@ def collect_telemetry(data):
29
  # Convert the UTC time to GMT+8
30
  timestamp = utc_now.astimezone(gmt_plus_8).isoformat()
31
  data['timestamp'] = timestamp
32
- '''
33
  client = MongoClient(mongodb_uri)
34
  db = client.get_database() # Use the default database specified in the URI
35
  collection = db["df_data"] # Replace "telemetry" with your desired collection name
 
19
  return
20
 
21
  try:
22
+
23
  # Get the current UTC time
24
  utc_now = datetime.now(timezone.utc)
25
 
 
29
  # Convert the UTC time to GMT+8
30
  timestamp = utc_now.astimezone(gmt_plus_8).isoformat()
31
  data['timestamp'] = timestamp
32
+
33
  client = MongoClient(mongodb_uri)
34
  db = client.get_database() # Use the default database specified in the URI
35
  collection = db["df_data"] # Replace "telemetry" with your desired collection name
pages/analyzing_page.py CHANGED
@@ -6,18 +6,16 @@ from classes.response_off import SeoOffPageAnalyst
6
  from classes.response_on_page import SeoOn
7
  from classes.response_website_and_tools import WebsiteAndTools
8
  from classes.response_seo import Seo
 
9
 
10
  def run_analysis():
11
- # Retrieve uploaded files from session state
12
- off_page_file = st.session_state.get('off_page_file_uploaded')
13
- gt_file = st.session_state.get('GT_file_uploaded')
14
- website_and_tools = st.session_state.get('website_and_tools')
15
- seo = st.session_state.get('seo')
16
  # Placeholders for status updates
17
  off_page_status = st.empty()
18
  on_page_status = st.empty()
19
  website_and_tools_status = st.empty()
20
  seo_status = st.empty()
 
21
  # Function to run SEO Off Page Analysis
22
  def run_off_page_analysis():
23
  try:
@@ -59,29 +57,43 @@ def run_analysis():
59
  seo_status.error(f"SEO Analysis failed: {e}")
60
  return None
61
 
 
 
 
 
 
 
 
 
 
 
62
  # Create threads for concurrent execution
63
  off_page_thread = threading.Thread(target=run_off_page_analysis)
64
  on_page_thread = threading.Thread(target=run_on_page_analysis)
65
  website_and_tools_thread = threading.Thread(target=run_website_and_tools_analysis)
66
  seo_thread = threading.Thread(target=run_seo_analysis)
 
67
 
68
  # Attach Streamlit context to threads
69
  add_script_run_ctx(off_page_thread)
70
  add_script_run_ctx(on_page_thread)
71
  add_script_run_ctx(website_and_tools_thread)
72
  add_script_run_ctx(seo_thread)
 
73
 
74
  # Start threads
75
  off_page_thread.start()
76
  on_page_thread.start()
77
  website_and_tools_thread.start()
78
  seo_thread.start()
 
79
 
80
  # Wait for threads to complete
81
  off_page_thread.join()
82
  on_page_thread.join()
83
  website_and_tools_thread.join()
84
  seo_thread.join()
 
85
 
86
  # Execute the analysis
87
  run_analysis()
 
6
  from classes.response_on_page import SeoOn
7
  from classes.response_website_and_tools import WebsiteAndTools
8
  from classes.response_seo import Seo
9
+ from classes.response_social_media import SocialMedia
10
 
11
  def run_analysis():
12
+
 
 
 
 
13
  # Placeholders for status updates
14
  off_page_status = st.empty()
15
  on_page_status = st.empty()
16
  website_and_tools_status = st.empty()
17
  seo_status = st.empty()
18
+ social_media_status = st.empty()
19
  # Function to run SEO Off Page Analysis
20
  def run_off_page_analysis():
21
  try:
 
57
  seo_status.error(f"SEO Analysis failed: {e}")
58
  return None
59
 
60
+ def run_social_media_analysis():
61
+ try:
62
+ social_media_status.info("Starting Social Media Analysis...")
63
+ result = SocialMedia(os.getenv('MODEL_Social_Media_Analyst'))
64
+ social_media_status.success("Social Media Analysis completed successfully.")
65
+ return result
66
+ except Exception as e:
67
+ social_media_status.error(f"Social Media Analysis failed: {e}")
68
+ return None
69
+
70
  # Create threads for concurrent execution
71
  off_page_thread = threading.Thread(target=run_off_page_analysis)
72
  on_page_thread = threading.Thread(target=run_on_page_analysis)
73
  website_and_tools_thread = threading.Thread(target=run_website_and_tools_analysis)
74
  seo_thread = threading.Thread(target=run_seo_analysis)
75
+ social_media_thread = threading.Thread(target=run_social_media_analysis)
76
 
77
  # Attach Streamlit context to threads
78
  add_script_run_ctx(off_page_thread)
79
  add_script_run_ctx(on_page_thread)
80
  add_script_run_ctx(website_and_tools_thread)
81
  add_script_run_ctx(seo_thread)
82
+ add_script_run_ctx(social_media_thread)
83
 
84
  # Start threads
85
  off_page_thread.start()
86
  on_page_thread.start()
87
  website_and_tools_thread.start()
88
  seo_thread.start()
89
+ social_media_thread.start()
90
 
91
  # Wait for threads to complete
92
  off_page_thread.join()
93
  on_page_thread.join()
94
  website_and_tools_thread.join()
95
  seo_thread.join()
96
+ social_media_thread.join()
97
 
98
  # Execute the analysis
99
  run_analysis()