richardkovacs commited on
Commit
24c5da9
1 Parent(s): eac00b8

feat: handle errors more gracefully

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -35,19 +35,25 @@ def respond(
35
 
36
  response = ""
37
 
38
- for message in client.chat_completion(
39
- messages,
40
- max_tokens=max_tokens,
41
- stream=True,
42
- temperature=temperature,
43
- top_p=top_p,
44
- ):
45
- token = message.choices[0].delta.content
 
46
 
47
- if token:
48
- response += token
49
 
50
- yield response
 
 
 
 
 
51
 
52
  """
53
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
35
 
36
  response = ""
37
 
38
+ try:
39
+ for message in client.chat_completion(
40
+ messages,
41
+ max_tokens=max_tokens,
42
+ stream=True,
43
+ temperature=temperature,
44
+ top_p=top_p,
45
+ ):
46
+ token = message.choices[0].delta.content
47
 
48
+ if token:
49
+ response += token
50
 
51
+ yield response
52
+ except ValueError as e:
53
+ print("ERROR:", e)
54
+ raise gr.Error("We have some trouble connecting to the model. Please try again later.")
55
+ except Exception as e:
56
+ raise gr.Error("An unknown error occurred. Please try again later.")
57
 
58
  """
59
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface