File size: 16,196 Bytes
1be66cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from openai import OpenAI
import streamlit as st
from langchain_openai import ChatOpenAI
from langchain_openai.embeddings import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
import markdown
from operator import itemgetter
from langchain.schema.runnable import RunnablePassthrough
from langchain_core.prompts import ChatPromptTemplate
from langchain.schema import Document
from dotenv import load_dotenv
from langchain_community.vectorstores import Qdrant
from PIL import Image, ImageEnhance
from tools import sentiment_analysis_util
#from langchain_qdrant import Qdrant
import os
import pandas as pd
import numpy as np
import datetime


# App config
load_dotenv()
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
base_llm = ChatOpenAI(model="gpt-4o")
embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")

# Page config

st.set_page_config(
    page_title="Narrativ πŸ“°",
    layout="wide",
    initial_sidebar_state="expanded",
    page_icon="πŸ”",
)


# Load environment variables
load_dotenv()
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
base_llm = ChatOpenAI(model="gpt-4o")
embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
uploaded_file = None
topic='employment'
date='2025-02-15'

# Custom CSS for centered content
st.markdown("""
    <style>
    .main-container {
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
    }
    
    .stSelectbox {
        max-width: 400px;
        margin: 0 auto;
    }
    
    /* Center all text elements */
    .centered-text {
        text-align: center;
    }
    </style>
""", unsafe_allow_html=True)

# Header
col1, col2, col3, col4,col5 = st.columns([1, 1, 2, 1, 1])
from PIL import Image, ImageEnhance

with col3:
    st.markdown("<h1 class='centered-text'>Search Narrativ</h1>", unsafe_allow_html=True)


# Suggestions
topic_suggestions = [
    "employment",
    "remote work",
    "unemployment"
]

data=pd.read_csv('./data/sentiment_index_hr_index_final2.csv',
                index_col='index',
                parse_dates=True
            )

# Convert the index to datetime, if not already done
data.index = pd.to_datetime(data.index)

# Generate a sorted list of unique dates
sorted_dates = sorted(pd.unique(data.index))

# Format the sorted dates as string 'YYYY-MM-DD'
date_suggestions = [pd.Timestamp(date).strftime('%Y-%m-%d') for date in sorted_dates]
date_suggestions=np.append('',date_suggestions)
# Create centered container for search
# Define the allowed date range
start_date = datetime.date(2025, 1, 15)
end_date = datetime.date(2025, 1, 21)

sidebar=st.sidebar

with sidebar:
    st.subheader("πŸ“° News")
    topic = st.selectbox(
        "Topic:",
        options=[""] + topic_suggestions,
        index=0,
        key="topic_select",
        placeholder="Select or type a topic..."
    ) 

    date = st.selectbox(
        "Date (optional):",
        options=date_suggestions,
        index=0,
        key="date_select",
        placeholder="Select or type a date..."
    )
    date=str(date)
    prompt = st.button("Summarize News", key="chat_button", use_container_width=True)

    st.subheader("πŸ“Š Survey")
    uploaded_file = st.file_uploader("πŸ“‚ Upload Pulse Survey (.txt)", type="txt")
    prompt_survey = st.button("Survey results", key="chat_button1", use_container_width=True)

# Handle search submission

if 'messages' not in st.session_state:
    st.session_state.messages = []

st.session_state.messages.append({"role": "assistant", "content": f'{date} {prompt}'})


