MarziehFadaee commited on
Commit
f301806
·
verified ·
1 Parent(s): cc575f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import re
5
  import uuid
6
  from functools import partial
 
7
 
8
 
9
 
@@ -28,21 +29,23 @@ def generate_response(user_message, history=None):
28
  stream = co.chat_stream(message=user_message, conversation_id=cid, model='command-r', connectors=[], temperature=0.3)
29
 
30
  output = ""
 
31
  for idx, response in enumerate(stream):
32
- if response.event_type == "text-generation":
33
- output += response.text
34
-
35
- if idx == 0:
36
- history.append(" " + output)
 
 
 
 
 
 
 
37
  else:
38
- history[-1] = output
39
-
40
- chat = [
41
- (history[i].strip(), history[i + 1].strip())
42
- for i in range(0, len(history) - 1, 2)
43
- ]
44
-
45
- yield chat, history
46
 
47
  return chat, history
48
 
 
4
  import re
5
  import uuid
6
  from functools import partial
7
+ from urllib.error import HTTPError
8
 
9
 
10
 
 
29
  stream = co.chat_stream(message=user_message, conversation_id=cid, model='command-r', connectors=[], temperature=0.3)
30
 
31
  output = ""
32
+
33
  for idx, response in enumerate(stream):
34
+ if response.status_code == HTTPStatus.OK:
35
+ if response.event_type == "text-generation":
36
+ output += response.text
37
+ if idx == 0:
38
+ history.append(" " + output)
39
+ else:
40
+ history[-1] = output
41
+ chat = [
42
+ (history[i].strip(), history[i + 1].strip())
43
+ for i in range(0, len(history) - 1, 2)
44
+ ]
45
+ yield chat, history
46
  else:
47
+ raise HTTPError(f"Request id: {response.request_id}, Status code: {response.status_code},
48
+ error code: {response.code}, error message: {response.message}")
 
 
 
 
 
 
49
 
50
  return chat, history
51