USC-GPT / app.py
bhulston's picture
Uploading relevant documents
c755297
raw
history blame
2.62 kB
import streamlit as st
from transformers import pipeline
from PIL import Image
from datetime import time as t
import time
from operator import itemgetter
import os
import json
import getpass
from dotenv import find_dotenv, load_dotenv
from langchain.vectorstores import Pinecone
from langchain.embeddings import OpenAIEmbeddings
import pinecone
from agents.keywords import keyword_agent
from agents.filter import filter_agent
from agents.results import results_agent
from reranker import reranker
from utils import build_filter
load_dotenv(find_dotenv())
OPENAI_API = os.environ.get("OPENAI_API_KEY")
PINECONE_API = os.environ.get("PINECONE_API")
pinecone.init(
api_key= PINECONE_API,
environment="gcp-starter"
)
index_name = "use-class-db"
embeddings = OpenAIEmbeddings()
index = pinecone.Index(index_name)
k = 5
if "messages" not in st.session_state:
st.session_state.messages = []
st.title("USC GPT - Find the perfect class")
class_time = st.slider(
"Filter Class Times:",
value=(t(11, 30), t(12, 45)))
# st.write("You're scheduled for:", class_time)
units = st.slider(
"Number of units",
1, 4,
value = (1, 4)
)
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("What kind of class are you looking for?"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
response = filter_agent(prompt)
query = response
response = index.query(
vector= embeddings.embed_query(query),
# filter= build_filter(json),
top_k=5,
include_metadata=True
)
response = reranker(query, response)
result_query = 'Original Query:' + query + 'Query Results:' + str(response)
print(results_agent(result_query))
### GPT Response
# Display assistant response in chat message container
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
assistant_response = "Hello there! How can I assist you today?"
# Simulate stream of response with milliseconds delay
for chunk in assistant_response.split():
full_response += chunk + " "
time.sleep(0.05)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": full_response})