if prompt:
    image = Image.open('./data/Sentiment_index_hr.png')
    enhancer = ImageEnhance.Brightness(image)
    #darker_image = enhancer.enhance(0.5)  # Adjust the brightness factor as needed
    st.image(image, output_format="PNG", clamp=True)

    if date:
        try:
            data=pd.read_csv('./data/sentiment_index_hr_index_final2.csv',
                index_col='index',
                parse_dates=True,
                infer_datetime_format=True
            )

            data = data.loc[data.index == date]
            filtered_data = data[data.apply(lambda row: row.astype(str).str.contains(topic, na=False).any(), axis=1)]
            data_all = filtered_data.values.flatten()
            docs = data_all 
            if len(docs)<1:
                st.warning("No articles found that contain the prompt string.")
        
            # Create markdown formatted text from the matching articles.
            # docs_text = "\n".join([f"- {article}" for article in data_prompt if article])
            # docs = [Document(page_content=docs_text)]
        except Exception as e:
            st.write('Please, enter a topic into the side panel.')
    
    else:
        try:
            data = pd.read_csv(
                './data/sentiment_index_hr_index_final2.csv',
                index_col='index',
                parse_dates=True,
                infer_datetime_format=True
            )
            filtered_data = data[data.apply(lambda row: row.astype(str).str.contains(topic, na=False).any(), axis=1)]
            if len(filtered_data)<1:
                filtered_data=data[data.apply(lambda row: row.astype(str), axis=1)]
            data_all = filtered_data.values.flatten()
            docs = data_all 
            if len(docs)<1:
                st.warning("No articles found that contain the prompt string.")

        except Exception as e:
            st.write('Please, enter a topic into the side panel.')

    # scrape in real time reddit news
    reddit_news_articles=sentiment_analysis_util.fetch_reddit_news('')
    docs_text = "\n".join([f"- {value}" for value in data_all if not pd.isna(value)])
    docs_text_reddit = "\n".join([f"- {value}" for value in reddit_news_articles if not pd.isna(value)])
    docs_text=docs_text+'\n'+'Reddit news:'+'\n'+docs_text_reddit
    docs = [Document(page_content=docs_text)]

    with open('./data/reddit.txt', 'w') as file:
        file.write(docs_text_reddit)

    split_documents = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
        chunk_size=1000,
        chunk_overlap=20
    ).split_documents(docs)

    vectorstore = Qdrant.from_documents(
        split_documents,
        embedding_model,
        location=":memory:",
        collection_name="langchainblogs"
    )

    retriever = vectorstore.as_retriever()

    print("Loaded Vectorstore")
    
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": topic})
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(topic)

    # Generate summarized message rationalize dominant sentiment
    RAG_PROMPT ="""You are an HR analyst specializing in employment trends, workforce dynamics, and remote work adoption. Your task is to analyze news articles provided by a client on a specific topic. You will receive the full text of relevant articles, along with key data points. Your goal is to evaluate labor market conditions and provide insights into workforce changes.
                Your Tasks:
                1. Summarize Opinions:

                    Extract the key opinions and perspectives from the provided news articles, reddit posts and linkedin posts.
                    The news articles will include: title, URL, date, text, article source, sentiment index created by the company, sentiment index using HF (Hugging Face) model, and confidence for the HF index.
                    The reddit posts will include: title, URL, date, text.
                    Highlight any significant patterns, agreements, or disagreements across sources regarding job trends, hiring, layoffs, wages, or remote work policies.
                    Include sentiment from reddit articles! Explicitly mention the reddit source in the summary.

                2. Analyze Sentiment:

                    Determine the overall sentiment (positive, negative, neutral) about labor market conditions based on the extracted opinions.
                    Provide a clear explanation of your sentiment conclusion, referencing specific points or trends from the articles.

                3. Provide Chain-of-Thought Reasoning:

                    Detail your reasoning process step by step. Explain how you interpreted the articles, derived insights, and reached your sentiment conclusion.
                    Ensure the reasoning is logical, transparent, and grounded in the content provided.

                4. Collect URL Sources:

                    From the provided context, select 5 critical and recent URL sources related to labor market trends and remote work policies.

                Output Format:

                    Summary of Opinions: [Concise summary of key opinions]
                    Sentiment Analysis:
                        Sentiment: [Positive/Negative/Neutral]
                        Reasoning: [Detailed explanation here]
                    Chain-of-Thought Reasoning: [Step-by-step explanation]
                    Sources: [URLs for 5 most critical and recent articles on this topic]

                Guidelines:

                    Maintain objectivity and precision in your analysis.
                    Focus on labor market trends, job market shifts, and remote work dynamics.
                    Use professional and analytical language suitable for client reports.
                    Respond in the language of the article (mostly English).

                CONTEXT:

                {context}
                QUERY:

                {question}

                Use the provided context to answer the user's question. Only use the provided context to answer the question. If you do not know the answer, respond with "I don't know."""
    rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
    # RAG CHAIN
    lcel_rag_chain = (
        {"context": itemgetter("question") | retriever, "question": itemgetter("question")}
        | RunnablePassthrough.assign(context=itemgetter("context"))
        | {"response": rag_prompt | base_llm, "context": itemgetter("context")}
    )

    try:
        summary = lcel_rag_chain.invoke({"question": topic})
        print(summary)
        st.chat_message("assistant").write((summary['response'].content))
    except Exception as e:
        st.error(f"Error generating summary: {e}")

    if date:
        with open('./data/sentiment_index_hr_index_final_date.md', 'w') as file:
                file.write(str(data_all))
    else:
        with open('./data/sentiment_index_hr_index_final1.md', 'w') as file:
                file.write(str(data_all))

if prompt_survey:
    import survey_summary
    st.session_state['uploaded_file'] = uploaded_file
    analysis = survey_summary.survey_agent('',uploaded_file)
    st.chat_message("assistant").write(str(analysis))

