Shreyas094 commited on
Commit
90977b9
·
verified ·
1 Parent(s): 49d681b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -15,7 +15,6 @@ from langchain_core.documents import Document
15
  from huggingface_hub import InferenceClient
16
  import inspect
17
  import logging
18
- from gradio.exceptions import CancelledError
19
 
20
 
21
  # Set up basic configuration for logging
@@ -159,7 +158,11 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=1, tempe
159
  continue
160
  print(f"Cloudflare API call {i+1} completed")
161
  except Exception as e:
162
- print(f"Error in generating response from Cloudflare: {str(e)}")
 
 
 
 
163
  else:
164
  # Original Hugging Face API logic
165
  client = InferenceClient(model, token=huggingface_token)
@@ -185,7 +188,11 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=1, tempe
185
  yield full_response
186
  print(f"Hugging Face API call {i+1} completed")
187
  except Exception as e:
188
- print(f"Error in generating response from Hugging Face: {str(e)}")
 
 
 
 
189
 
190
  # Clean up the response
191
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
@@ -261,12 +268,14 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
261
  for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
262
  history[-1] = (message, partial_response)
263
  yield history
264
- except CancelledError:
265
- yield history
266
  except Exception as e:
267
- logging.error(f"Unexpected error in respond: {str(e)}")
268
- history[-1] = (message, f"An unexpected error occurred: {str(e)}")
269
- yield history
 
 
 
 
270
 
271
  logging.basicConfig(level=logging.DEBUG)
272
 
 
15
  from huggingface_hub import InferenceClient
16
  import inspect
17
  import logging
 
18
 
19
 
20
  # Set up basic configuration for logging
 
158
  continue
159
  print(f"Cloudflare API call {i+1} completed")
160
  except Exception as e:
161
+ if "cancelled" in str(e).lower():
162
+ print("Generation cancelled")
163
+ return
164
+ else:
165
+ print(f"Error in generating response from Cloudflare: {str(e)}")
166
  else:
167
  # Original Hugging Face API logic
168
  client = InferenceClient(model, token=huggingface_token)
 
188
  yield full_response
189
  print(f"Hugging Face API call {i+1} completed")
190
  except Exception as e:
191
+ if "cancelled" in str(e).lower():
192
+ print("Generation cancelled")
193
+ return
194
+ else:
195
+ print(f"Error in generating response from Hugging Face: {str(e)}")
196
 
197
  # Clean up the response
198
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
 
268
  for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
269
  history[-1] = (message, partial_response)
270
  yield history
 
 
271
  except Exception as e:
272
+ if "cancelled" in str(e).lower():
273
+ # This is likely a cancellation, so we'll just return the current history
274
+ yield history
275
+ else:
276
+ logging.error(f"Unexpected error in respond: {str(e)}")
277
+ history[-1] = (message, f"An unexpected error occurred: {str(e)}")
278
+ yield history
279
 
280
  logging.basicConfig(level=logging.DEBUG)
281