TrafficLens / app.py
KatGaw's picture
Update app.py
1f8d01f verified
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
import datetime
# Page config
from PIL import Image, ImageEnhance
st.set_page_config(
page_title="TrafficLens πŸ“°",
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")
prompt='I-495'
date='2025-01-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])
with col3:
st.markdown("<h1 class='centered-text'>Search TrafficLens</h1>", unsafe_allow_html=True)
with col4:
image = Image.open('./data/news_icon.png')
st.image(image, width=100, output_format="PNG", clamp=True)
st.markdown("<p class='centered-text'>Enter a topic and optional date to analyze traffic.</p>", unsafe_allow_html=True)
# Suggestions
topic_suggestions = [
"accident",
"traffic",
"I-95"
]
data=pd.read_csv('./data/sentiment_index_traffic_index_final1.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)
col1, col2= st.columns([1,1])
with col1:
prompt = st.selectbox(
"Topic:",
options=[""] + topic_suggestions,
index=0,
key="topic_select",
placeholder="Select or type a topic..."
)
with col2:
# date =
#st.date_input(
# "Choose a date:",
# # min_value=start_date,
# # max_value=end_date,
# # value=start_date # Default to start date
# )
date = st.selectbox(
"Date (optional):",
options=date_suggestions,
index=0,
key="date_select",
placeholder="Select or type a date..."
)
date=str(date)
# st.write(f"You selected: {date} for the topic: {prompt}.")
col1, col2, col3, col4 = st.columns([1,1,1,1])
with col2:
chat = st.button("chat", key="chat_button", use_container_width=True)
with col3:
tableau=st.button("tableau", key="tableau_button", use_container_width=True)
# Handle search submission
st.session_state.prompt = prompt
st.session_state.date = date
if chat:
# You can add navigation to results page or display results here
st.success(f"Searching for: {prompt} {'on ' + date if date else ''}")
# Add your search processing logic here
st.switch_page("./pages/chat.py")
if tableau:
st.switch_page("./pages/tableau.py")