MaziyarPanahi commited on
Commit
17b76bb
1 Parent(s): 6b22bf5

new update

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -163,33 +163,33 @@ with gr.Blocks() as demo:
163
  stream=True,
164
  timeout=(10, 30),
165
  )
166
-
167
- for line in response.iter_lines():
168
- if line:
169
- for line in response.iter_lines():
170
- # Filter out keep-alive new lines
171
- if line:
172
- data = line.decode("utf-8").lstrip("data: ")
173
- # Check if the examples are valid
174
- valid_check = is_valid_json(data)
175
- if valid_check[0]:
176
- try:
177
- # Attempt to parse the JSON dataa
178
- # json_data = json.loads(data)
179
- json_data = valid_check[1]
180
-
181
- delta_content = (
182
- json_data.get("choices", [{}])[0]
183
- .get("delta", {})
184
- .get("content", "")
185
- )
186
-
187
- if delta_content: # Ensure there's content to print
188
- history[-1][1] += delta_content
189
- time.sleep(0.05)
190
- yield history
191
- except json.JSONDecodeError as e:
192
- print(f"Error decoding JSON: {e} date: {data}")
193
  except requests.exceptions.RequestException as e:
194
  print(f"An error occurred: {e}")
195
 
 
163
  stream=True,
164
  timeout=(10, 30),
165
  )
166
+ if response.status_code == 200:
167
+ for line in response.iter_lines():
168
+ if line:
169
+ for line in response.iter_lines():
170
+ # Filter out keep-alive new lines
171
+ if line:
172
+ data = line.decode("utf-8").lstrip("data: ")
173
+ # Check if the examples are valid
174
+ valid_check = is_valid_json(data)
175
+ if valid_check[0]:
176
+ try:
177
+ # Attempt to parse the JSON dataa
178
+ # json_data = json.loads(data)
179
+ json_data = valid_check[1]
180
+
181
+ delta_content = (
182
+ json_data.get("choices", [{}])[0]
183
+ .get("delta", {})
184
+ .get("content", "")
185
+ )
186
+
187
+ if delta_content: # Ensure there's content to print
188
+ history[-1][1] += delta_content
189
+ time.sleep(0.05)
190
+ yield history
191
+ except json.JSONDecodeError as e:
192
+ print(f"Error decoding JSON: {e} date: {data}")
193
  except requests.exceptions.RequestException as e:
194
  print(f"An error occurred: {e}")
195