GamerC0der commited on
Commit
10faf95
·
verified ·
1 Parent(s): 2e6744a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py CHANGED
@@ -10,6 +10,8 @@ from datetime import datetime, timedelta
10
  import threading
11
  import atexit
12
  import uuid
 
 
13
  from collections import defaultdict
14
 
15
  app = Flask(__name__)
@@ -186,6 +188,27 @@ def verify_admin_password(password):
186
  """Verify admin password"""
187
  return password == ADMIN_PASSWORD
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  def export_database():
190
  """Export current database to JSON"""
191
  try:
@@ -886,6 +909,15 @@ def chat_completions():
886
  if chunk:
887
  chunk_str = chunk.decode('utf-8', errors='replace')
888
 
 
 
 
 
 
 
 
 
 
889
  # Replace the chat completion ID with our UUID
890
  chunk_str = chunk_str.replace('"id":"chatcmpl-', f'"id":"{completion_id}"')
891
 
 
10
  import threading
11
  import atexit
12
  import uuid
13
+ import json
14
+ import re
15
  from collections import defaultdict
16
 
17
  app = Flask(__name__)
 
188
  """Verify admin password"""
189
  return password == ADMIN_PASSWORD
190
 
191
+ def fix_malformed_json(json_str):
192
+ """Fix the malformed JSON from upstream server"""
193
+ try:x
194
+ # The upstream sends: {"choices":[{"delta":{"content":"text"},"index":0}],"created":123,"id":"chatcmpl-abc"123,"model":"gpt-5-nano","object":"chat.completion.chunk"}
195
+ # We need to fix it to: {"choices":[{"delta":{"content":"text"},"index":0}],"created":123,"id":"chatcmpl-abc","model":"gpt-5-nano","object":"chat.completion.chunk"}
196
+
197
+ # Find the pattern where id value is immediately followed by another value without comma
198
+ pattern = r'"id":"([^"]*)"(\d+)'
199
+ match = re.search(pattern, json_str)
200
+
201
+ if match:
202
+ id_value = match.group(1)
203
+ timestamp = match.group(2)
204
+ # Replace the malformed part with proper comma separation
205
+ fixed = re.sub(pattern, f'"id":"{id_value}","created":{timestamp}', json_str)
206
+ return fixed
207
+
208
+ return json_str
209
+ except:
210
+ return json_str
211
+
212
  def export_database():
213
  """Export current database to JSON"""
214
  try:
 
909
  if chunk:
910
  chunk_str = chunk.decode('utf-8', errors='replace')
911
 
912
+ # Fix malformed JSON from upstream server
913
+ if 'data: {' in chunk_str:
914
+ # Extract JSON part
915
+ json_start = chunk_str.find('data: {')
916
+ if json_start != -1:
917
+ json_part = chunk_str[json_start + 6:] # Remove 'data: ' prefix
918
+ fixed_json = fix_malformed_json(json_part)
919
+ chunk_str = f"data: {fixed_json}"
920
+
921
  # Replace the chat completion ID with our UUID
922
  chunk_str = chunk_str.replace('"id":"chatcmpl-', f'"id":"{completion_id}"')
923