MaziyarPanahi commited on
Commit
a00ee3f
1 Parent(s): c6abdf4

new update

Browse files
Files changed (1) hide show
  1. app.py +63 -28
app.py CHANGED
@@ -3,6 +3,8 @@ import time
3
  import requests
4
  import json
5
  import os
 
 
6
 
7
  API_URL = os.getenv("API_URL")
8
  API_KEY = os.getenv("API_KEY")
@@ -114,6 +116,26 @@ with gr.Blocks() as demo:
114
  history_messages = sys_msg + history_messages
115
  print(history_messages)
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  data = {
118
  "messages": history_messages,
119
  "stream": True,
@@ -129,34 +151,47 @@ with gr.Blocks() as demo:
129
  # },
130
  }
131
 
132
- # Making the POST request and streaming the response
133
- response = requests.post(
134
- url, headers=headers, data=json.dumps(data), stream=True
135
- )
136
- for line in response.iter_lines():
137
- # Filter out keep-alive new lines
138
- if line:
139
- data = line.decode("utf-8").lstrip("data: ")
140
- # Check if the examples are valid
141
- valid_check = is_valid_json(data)
142
- if valid_check[0]:
143
- try:
144
- # Attempt to parse the JSON dataa
145
- # json_data = json.loads(data)
146
- json_data = valid_check[1]
147
-
148
- delta_content = (
149
- json_data.get("choices", [{}])[0]
150
- .get("delta", {})
151
- .get("content", "")
152
- )
153
-
154
- if delta_content: # Ensure there's content to print
155
- history[-1][1] += delta_content
156
- time.sleep(0.05)
157
- yield history
158
- except json.JSONDecodeError as e:
159
- print(f"Error decoding JSON: {e} date: {data}")
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  msg.submit(
162
  user, [msg, chatbot], [msg, chatbot], queue=True, concurrency_limit=10
 
3
  import requests
4
  import json
5
  import os
6
+ from urllib3.util import Retry
7
+ from requests.adapters import HTTPAdapter
8
 
9
  API_URL = os.getenv("API_URL")
10
  API_KEY = os.getenv("API_KEY")
 
116
  history_messages = sys_msg + history_messages
117
  print(history_messages)
118
 
119
+ # Create a session object
120
+ session = requests.Session()
121
+
122
+ # Define the retry strategy
123
+ retries = Retry(
124
+ total=5, # Total number of retries to allow
125
+ backoff_factor=1, # A backoff factor to apply between attempts
126
+ status_forcelist=[
127
+ 500,
128
+ 502,
129
+ 503,
130
+ 504,
131
+ ], # A set of HTTP status codes that we should force a retry on
132
+ method_whitelist=[
133
+ "HEAD",
134
+ "GET",
135
+ "OPTIONS",
136
+ "POST",
137
+ ], # HTTP methods to retry on
138
+ )
139
  data = {
140
  "messages": history_messages,
141
  "stream": True,
 
151
  # },
152
  }
153
 
154
+ # Mount it for http usage
155
+ session.mount("http://", HTTPAdapter(max_retries=retries))
156
+
157
+ # Making the POST request with increased timeout and retry logic
158
+ try:
159
+ response = session.post(
160
+ url,
161
+ headers=headers,
162
+ data=json.dumps(data),
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
 
196
  msg.submit(
197
  user, [msg, chatbot], [msg, chatbot], queue=True, concurrency_limit=10