Spaces:
Runtime error
Runtime error
0.2
Browse files
app.py
CHANGED
|
@@ -22,6 +22,8 @@ with st.sidebar:
|
|
| 22 |
corpus_id = st.text_input("Vectara Corpus ID", value=str(os.getenv("CORPUS_ID", "")))
|
| 23 |
openai_api_key = st.text_input("OpenAI API Key", value=os.getenv("OPENAI_API_KEY", ""))
|
| 24 |
submit_button = st.button("Submit")
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Constants
|
| 27 |
CUSTOMER_ID = customer_id if customer_id else os.getenv("CUSTOMER_ID")
|
|
@@ -60,50 +62,54 @@ prompt = PromptTemplate.from_template(
|
|
| 60 |
runnable = prompt | ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], openai_api_key=OPENAI_API_KEY) | StrOutputParser()
|
| 61 |
|
| 62 |
# Main Streamlit App
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
st.session_state
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
st.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
st.
|
| 77 |
-
|
| 78 |
-
st.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
corpus_id = st.text_input("Vectara Corpus ID", value=str(os.getenv("CORPUS_ID", "")))
|
| 23 |
openai_api_key = st.text_input("OpenAI API Key", value=os.getenv("OPENAI_API_KEY", ""))
|
| 24 |
submit_button = st.button("Submit")
|
| 25 |
+
|
| 26 |
+
keys_provided = all([customer_id, api_key, corpus_id, openai_api_key])
|
| 27 |
|
| 28 |
# Constants
|
| 29 |
CUSTOMER_ID = customer_id if customer_id else os.getenv("CUSTOMER_ID")
|
|
|
|
| 62 |
runnable = prompt | ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], openai_api_key=OPENAI_API_KEY) | StrOutputParser()
|
| 63 |
|
| 64 |
# Main Streamlit App
|
| 65 |
+
if keys_provided:
|
| 66 |
+
st.title("Legal Consultation Chat")
|
| 67 |
+
|
| 68 |
+
# Initialize chat history
|
| 69 |
+
if "messages" not in st.session_state:
|
| 70 |
+
st.session_state.messages = []
|
| 71 |
+
|
| 72 |
+
# Display chat messages from history on app rerun
|
| 73 |
+
for message in st.session_state.messages:
|
| 74 |
+
with st.chat_message(message["role"]):
|
| 75 |
+
st.markdown(message["content"])
|
| 76 |
+
|
| 77 |
+
# Accept user input and run the main chat interaction
|
| 78 |
+
if user_input := st.chat_input("Enter your issue:"):
|
| 79 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 80 |
+
with st.chat_message("user"):
|
| 81 |
+
st.markdown(user_input)
|
| 82 |
+
|
| 83 |
+
knowledge_content = get_knowledge_content(vectara_client, user_input)
|
| 84 |
+
print("__________________ Start of knowledge content __________________")
|
| 85 |
+
print(knowledge_content)
|
| 86 |
+
response = runnable.invoke({"knowledge": knowledge_content, "issue": user_input})
|
| 87 |
+
|
| 88 |
+
response_words = response.split()
|
| 89 |
+
with st.chat_message("assistant"):
|
| 90 |
+
message_placeholder = st.empty()
|
| 91 |
+
full_response = ""
|
| 92 |
+
for word in response_words:
|
| 93 |
+
full_response += word + " "
|
| 94 |
+
time.sleep(0.05)
|
| 95 |
+
message_placeholder.markdown(full_response + "▌")
|
| 96 |
+
message_placeholder.markdown(full_response)
|
| 97 |
+
|
| 98 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 99 |
+
|
| 100 |
+
# Run when the submit button is pressed
|
| 101 |
+
if submit_button and uploaded_file:
|
| 102 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
|
| 103 |
+
tmpfile.write(uploaded_file.getvalue())
|
| 104 |
+
tmp_filename = tmpfile.name
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
vectara_client.add_files([tmp_filename])
|
| 108 |
+
st.sidebar.success("PDF file successfully uploaded to Vectara!")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
st.sidebar.error(f"An error occurred: {str(e)}")
|
| 111 |
+
finally:
|
| 112 |
+
os.remove(tmp_filename) # Clean up temporary file
|
| 113 |
+
else:
|
| 114 |
+
# Not all keys are provided, instruct the user to input them
|
| 115 |
+
st.warning("Please input all required API keys in the sidebar to proceed.")
|