asthaa30 commited on
Commit
e871e90
·
verified ·
1 Parent(s): ff1400c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -32,17 +32,27 @@ def respond(
32
 
33
  response = ""
34
 
35
- for message in client.chat_completion(
36
- messages,
37
- max_tokens=max_tokens,
38
- stream=True,
39
- temperature=temperature,
40
- top_p=top_p,
41
- ):
42
- token = message.choices[0].delta.content
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- response += token
45
- yield response
46
 
47
  """
48
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
32
 
33
  response = ""
34
 
35
+ try:
36
+ for message in client.chat_completion(
37
+ messages,
38
+ max_tokens=max_tokens,
39
+ stream=True,
40
+ temperature=temperature,
41
+ top_p=top_p,
42
+ ):
43
+ payload = message.choices[0].delta.content
44
+ # Attempt to decode the payload, but handle JSON errors gracefully
45
+ try:
46
+ token = json.loads(payload.lstrip("data:").rstrip("/n"))["content"]
47
+ response += token
48
+ yield response
49
+ except json.JSONDecodeError as e:
50
+ print(f"JSON decoding error: {e} - payload: {payload}")
51
+ continue # Skip malformed payloads and continue
52
+ except Exception as e:
53
+ print(f"Error in chat completion: {e}")
54
+ yield "Error processing response from the model."
55
 
 
 
56
 
57
  """
58
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface