Spaces:
Sleeping
Sleeping
Commit
·
6a70465
1
Parent(s):
94b7b89
Updated app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,32 @@
|
|
| 1 |
-
## Conversational Q&A Chatbot
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
st.set_page_config(page_title="Conversational Q&A Chatbot")
|
| 9 |
st.header("Hey, Let's Chat")
|
| 10 |
|
|
@@ -12,29 +34,24 @@ from dotenv import load_dotenv
|
|
| 12 |
load_dotenv()
|
| 13 |
import os
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
if 'flowmessages' not in st.session_state:
|
| 18 |
-
st.session_state['flowmessages']=[
|
| 19 |
-
SystemMessage(content="
|
| 20 |
]
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
st.session_state['flowmessages'].append(HumanMessage(content=question))
|
| 27 |
-
answer=chat(st.session_state['flowmessages'])
|
| 28 |
-
st.session_state['flowmessages'].append(AIMessage(content=answer.content))
|
| 29 |
-
return answer.content
|
| 30 |
-
|
| 31 |
-
input=st.text_input("Input: ",key="input")
|
| 32 |
-
response=get_chatmodel_response(input)
|
| 33 |
-
|
| 34 |
-
submit=st.button("Ask the question")
|
| 35 |
-
|
| 36 |
-
## If ask button is clicked
|
| 37 |
|
|
|
|
| 38 |
if submit:
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 4 |
|
| 5 |
+
def get_chatmodel_response(question):
|
| 6 |
+
# Retry logic
|
| 7 |
+
max_retries = 3
|
| 8 |
+
retries = 0
|
| 9 |
+
|
| 10 |
+
while retries < max_retries:
|
| 11 |
+
try:
|
| 12 |
+
st.session_state['flowmessages'].append(HumanMessage(content=question))
|
| 13 |
+
answer = chat(st.session_state['flowmessages'])
|
| 14 |
+
st.session_state['flowmessages'].append(AIMessage(content=answer.content))
|
| 15 |
+
return answer.content
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Error: {e}")
|
| 18 |
+
if "Rate limit" in str(e):
|
| 19 |
+
print(f"Rate limit exceeded. Waiting and retrying...")
|
| 20 |
+
time.sleep(5) # Adjust the waiting time as needed
|
| 21 |
+
retries += 1
|
| 22 |
+
else:
|
| 23 |
+
print("Unhandled exception. Please try again later.")
|
| 24 |
+
break
|
| 25 |
+
|
| 26 |
+
print("Exceeded the maximum number of retries. Please try again later.")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
# Streamlit app setup
|
| 30 |
st.set_page_config(page_title="Conversational Q&A Chatbot")
|
| 31 |
st.header("Hey, Let's Chat")
|
| 32 |
|
|
|
|
| 34 |
load_dotenv()
|
| 35 |
import os
|
| 36 |
|
| 37 |
+
# Assuming the existence of the ChatOpenAI class
|
| 38 |
+
chat = ChatOpenAI(temperature=0.5)
|
| 39 |
|
| 40 |
if 'flowmessages' not in st.session_state:
|
| 41 |
+
st.session_state['flowmessages'] = [
|
| 42 |
+
SystemMessage(content="You are a comedian AI assistant")
|
| 43 |
]
|
| 44 |
|
| 45 |
+
# Streamlit UI
|
| 46 |
+
input_question = st.text_input("Input: ", key="input")
|
| 47 |
+
submit = st.button("Ask the question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# If the "Ask" button is clicked
|
| 50 |
if submit:
|
| 51 |
+
response = get_chatmodel_response(input_question)
|
| 52 |
+
|
| 53 |
+
if response is not None:
|
| 54 |
+
st.subheader("The Response is:")
|
| 55 |
+
st.write(response)
|
| 56 |
+
else:
|
| 57 |
+
st.subheader("Error: Unable to get response. Please try again later.")
|