POLRAMBORA commited on
Commit
3f1142b
·
verified ·
1 Parent(s): d1f1b10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
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
- print(f"Received line: {line}")
 
 
110
 
111
- if line.strip():
112
- try:
113
- if line.startswith("data:"):
114
- line = line[5:].strip()
115
-
116
- if line:
117
- chunk = json.loads(line)
118
- chunk_message = chunk.get("chunk", {}).get("content", "")
119
- assistant_reply += chunk_message
120
- yield assistant_reply
121
- except json.JSONDecodeError as e:
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
- loading_message = history + [(message, "Loading...", "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH)]
219
- yield render_message(loading_message), loading_message, ""
220
 
221
- assistant_reply = ""
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
- 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):