client = OpenAI(api_key=OPENAI_API_KEY)

if "openai_model" not in st.session_state:
    st.session_state["openai_model"] = "gpt-4o"

prompt1 = st.chat_input("Type your additional questions here...")

# Suggested keywords with enhanced styling
suggested_keywords = ["Latest News", "News on remote work", f"Survey sentiment", f"Employee satisfaction", f"How many employees?"]
st.markdown("**Suggested Keywords:**")
cols = st.columns(len(suggested_keywords))
for idx, keyword in enumerate(suggested_keywords):
    if cols[idx].button(keyword, key=keyword):
        prompt1 = keyword

if prompt1:
    st.session_state.messages.append({"role": "user", "content": prompt1})
    with open('./data/employee_pulse_survey.txt', 'r') as file:
        survey_txt = file.read()
    # Decide if call SQL agent, SURVEY agent or SENTIMENT agent
    database_columns=pd.read_csv('./data/hr_data.csv').columns
    response = base_llm.invoke(f"""You are the Supervisor of the company. In your team you have, general conversation analyst, data analyst, survey analyst and news article analyst. 
                                    If the question {prompt1} can be answered from the history of the conversation:{st.session_state.messages[-10:]} or you can use your knowledge and do not need to call the team members, respond 'history'.
                                    If not: decide if the question: '{prompt1}' is about data available in the database, based on the following columns: {database_columns}, it has information about all employees. If yes, respond 'data'.
                                    If not: decide if the question is asking about the survey: {survey_txt}. If yes, respond 'survey'.
                                    If not: decide if the question is asking about news articles on employment trends or remote work. If yes, respond 'news'.
                                    Your response will be either 'history' or 'data' or 'survey' or 'news'. 
                                    Don't answer anything else.
                                    Survey: {survey_txt}""")
    st.write(response.content)
    if 'data' in response.content.lower():
        # SQL AGENT
        import sql_agent

        response = sql_agent.sql_agent(f'the question is: {prompt1} and the history is: {st.session_state.messages[-10:]}')
        st.session_state.messages.append({"role": "sql_agent", "content": response})

    elif 'news' in response.content.lower():
        # SENTIMENT AGENT
        if date:
            file_path = f'./data/sentiment_index_hr_index_final_date.md'
        else:
            file_path = f'./data/sentiment_index_hr_index_final1.md'
            
        try:
            with open(file_path, "r", encoding="utf-8") as file_content:
                docs = file_content.read()
        except Exception as e:
            st.error(f"Error loading context: {e}")
            docs = ""

        # Display user message in chat message container
        response = base_llm.invoke(f"""You are a data analyst, the question is: {prompt1}, the conversation history is: {st.session_state.messages[-10:]} and the context is from {docs}""")
        st.session_state.messages.append({"role": "news_agent", "content": response})
        # st.chat_message("assistant").write(str(response))

    elif 'survey' in response.content.lower():
        # SURVEY AGENT
        with open('./data/employee_pulse_survey.txt', 'r') as file:
            survey_text = file.read()
        import survey_agent1
        response = survey_agent1.analyze_survey_document(survey_text, f'the question is: {prompt1} and the history is: {st.session_state.messages[-10:]}')
        st.session_state.messages.append({"role": "survey_agent", "content": response})
        # st.chat_message("assistant").write(str(response))
        
    # Go back to the MAIN SUPERVISOR
        # Display user message in chat message container
    print('History:',st.session_state.messages[-10:])
    response = base_llm.invoke(f"""You are a supervisor, who collects the answers from the team and give the final answer to the user.
                                      Take the last response, 'response', from your team member: SQL agent, SURVEY agent or SENTIMENT agent and give the final answer to the user.
                                      The user's question is: {prompt1},
                                      the responses from the team are: {st.session_state.messages[-10:]}""")

    st.chat_message("supervisor").write(str(response.content))
    st.session_state.messages.append({"role": "supervisor", "content": response.content})

    # with st.chat_message("user"):
    #     st.markdown(prompt1)
    # # Display assistant response in chat message container
    # with st.chat_message("assistant"):
    #     try:
    #         stream = client.chat.completions.create(
    #             model=st.session_state["openai_model"],
    #             messages=[
    #                 {"role": m["role"], "content": m["content"]}
    #                 for m in st.session_state.messages[:-10]
    #             ],
    #             stream=True,
    #         )
    #         response = st.write_stream(stream)
    #         st.session_state.messages.append({"role": "supervisor", "content": response})
    #     except Exception as e:
    #         st.error(f"Error generating response: {e}")