asitts commited on
Commit
99982a5
β€’
1 Parent(s): ca3d49a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -2
app.py CHANGED
@@ -1,4 +1,48 @@
1
  import streamlit as st
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from llama_index import VectorStoreIndex, ServiceContext, Document
3
+ from llama_index.llms import OpenAI
4
+ import openai
5
+ from llama_index import SimpleDirectoryReader
6
+ import os
7
 
8
+ st.set_page_config(page_title="SmartStarts", page_icon="πŸ€–", layout="centered", initial_sidebar_state="auto", menu_items=None)
9
+
10
+ test_key_print = os.environ['OPENAI_KEY']
11
+
12
+ openai.api_key = test_key_print
13
+ st.title("SmartStarts Interview πŸ’¬πŸ€–")
14
+ st.info("Do not enter any none public info. This is for internal test / demo purposes only.", icon="πŸ“ƒ")
15
+
16
+ if "messages" not in st.session_state.keys(): # Initialize the chat messages history
17
+ st.session_state.messages = [
18
+ {"role": "assistant", "content": "Which SmartStart would you like to provide information for"}
19
+ ]
20
+
21
+ @st.cache_resource(show_spinner=False)
22
+ def load_data():
23
+ with st.spinner(text="Loading and indexing the HUD Audit Guide – hang tight! This should take 1-2 minutes."):
24
+ reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
25
+ docs = reader.load_data()
26
+ service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo", temperature=0.5, system_prompt="You are an expert on soliciting information on how assets, called SmartStarts, could be utilized to deliver professional services more efficiently and effecively. product and knowledge experts. Your objective is to understand what th the HUD Audit Guide and your job is to answer technical questions. Assume that all questions are related to the SmartStart entered at the beginning of the conversation. Keep your answers technical and based on facts – do not hallucinate features."))
27
+ index = VectorStoreIndex.from_documents(docs, service_context=service_context)
28
+ return index
29
+
30
+ index = load_data()
31
+ #chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True, system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
32
+ chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
33
+
34
+ if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
35
+ st.session_state.messages.append({"role": "user", "content": prompt})
36
+
37
+ for message in st.session_state.messages: # Display the prior chat messages
38
+ with st.chat_message(message["role"]):
39
+ st.write(message["content"])
40
+
41
+ # If last message is not from assistant, generate a new response
42
+ if st.session_state.messages[-1]["role"] != "assistant":
43
+ with st.chat_message("assistant"):
44
+ with st.spinner("Thinking..."):
45
+ response = chat_engine.chat(prompt)
46
+ st.write(response.response)
47
+ message = {"role": "assistant", "content": response.response}
48
+ st.session_state.messages.append(message) # Add response to message history