Spaces:
Build error
Build error
Ronio Jerico Roque commited on
Commit ·
4394238
1
Parent(s): 3a2bd5f
Add Target Market analysis functionality and integrate into concurrent execution
Browse files- classes/client_summary.py +3 -0
- classes/response_target_market.py +91 -0
- pages/analyzing_page.py +16 -0
classes/client_summary.py
CHANGED
|
@@ -20,6 +20,8 @@ class CientSummary:
|
|
| 20 |
st.session_state['client_name'] = ''
|
| 21 |
if 'client_webiste' not in st.session_state:
|
| 22 |
st.session_state['client_webiste'] = ''
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def process (self):
|
| 25 |
with st.spinner('Seo Analyst...', show_time=True):
|
|
@@ -38,6 +40,7 @@ class CientSummary:
|
|
| 38 |
|
| 39 |
if self.client_summary:
|
| 40 |
st.session_state['client_summary'] = 'uploaded'
|
|
|
|
| 41 |
collect_telemetry(debug_client_summary)
|
| 42 |
if self.name:
|
| 43 |
st.session_state['client_name'] = 'uploaded'
|
|
|
|
| 20 |
st.session_state['client_name'] = ''
|
| 21 |
if 'client_webiste' not in st.session_state:
|
| 22 |
st.session_state['client_webiste'] = ''
|
| 23 |
+
if 'target_market' not in st.session_state:
|
| 24 |
+
st.session_state['target_market'] = ''
|
| 25 |
|
| 26 |
def process (self):
|
| 27 |
with st.spinner('Seo Analyst...', show_time=True):
|
|
|
|
| 40 |
|
| 41 |
if self.client_summary:
|
| 42 |
st.session_state['client_summary'] = 'uploaded'
|
| 43 |
+
st.session_state['target_market'] = 'uploaded'
|
| 44 |
collect_telemetry(debug_client_summary)
|
| 45 |
if self.name:
|
| 46 |
st.session_state['client_name'] = 'uploaded'
|
classes/response_target_market.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 TargetMarket:
|
| 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('Target Market 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' : 'Target Market 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_target_market = st.session_state['target_market']
|
| 77 |
+
if session_target_market == 'uploaded':
|
| 78 |
+
count += 1
|
| 79 |
+
self.payload += self.fetch_data("Client Summary")
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
pass
|
| 83 |
+
|
| 84 |
+
if count >= 1:
|
| 85 |
+
self.process()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
st.set_page_config(layout="wide")
|
| 90 |
+
|
| 91 |
+
upload = uploadFile()
|
pages/analyzing_page.py
CHANGED
|
@@ -12,6 +12,7 @@ from classes.response_pull_through_offers import PullThroughOffers
|
|
| 12 |
from classes.response_content import Content
|
| 13 |
from classes.response_sem_ppc import Sem_PPC
|
| 14 |
from classes.response_marketplace import Marketplace
|
|
|
|
| 15 |
|
| 16 |
def run_analysis():
|
| 17 |
# Placeholders for status updates
|
|
@@ -25,6 +26,7 @@ def run_analysis():
|
|
| 25 |
content_status = st.empty()
|
| 26 |
sem_ppc = st.empty()
|
| 27 |
marketplace = st.empty()
|
|
|
|
| 28 |
|
| 29 |
def run_off_page_analysis():
|
| 30 |
try:
|
|
@@ -126,6 +128,16 @@ def run_analysis():
|
|
| 126 |
marketplace.error(f"Marketplace Analysis failed: {e}")
|
| 127 |
return None
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# Create threads for concurrent execution
|
| 130 |
off_page_thread = threading.Thread(target=run_off_page_analysis)
|
| 131 |
on_page_thread = threading.Thread(target=run_on_page_analysis)
|
|
@@ -137,6 +149,7 @@ def run_analysis():
|
|
| 137 |
content_thread = threading.Thread(target=run_content)
|
| 138 |
content_sem_ppc_thread = threading.Thread(target=run_sem_ppc_analysis)
|
| 139 |
marketplace_thread = threading.Thread(target=run_marketplace_analysis)
|
|
|
|
| 140 |
|
| 141 |
# Attach Streamlit context to threads
|
| 142 |
add_script_run_ctx(off_page_thread)
|
|
@@ -149,6 +162,7 @@ def run_analysis():
|
|
| 149 |
add_script_run_ctx(content_thread)
|
| 150 |
add_script_run_ctx(content_sem_ppc_thread)
|
| 151 |
add_script_run_ctx(marketplace_thread)
|
|
|
|
| 152 |
|
| 153 |
# Start threads
|
| 154 |
off_page_thread.start()
|
|
@@ -161,6 +175,7 @@ def run_analysis():
|
|
| 161 |
content_thread.start()
|
| 162 |
content_sem_ppc_thread.start()
|
| 163 |
marketplace_thread.start()
|
|
|
|
| 164 |
|
| 165 |
# Wait for threads to complete
|
| 166 |
off_page_thread.join()
|
|
@@ -173,6 +188,7 @@ def run_analysis():
|
|
| 173 |
content_thread.join()
|
| 174 |
content_sem_ppc_thread.join()
|
| 175 |
marketplace_thread.join()
|
|
|
|
| 176 |
|
| 177 |
st.success("🎉 All analyses completed!") # Final success message
|
| 178 |
# --- Display Button After Completion ---
|
|
|
|
| 12 |
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 |
|
| 17 |
def run_analysis():
|
| 18 |
# Placeholders for status updates
|
|
|
|
| 26 |
content_status = st.empty()
|
| 27 |
sem_ppc = st.empty()
|
| 28 |
marketplace = st.empty()
|
| 29 |
+
target_market = st.empty()
|
| 30 |
|
| 31 |
def run_off_page_analysis():
|
| 32 |
try:
|
|
|
|
| 128 |
marketplace.error(f"Marketplace Analysis failed: {e}")
|
| 129 |
return None
|
| 130 |
|
| 131 |
+
def run_target_market_analysis():
|
| 132 |
+
try:
|
| 133 |
+
target_market.info("Starting Target Market Analysis...")
|
| 134 |
+
result = TargetMarket(os.getenv('Model_Target_Market_Analyst'))
|
| 135 |
+
target_market.success("Target Market Analysis completed successfully.")
|
| 136 |
+
return result
|
| 137 |
+
except Exception as e:
|
| 138 |
+
target_market.error(f"Target Market Analysis failed: {e}")
|
| 139 |
+
return None
|
| 140 |
+
|
| 141 |
# Create threads for concurrent execution
|
| 142 |
off_page_thread = threading.Thread(target=run_off_page_analysis)
|
| 143 |
on_page_thread = threading.Thread(target=run_on_page_analysis)
|
|
|
|
| 149 |
content_thread = threading.Thread(target=run_content)
|
| 150 |
content_sem_ppc_thread = threading.Thread(target=run_sem_ppc_analysis)
|
| 151 |
marketplace_thread = threading.Thread(target=run_marketplace_analysis)
|
| 152 |
+
target_market_thread = threading.Thread(target=run_target_market_analysis)
|
| 153 |
|
| 154 |
# Attach Streamlit context to threads
|
| 155 |
add_script_run_ctx(off_page_thread)
|
|
|
|
| 162 |
add_script_run_ctx(content_thread)
|
| 163 |
add_script_run_ctx(content_sem_ppc_thread)
|
| 164 |
add_script_run_ctx(marketplace_thread)
|
| 165 |
+
add_script_run_ctx(target_market_thread)
|
| 166 |
|
| 167 |
# Start threads
|
| 168 |
off_page_thread.start()
|
|
|
|
| 175 |
content_thread.start()
|
| 176 |
content_sem_ppc_thread.start()
|
| 177 |
marketplace_thread.start()
|
| 178 |
+
target_market_thread.start()
|
| 179 |
|
| 180 |
# Wait for threads to complete
|
| 181 |
off_page_thread.join()
|
|
|
|
| 188 |
content_thread.join()
|
| 189 |
content_sem_ppc_thread.join()
|
| 190 |
marketplace_thread.join()
|
| 191 |
+
target_market_thread.join()
|
| 192 |
|
| 193 |
st.success("🎉 All analyses completed!") # Final success message
|
| 194 |
# --- Display Button After Completion ---
|