Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -188,26 +188,6 @@ def verify_admin_password(password):
|
|
| 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:
|
| 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"""
|
|
@@ -909,17 +889,8 @@ def chat_completions():
|
|
| 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 =
|
| 923 |
|
| 924 |
# Replace model IDs in the response to match the original request
|
| 925 |
if original_model == 'gpt-5-chat':
|
|
|
|
| 188 |
"""Verify admin password"""
|
| 189 |
return password == ADMIN_PASSWORD
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
def export_database():
|
| 193 |
"""Export current database to JSON"""
|
|
|
|
| 889 |
if chunk:
|
| 890 |
chunk_str = chunk.decode('utf-8', errors='replace')
|
| 891 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 892 |
# Replace the chat completion ID with our UUID
|
| 893 |
+
chunk_str = re.sub(r'"id":"chatcmpl-[^"]*"', f'"id":"{completion_id}"', chunk_str)
|
| 894 |
|
| 895 |
# Replace model IDs in the response to match the original request
|
| 896 |
if original_model == 'gpt-5-chat':
|