Spaces:
Runtime error
Runtime error
update to handle empty chunk values from aPI
Browse files
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 |
-
|
77 |
-
if
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
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 |
|