Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,7 @@ from langchain_core.output_parsers import StrOutputParser
|
|
14 |
from langchain_core.runnables import RunnableLambda
|
15 |
from datetime import date
|
16 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
17 |
-
|
18 |
import time
|
19 |
# Environment variables
|
20 |
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
|
@@ -125,15 +125,23 @@ def clear_chat_history():
|
|
125 |
st.session_state.messages = []
|
126 |
st.session_state.context_sources = []
|
127 |
st.session_state.key = 0
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
# In your Streamlit app
|
130 |
def generate_response(chain, query, context):
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
except TimeoutError:
|
135 |
return "I apologize, but I couldn't generate a response in time. The query might be too complex for me to process quickly. Could you try simplifying your question?"
|
136 |
-
|
137 |
|
138 |
# Sidebar
|
139 |
with st.sidebar:
|
|
|
14 |
from langchain_core.runnables import RunnableLambda
|
15 |
from datetime import date
|
16 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
17 |
+
import threading
|
18 |
import time
|
19 |
# Environment variables
|
20 |
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
|
|
|
125 |
st.session_state.messages = []
|
126 |
st.session_state.context_sources = []
|
127 |
st.session_state.key = 0
|
128 |
+
def run_with_timeout(func, args, timeout):
|
129 |
+
result = [None]
|
130 |
+
def worker():
|
131 |
+
result[0] = func(*args)
|
132 |
+
thread = threading.Thread(target=worker)
|
133 |
+
thread.start()
|
134 |
+
thread.join(timeout)
|
135 |
+
if thread.is_alive():
|
136 |
+
return None
|
137 |
+
return result[0]
|
138 |
# In your Streamlit app
|
139 |
def generate_response(chain, query, context):
|
140 |
+
timeout_seconds = 60
|
141 |
+
result = run_with_timeout(chain.run, ({"context": context, "question": query},), timeout_seconds)
|
142 |
+
if result is None:
|
|
|
143 |
return "I apologize, but I couldn't generate a response in time. The query might be too complex for me to process quickly. Could you try simplifying your question?"
|
144 |
+
return result
|
145 |
|
146 |
# Sidebar
|
147 |
with st.sidebar:
|