Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -106,20 +106,21 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 106 |
if response.status_code == 200:
|
| 107 |
assistant_reply = ""
|
| 108 |
for line in response.iter_lines(decode_unicode=True):
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
print(f"Stream chunk error: {e} with line: {line}")
|
| 123 |
|
| 124 |
|
| 125 |
|
|
@@ -215,17 +216,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 215 |
last_message_state = gr.State("")
|
| 216 |
|
| 217 |
def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
|
| 218 |
-
|
| 219 |
-
|
| 220 |
|
| 221 |
-
|
| 222 |
-
partial_history = history
|
| 223 |
for partial_reply in respond(message, api_key, max_tokens, top_p, temperature):
|
| 224 |
-
|
| 225 |
-
|
| 226 |
|
| 227 |
-
history, assistant_reply = partial_history, partial_reply if 'partial_reply' in locals() else ("", "")
|
| 228 |
-
yield render_message(history), history, ""
|
| 229 |
|
| 230 |
|
| 231 |
def regenerate_response(history, last_message, max_tokens, top_p, temperature):
|
|
|
|
| 106 |
if response.status_code == 200:
|
| 107 |
assistant_reply = ""
|
| 108 |
for line in response.iter_lines(decode_unicode=True):
|
| 109 |
+
if line.strip():
|
| 110 |
+
if line.strip() == "[DONE]":
|
| 111 |
+
break
|
| 112 |
|
| 113 |
+
try:
|
| 114 |
+
if line.startswith("data:"):
|
| 115 |
+
line = line[5:].strip()
|
| 116 |
+
|
| 117 |
+
if line:
|
| 118 |
+
chunk = json.loads(line)
|
| 119 |
+
chunk_message = chunk.get("chunk", {}).get("content", "")
|
| 120 |
+
assistant_reply += chunk_message
|
| 121 |
+
yield assistant_reply
|
| 122 |
+
except json.JSONDecodeError as e:
|
| 123 |
+
print(f"Stream chunk error: {e} with line: {line}")
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
|
|
|
|
| 216 |
last_message_state = gr.State("")
|
| 217 |
|
| 218 |
def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
|
| 219 |
+
loading_message = history + [(message, "Loading...", "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH)]
|
| 220 |
+
yield render_message(loading_message), loading_message, ""
|
| 221 |
|
| 222 |
+
partial_history = history
|
|
|
|
| 223 |
for partial_reply in respond(message, api_key, max_tokens, top_p, temperature):
|
| 224 |
+
partial_history = history + [(message, partial_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH)]
|
| 225 |
+
yield render_message(partial_history), partial_history, ""
|
| 226 |
|
|
|
|
|
|
|
| 227 |
|
| 228 |
|
| 229 |
def regenerate_response(history, last_message, max_tokens, top_p, temperature):
|