File size: 10,078 Bytes
92b06f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5282048
 
 
92b06f4
 
 
 
bcae3b5
92b06f4
90f7718
fc7f232
90f7718
 
fc7f232
 
 
90f7718
 
92b06f4
 
 
 
 
90f7718
92b06f4
 
930b737
dc103ef
92b06f4
930b737
92b06f4
 
 
 
 
 
eefb701
92b06f4
 
 
012481c
92b06f4
 
bcae3b5
 
66d652d
92b06f4
 
 
 
 
 
 
 
 
eefb701
92b06f4
 
 
012481c
92b06f4
66d652d
 
92b06f4
 
 
 
 
 
 
 
 
 
939d4d1
c40d6e0
 
 
 
 
92b06f4
c40d6e0
 
 
 
 
 
939d4d1
c40d6e0
 
7424b40
eec2077
92b06f4
f07c56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92b06f4
 
66d652d
c40d6e0
66d652d
 
c40d6e0
66d652d
c40d6e0
66d652d
 
 
 
 
 
 
c40d6e0
de2582c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66d652d
de2582c
 
 
 
 
 
 
 
 
 
66d652d
de2582c
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
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 langchain_qdrant import Qdrant
import os
import pandas as pd
import numpy as np

st.set_page_config(
    page_title="Narrativ 🧠",
    layout="wide",
    initial_sidebar_state="expanded",
    page_icon="🧠",
)

# Custom CSS for enhanced styling
st.markdown("""
    <style>
    .circular-image {
        width: 200px;
        height: 200px;
        border-radius: 50%;
        object-fit: cover;
        display: block;
        margin: 20px auto;
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    
    /* Container for search section */
    .search-container {
        background: white;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        margin: 20px 0;
    }
    
    /* Combined search input styling */
    .combined-search {
        display: flex;
        gap: 10px;
        align-items: center;
        margin-bottom: 20px;
    }
    </style>
    """, unsafe_allow_html=True)

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

#========== APP

from PIL import Image, ImageEnhance

image = Image.open('./data/Sentiment_index_traffic.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)

st.title("Narrativ 📰")

#check1 = st.button("Submit", key="submit_button")
prompt='traffic'

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

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

if 'messages' not in st.session_state:
    st.session_state.messages = []
    
prompt=st.session_state.prompt
date=st.session_state.date
# Change the sidebar background with enhanced gradient and text styling
# sideb.markdown(



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

if prompt and len(prompt1)==0:
    if date:
        try:
            data=pd.read_csv('./data/sentiment_index_traffic_index_final1.csv',
                index_col='index',
                parse_dates=True,
                infer_datetime_format=True
            ).drop(columns=['llm_index','sentiment_index_hf','confidence_hf'])

            data = data.loc[data.index == date]
            filtered_data = data[data.apply(lambda row: row.astype(str).str.contains(prompt, na=False).any(), axis=1)]
            urls = data['url'].values.flatten()
            data_all = filtered_data.values.flatten()
            docs = data_all 
            if len(docs)==0:
                st.warning("No articles found that contain the topic on the given day.")

        except Exception as e:
            st.error(f"Error processing date: {e}")
    else:
        try:
            data = pd.read_csv(
                './data/sentiment_index_traffic_index_final1.csv',
                index_col='index',
                parse_dates=True,
                infer_datetime_format=True
            ).drop(columns=['llm_index','sentiment_index_hf','confidence_hf'])
            
            filtered_data = data[data.apply(lambda row: row.astype(str).str.contains(prompt, na=False).any(), axis=1)]
            data_all = filtered_data.values.flatten()
            urls = data['url'].values.flatten()
            docs = data_all 
            if len(docs)==0:
                st.warning("No articles found that contain the topic on the given day.")
            
            #data_all = data.values.flatten()
            #docs = data_all
            # with open(f'./data/sentiment_index_traffic_index_final1.md', "w", encoding="utf-8") as file:
            #     file.write(str(data_all))
            # with open(f'./data/sentiment_index_traffic_index_final1.md', "r", encoding="utf-8") as file_content:
            #     docs = file_content.read()
        except Exception as e:
            st.error(f"Error loading data: {e}")

elif prompt==False and len(prompt1)==0:
    data=pd.read_csv('./data/sentiment_index_traffic_index_final1.csv',
                index_col='index',
                parse_dates=True,
                infer_datetime_format=True
            ).drop(columns=['llm_index','sentiment_index_hf','confidence_hf'])

    urls = data['url'].values.flatten()
    data_all = data.values.flatten()
    docs = data_all 
    if len(docs)==0:
        st.warning("No articles found that contain the topic on the given day.")
                
if len(docs)>0 and len(prompt1)==0:
    docs_text = "\n".join([f"- {value}" for value in data_all if not pd.isna(value)])
    docs = [Document(page_content=docs_text)]
    st.subheader('Excel data')
    st.write(data.drop(columns=['summary_date','summary']))

    with st.spinner("Now, I am creating the summary..."):
        try:
            no_rows=len(data)
            data_summary=data.groupby('title').first()
            filtered_data = data_summary[data_summary.apply(lambda row: row.astype(str).str.contains(prompt, na=False).any(), axis=1)]
            data_all_summary = filtered_data['summary_date'].groupby('title').first().values.flatten()
            docs_text_summary = "\n".join([f"- {value}" for value in data_all_summary if not pd.isna(value)])
            summary_data=str(docs_text_summary) #docs['summary_date']
            print('heere',summary_data)
            summary = base_llm.invoke(f"""You are a Transurban traffic analyst, that focuses on the Express lanes. Format nicely the summary into paragraphs for Streamlit. Say how many news articles are available for the given data, the number is: {no_rows}.
                                    ## Output Format:
        - **Summary of Opinions:** [Concise summary of key opinions]
        - **Sentiment Analysis:**
        - Sentiment: [Positive/Negative/Neutral, this sentiment will be based on how a particular phenomenon impacts the I-495 and I-95 express lanes.]
        - 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 the context specific to the Greater Washington Area.
        - Use professional and analytical language suitable for client reports.
        - Respond in the language of the article (mostly English).
        - From the provided context, add the URL sources, you find them here, URLs: {urls} - make sure they are clicable! related to the topic.
                                    Context: {summary_data}""").content #lcel_rag_chain.invoke({"question": prompt})
            print(summary)
            st.chat_message("assistant").write((summary))
            st.session_state.messages.append({"role": "assistant", "content": summary})
        except Exception as e:
            st.error(f"Error generating summary: {e}")
    
        if date:
            with open('./data/sentiment_index_traffic_index_final_date.md', 'w') as file:
                    file.write(str(data_all))
        else:
            with open('./data/sentiment_index_traffic_index_final1.md', 'w') as file:
                    file.write(str(data_all))


    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 = ["Summarize results", f"Explain the traffic drop", f"Explain the traffic growth"]
    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:
    with st.spinner("I am preparing the answer by analyzing the articles and our chat history..."):
        if date:
            file_path = f'./data/sentiment_index_traffic_index_final_date.md'
        else:
            file_path = f'./data/sentiment_index_traffic_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 = ""

        # Add user message to chat history
        st.session_state.messages.append({"role": "user", "content": f'You are a Transurban traffic analyst, that focuses on the Express lanes I-495 and I-95 in the Greater Washington Area. Having this knowledge answer questions: {prompt1} using context from {docs}'})
        # Display user message in chat message container
        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
                    ],
                    stream=True,
                )
                response = st.write_stream(stream)
                st.session_state.messages.append({"role": "assistant", "content": response})
            except Exception as e:
                st.error(f"Error generating response: {e}")