KatGaw commited on
Commit
36c187e
·
verified ·
1 Parent(s): 9bd1454

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import streamlit as st
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain_openai.embeddings import OpenAIEmbeddings
5
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
6
+ import markdown
7
+ from operator import itemgetter
8
+ from langchain.schema.runnable import RunnablePassthrough
9
+ from langchain_core.prompts import ChatPromptTemplate
10
+ from langchain.schema import Document
11
+ from dotenv import load_dotenv
12
+ from langchain_community.vectorstores import Qdrant
13
+ #from langchain_qdrant import Qdrant
14
+ import os
15
+ import pandas as pd
16
+ import numpy as np
17
+ import datetime
18
+
19
+
20
+ # Page config
21
+ from PIL import Image, ImageEnhance
22
+
23
+ st.set_page_config(
24
+ page_title="Narrativ 📰",
25
+ layout="wide",
26
+ initial_sidebar_state="expanded",
27
+ page_icon="🔍",
28
+ )
29
+
30
+ # Load environment variables
31
+ load_dotenv()
32
+ OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
33
+ base_llm = ChatOpenAI(model="gpt-4o")
34
+ embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
35
+
36
+ prompt='I-495'
37
+ date='2025-01-15'
38
+
39
+ # Custom CSS for centered content
40
+ st.markdown("""
41
+ <style>
42
+ .main-container {
43
+ max-width: 800px;
44
+ margin: 0 auto;
45
+ padding: 20px;
46
+ }
47
+
48
+ .stSelectbox {
49
+ max-width: 400px;
50
+ margin: 0 auto;
51
+ }
52
+
53
+ /* Center all text elements */
54
+ .centered-text {
55
+ text-align: center;
56
+ }
57
+ </style>
58
+ """, unsafe_allow_html=True)
59
+
60
+ # Header
61
+ col1, col2, col3, col4,col5 = st.columns([1, 1, 2, 1, 1])
62
+
63
+
64
+ with col3:
65
+ st.markdown("<h1 class='centered-text'>Search Narrativ</h1>", unsafe_allow_html=True)
66
+
67
+ with col4:
68
+ image = Image.open('./data/news_icon.png')
69
+ st.image(image, width=100, output_format="PNG", clamp=True)
70
+
71
+ st.markdown("<p class='centered-text'>Enter a topic and optional date to analyze traffic.</p>", unsafe_allow_html=True)
72
+
73
+ # Suggestions
74
+ topic_suggestions = [
75
+ "I-495",
76
+ "I-95",
77
+ "accident"
78
+ ]
79
+
80
+ data=pd.read_csv('./data/sentiment_index_traffic_index_final1.csv',
81
+ index_col='index',
82
+ parse_dates=True
83
+ )
84
+
85
+ # Convert the index to datetime, if not already done
86
+ data.index = pd.to_datetime(data.index)
87
+
88
+ # Generate a sorted list of unique dates
89
+ sorted_dates = sorted(pd.unique(data.index))
90
+
91
+ # Format the sorted dates as string 'YYYY-MM-DD'
92
+ date_suggestions = [pd.Timestamp(date).strftime('%Y-%m-%d') for date in sorted_dates]
93
+ date_suggestions=np.append('',date_suggestions)
94
+ # Create centered container for search
95
+ # Define the allowed date range
96
+ start_date = datetime.date(2025, 1, 15)
97
+ end_date = datetime.date(2025, 1, 21)
98
+ col1, col2= st.columns([1,1])
99
+ with col1:
100
+ prompt = st.selectbox(
101
+ "Topic:",
102
+ options=[""] + topic_suggestions,
103
+ index=0,
104
+ key="topic_select",
105
+ placeholder="Select or type a topic..."
106
+ )
107
+ with col2:
108
+
109
+ # date =
110
+ #st.date_input(
111
+ # "Choose a date:",
112
+ # # min_value=start_date,
113
+ # # max_value=end_date,
114
+ # # value=start_date # Default to start date
115
+ # )
116
+ date = st.selectbox(
117
+ "Date (optional):",
118
+ options=date_suggestions,
119
+ index=0,
120
+ key="date_select",
121
+ placeholder="Select or type a date..."
122
+ )
123
+ date=str(date)
124
+
125
+
126
+ # st.write(f"You selected: {date} for the topic: {prompt}.")
127
+
128
+ col1, col2, col3, col4 = st.columns([1,1,1,1])
129
+ with col2:
130
+ chat = st.button("chat", key="chat_button", use_container_width=True)
131
+ with col3:
132
+ tableau=st.button("tableau", key="tableau_button", use_container_width=True)
133
+
134
+ # Handle search submission
135
+ st.session_state.prompt = prompt
136
+ st.session_state.date = date
137
+
138
+ if chat:
139
+
140
+ # You can add navigation to results page or display results here
141
+ st.success(f"Searching for: {prompt} {'on ' + date if date else ''}")
142
+ # Add your search processing logic here
143
+ st.switch_page("./pages/chat.py")
144
+
145
+ if tableau:
146
+ st.switch_page("./pages/tableau.py")