Spaces:
Runtime error
Runtime error
Commit
·
6cc6a76
1
Parent(s):
f2a6896
Updated app.py
Browse filesI have handled the rate limit error.
app.py
CHANGED
|
@@ -1,37 +1,42 @@
|
|
| 1 |
-
# Q&A Chatbot
|
| 2 |
from langchain.llms import OpenAI
|
| 3 |
-
|
| 4 |
-
#from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
#load_dotenv() # take environment variables from .env.
|
| 7 |
-
|
| 8 |
import streamlit as st
|
| 9 |
import os
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
## Function to load OpenAI model and get respones
|
| 13 |
-
|
| 14 |
def get_openai_response(question):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
st.set_page_config(page_title="Q&A Demo")
|
| 22 |
-
|
| 23 |
st.header("Langchain Application")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
submit=st.button("Ask the question")
|
| 29 |
-
|
| 30 |
-
## If ask button is clicked
|
| 31 |
|
|
|
|
| 32 |
if submit:
|
| 33 |
-
|
| 34 |
-
st.write(response)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain.llms import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
# Function to load OpenAI model and get responses
|
|
|
|
|
|
|
| 7 |
def get_openai_response(question):
|
| 8 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
llm = OpenAI(openai_api_key=openai_api_key, model_name="text-davinci-003", temperature=0.5)
|
| 10 |
+
|
| 11 |
+
# Retry logic
|
| 12 |
+
max_retries = 3
|
| 13 |
+
retries = 0
|
| 14 |
+
|
| 15 |
+
while retries < max_retries:
|
| 16 |
+
try:
|
| 17 |
+
response = llm(question)
|
| 18 |
+
return response
|
| 19 |
+
except RateLimitError as e:
|
| 20 |
+
print(f"Rate limit exceeded. Waiting for {e.retry_after} seconds and retrying...")
|
| 21 |
+
time.sleep(e.retry_after)
|
| 22 |
+
retries += 1
|
| 23 |
+
|
| 24 |
+
print("Exceeded maximum number of retries. Please try again later.")
|
| 25 |
+
return None # Or handle the error in an appropriate way in your code
|
| 26 |
+
|
| 27 |
+
# Streamlit app
|
| 28 |
st.set_page_config(page_title="Q&A Demo")
|
|
|
|
| 29 |
st.header("Langchain Application")
|
| 30 |
|
| 31 |
+
input_question = st.text_input("Input: ", key="input")
|
| 32 |
+
submit = st.button("Ask the question")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# If the "Ask" button is clicked
|
| 35 |
if submit:
|
| 36 |
+
response = get_openai_response(input_question)
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
if response is not None:
|
| 39 |
+
st.subheader("The Response is:")
|
| 40 |
+
st.write(response)
|
| 41 |
+
else:
|
| 42 |
+
st.subheader("Error: Unable to get response. Please try again later.")
|