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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -99,34 +99,28 @@ def respond(message, api_key, max_tokens, top_p, temperature):
99
  "input": message,
100
  "stream": True
101
  }
102
- line_content = None
103
-
104
  try:
105
  response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
106
 
107
  if response.status_code == 200:
108
  assistant_reply = ""
109
  for line in response.iter_lines(decode_unicode=True):
110
- line = line.strip()
111
- if not line:
112
- continue
113
-
114
- print(f"Received line: {line}")
115
-
116
- if line.startswith("data:"):
117
- line_content = line[5:].strip()
118
- if not line_content:
119
- continue
120
-
121
- try:
122
- chunk = json.loads(line_content)
123
- chunk_message = chunk.get("chunk", {}).get("content", "")
124
- assistant_reply += chunk_message
125
- yield assistant_reply
126
- except json.JSONDecodeError as e:
127
- print(f"Stream chunk error: {e} with line: {line_content}")
128
- yield f"Error in stream chunk: {e}" # Optionally notify the user
129
- continue
130
 
131
 
132
  history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH))
 
99
  "input": message,
100
  "stream": True
101
  }
102
+
 
103
  try:
104
  response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
105
 
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
 
126
  history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH))