ysharma HF staff commited on
Commit
cf01eeb
1 Parent(s): 98e782e

update to handle empty chunk values from aPI

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -73,18 +73,20 @@ def predict(inputs, top_p, temperature, openai_api_key, chat_counter, chatbot=[]
73
  counter+=1
74
  # check whether each line is non-empty
75
  if chunk :
76
- # decode each line as response data is in bytes
77
- if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
78
- break
79
- #print(json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"])
80
- partial_words = partial_words + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
81
- if token_counter == 0:
82
- history.append(" " + partial_words)
83
- else:
84
- history[-1] = partial_words
85
- chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
86
- token_counter+=1
87
- yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
 
 
88
 
89
 
90
 
 
73
  counter+=1
74
  # check whether each line is non-empty
75
  if chunk :
76
+ chunk_str = chunk.decode()[6:]
77
+ if chunk_str:
78
+ delta = json.loads(chunk_str)['choices'][0]["delta"]
79
+ if len(delta) == 0:
80
+ break
81
+ # decode each line as response data is in bytes
82
+ partial_words = partial_words + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
83
+ if token_counter == 0:
84
+ history.append(" " + partial_words)
85
+ else:
86
+ history[-1] = partial_words
87
+ chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
88
+ token_counter+=1
89
+ yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
90
 
91
 
92