Spaces:
Runtime error
Runtime error
ALMOST SO ABCK
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import requests
|
|
3 |
import json
|
4 |
import os
|
5 |
import datetime
|
|
|
6 |
from requests.exceptions import RequestException
|
7 |
|
8 |
api_keys_env = os.environ.get('API_KEYS')
|
@@ -10,6 +11,7 @@ if api_keys_env:
|
|
10 |
API_KEYS = [key.strip() for key in api_keys_env.strip().split('\n') if key.strip()]
|
11 |
else:
|
12 |
raise ValueError("all keez ded go kys")
|
|
|
13 |
API_URL = os.environ.get('API_URL')
|
14 |
|
15 |
DEFAULT_PARAMS = {
|
@@ -79,60 +81,118 @@ def predict(message, history, system_prompt, temperature, top_p, top_k, min_p, t
|
|
79 |
|
80 |
api_key_index = 0
|
81 |
retries = 0
|
82 |
-
max_retries = len(API_KEYS)
|
|
|
|
|
|
|
83 |
|
84 |
while retries < max_retries:
|
85 |
-
|
|
|
86 |
|
|
|
87 |
headers = {
|
88 |
"Authorization": f"Bearer {current_api_key}",
|
89 |
"Content-Type": "application/json"
|
90 |
}
|
91 |
|
92 |
try:
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
break
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
try:
|
108 |
-
|
109 |
-
if '
|
110 |
-
|
111 |
-
|
112 |
-
if content:
|
113 |
-
partial_message += content
|
114 |
-
yield partial_message
|
115 |
except json.JSONDecodeError:
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
else:
|
127 |
-
# theothershits
|
128 |
-
error_message = f"Error: Received status code {response.status_code} - {response.text}"
|
129 |
-
print(error_message)
|
130 |
yield f"An error occurred: {error_message}"
|
131 |
-
|
|
|
132 |
except RequestException as e:
|
133 |
print(f"Request error: {e}")
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
def import_chat(custom_format_string):
|
138 |
try:
|
|
|
3 |
import json
|
4 |
import os
|
5 |
import datetime
|
6 |
+
import time
|
7 |
from requests.exceptions import RequestException
|
8 |
|
9 |
api_keys_env = os.environ.get('API_KEYS')
|
|
|
11 |
API_KEYS = [key.strip() for key in api_keys_env.strip().split('\n') if key.strip()]
|
12 |
else:
|
13 |
raise ValueError("all keez ded go kys")
|
14 |
+
|
15 |
API_URL = os.environ.get('API_URL')
|
16 |
|
17 |
DEFAULT_PARAMS = {
|
|
|
81 |
|
82 |
api_key_index = 0
|
83 |
retries = 0
|
84 |
+
max_retries = len(API_KEYS) * 5
|
85 |
+
partial_message = ""
|
86 |
+
processing_count = 0
|
87 |
+
max_processing_attempts = 10
|
88 |
|
89 |
while retries < max_retries:
|
90 |
+
if stop_flag[0]:
|
91 |
+
return
|
92 |
|
93 |
+
current_api_key = API_KEYS[api_key_index]
|
94 |
headers = {
|
95 |
"Authorization": f"Bearer {current_api_key}",
|
96 |
"Content-Type": "application/json"
|
97 |
}
|
98 |
|
99 |
try:
|
100 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
|
101 |
+
|
102 |
+
if response.status_code == 200:
|
103 |
+
processing_count = 0
|
104 |
+
for line in response.iter_lines():
|
105 |
+
if stop_flag[0]:
|
106 |
+
response.close()
|
107 |
+
return
|
108 |
+
|
109 |
+
if not line:
|
110 |
+
continue
|
111 |
+
|
112 |
+
line = line.decode('utf-8')
|
113 |
+
if RESPONSE_LOGGING_ENABLED:
|
114 |
+
print(f"API Response: {line}")
|
115 |
+
|
116 |
+
if ": OPENROUTER PROCESSING" in line:
|
117 |
+
processing_count += 1
|
118 |
+
if processing_count >= max_processing_attempts:
|
119 |
+
print("Too many processing attempts, cycling to next key...")
|
120 |
break
|
121 |
+
continue
|
122 |
+
|
123 |
+
if not line.startswith("data: "):
|
124 |
+
continue
|
125 |
+
|
126 |
+
if line.strip() == "data: [DONE]":
|
127 |
+
response.close()
|
128 |
+
if partial_message:
|
129 |
+
yield partial_message
|
130 |
+
return
|
131 |
+
|
132 |
+
try:
|
133 |
+
json_data = json.loads(line[6:])
|
134 |
+
|
135 |
+
# Check for rate limit error
|
136 |
+
if 'error' in json_data:
|
137 |
+
error_msg = json_data.get('error', {}).get('message', '')
|
138 |
+
if isinstance(error_msg, str):
|
139 |
try:
|
140 |
+
error_obj = json.loads(error_msg)
|
141 |
+
if error_obj.get('error', {}).get('type') == 'rate_limit_exceeded':
|
142 |
+
print("Rate limit hit in streaming response, cycling keys...")
|
143 |
+
break
|
|
|
|
|
|
|
144 |
except json.JSONDecodeError:
|
145 |
+
pass
|
146 |
+
continue
|
147 |
+
|
148 |
+
if 'choices' in json_data and json_data['choices']:
|
149 |
+
delta = json_data['choices'][0]['delta']
|
150 |
+
content = delta.get('content', '')
|
151 |
+
if content:
|
152 |
+
partial_message += content
|
153 |
+
yield partial_message
|
154 |
+
|
155 |
+
except json.JSONDecodeError as e:
|
156 |
+
print(f"JSON decode error: {e}")
|
157 |
+
continue
|
158 |
+
|
159 |
+
response.close()
|
160 |
+
if partial_message:
|
161 |
+
return
|
162 |
+
|
163 |
+
elif response.status_code == 429:
|
164 |
+
print("Rate limit hit from status code, cycling keys...")
|
165 |
+
time.sleep(1)
|
166 |
+
elif response.status_code == 401:
|
167 |
+
print(f"Invalid API key {api_key_index}, cycling to next...")
|
168 |
+
api_key_index = (api_key_index + 1) % len(API_KEYS)
|
169 |
+
retries += 1
|
170 |
+
continue
|
171 |
+
else:
|
172 |
+
error_message = f"Error: Received status code {response.status_code} - {response.text}"
|
173 |
+
print(error_message)
|
174 |
+
if partial_message:
|
175 |
+
yield partial_message + f"\n[Error: {error_message}]"
|
176 |
else:
|
|
|
|
|
|
|
177 |
yield f"An error occurred: {error_message}"
|
178 |
+
return
|
179 |
+
|
180 |
except RequestException as e:
|
181 |
print(f"Request error: {e}")
|
182 |
+
if partial_message:
|
183 |
+
yield partial_message + f"\n[Error: {str(e)}]"
|
184 |
+
else:
|
185 |
+
yield f"An error occurred: {str(e)}"
|
186 |
+
return
|
187 |
+
|
188 |
+
retries += 1
|
189 |
+
api_key_index = (api_key_index + 1) % len(API_KEYS)
|
190 |
+
time.sleep(1)
|
191 |
+
|
192 |
+
if partial_message:
|
193 |
+
yield partial_message + "\n[Error: Maximum retries reached]"
|
194 |
+
else:
|
195 |
+
yield "Error: Maximum retries reached. Please try again later."
|
196 |
|
197 |
def import_chat(custom_format_string):
|
198 |
try:
|