Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,21 @@ chat_sessions_cache = {}
|
|
| 21 |
# Store uploaded files metadata
|
| 22 |
files_cache = {}
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def get_headers():
|
| 26 |
"""Get authorization headers"""
|
|
@@ -165,16 +180,21 @@ def extract_files_from_messages(messages, msg_format="openai"):
|
|
| 165 |
if block_type == 'image_url':
|
| 166 |
image_url_obj = block.get('image_url', {})
|
| 167 |
url = image_url_obj.get('url', '')
|
|
|
|
| 168 |
|
| 169 |
# Check if it's a file ID or our own proxy URL
|
| 170 |
file_id = get_id_from_url(url)
|
|
|
|
| 171 |
if file_id:
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
files.append({
|
| 174 |
"base64_data": record.get('_data', ''),
|
| 175 |
"media_type": record.get('content_type', 'image/png'),
|
| 176 |
"filename": record.get('filename', f"file_{uuid.uuid4().hex[:8]}"),
|
| 177 |
"onyx_file_id": record.get('onyx_file_id'),
|
|
|
|
| 178 |
"type": "image"
|
| 179 |
})
|
| 180 |
|
|
@@ -316,6 +336,7 @@ def upload_file_to_onyx(file_data, chat_session_id):
|
|
| 316 |
"id": file_data['onyx_file_id'],
|
| 317 |
"type": file_data.get('type', 'document'),
|
| 318 |
"name": file_data.get('filename', 'file'),
|
|
|
|
| 319 |
}
|
| 320 |
|
| 321 |
# Decode base64 to binary
|
|
@@ -367,6 +388,7 @@ def upload_file_to_onyx(file_data, chat_session_id):
|
|
| 367 |
"id": f_id,
|
| 368 |
"type": file_data.get('type', 'document'),
|
| 369 |
"name": file_data['filename'],
|
|
|
|
| 370 |
}
|
| 371 |
|
| 372 |
# If the response includes the full descriptor, use it
|
|
@@ -748,8 +770,19 @@ def build_onyx_payload(messages, model_provider, model_version, temperature, cha
|
|
| 748 |
# If files were uploaded, add a note to the message
|
| 749 |
file_note = ""
|
| 750 |
if file_descriptors:
|
| 751 |
-
|
| 752 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
|
| 754 |
|
| 755 |
# Construct the full message
|
|
@@ -842,8 +875,19 @@ def build_anthropic_payload_from_messages(messages, system_prompt, model_provide
|
|
| 842 |
# If files were uploaded, add a note
|
| 843 |
file_note = ""
|
| 844 |
if file_descriptors:
|
| 845 |
-
|
| 846 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 847 |
|
| 848 |
|
| 849 |
# Construct full message
|
|
@@ -1002,7 +1046,7 @@ def stream_onyx_response(payload, model, session_key, has_tools=False):
|
|
| 1002 |
try:
|
| 1003 |
print(f"Trying endpoint: {url}")
|
| 1004 |
|
| 1005 |
-
with requests.post(url, json=payload, headers=get_headers(), stream=True, timeout=
|
| 1006 |
print(f"Response status: {response.status_code}")
|
| 1007 |
|
| 1008 |
if response.status_code != 200:
|
|
@@ -1105,7 +1149,7 @@ def collect_full_response(payload, model, session_key, has_tools=False):
|
|
| 1105 |
|
| 1106 |
is_streaming_request = payload.get('stream', False)
|
| 1107 |
|
| 1108 |
-
with requests.post(url, json=payload, headers=get_headers(), stream=is_streaming_request, timeout=
|
| 1109 |
print(f"Response status: {response.status_code}")
|
| 1110 |
|
| 1111 |
if response.status_code == 404:
|
|
@@ -1263,7 +1307,7 @@ def generate_anthropic_stream_events(payload, model, session_key, has_tools=Fals
|
|
| 1263 |
|
| 1264 |
for url in endpoints:
|
| 1265 |
try:
|
| 1266 |
-
with requests.post(url, json=payload, headers=get_headers(), stream=True, timeout=
|
| 1267 |
if response.status_code != 200:
|
| 1268 |
continue
|
| 1269 |
|
|
@@ -1416,7 +1460,7 @@ def collect_anthropic_full_response(payload, model, session_key, has_tools=False
|
|
| 1416 |
try:
|
| 1417 |
is_streaming_request = payload.get('stream', False)
|
| 1418 |
|
| 1419 |
-
with requests.post(url, json=payload, headers=get_headers(), stream=is_streaming_request, timeout=
|
| 1420 |
if response.status_code == 404:
|
| 1421 |
continue
|
| 1422 |
|
|
|
|
| 21 |
# Store uploaded files metadata
|
| 22 |
files_cache = {}
|
| 23 |
|
| 24 |
+
def cleanup_expired_files():
|
| 25 |
+
"""Remove files from cache that are older than 5 minutes"""
|
| 26 |
+
current_time = int(time.time())
|
| 27 |
+
expiry_seconds = 5 * 60
|
| 28 |
+
expired_ids = [fid for fid, record in files_cache.items()
|
| 29 |
+
if current_time - record.get('created_at', record.get('uploaded_at', 0)) > expiry_seconds]
|
| 30 |
+
|
| 31 |
+
for fid in expired_ids:
|
| 32 |
+
print(f"Cleaning up expired file: {fid}")
|
| 33 |
+
del files_cache[fid]
|
| 34 |
+
|
| 35 |
+
@app.before_request
|
| 36 |
+
def run_cleanup():
|
| 37 |
+
cleanup_expired_files()
|
| 38 |
+
|
| 39 |
|
| 40 |
def get_headers():
|
| 41 |
"""Get authorization headers"""
|
|
|
|
| 180 |
if block_type == 'image_url':
|
| 181 |
image_url_obj = block.get('image_url', {})
|
| 182 |
url = image_url_obj.get('url', '')
|
| 183 |
+
print(f"[DEBUG] Found image_url block with URL: {url}")
|
| 184 |
|
| 185 |
# Check if it's a file ID or our own proxy URL
|
| 186 |
file_id = get_id_from_url(url)
|
| 187 |
+
print(f"[DEBUG] Extracted file_id: {file_id}")
|
| 188 |
if file_id:
|
| 189 |
+
print(f"[DEBUG] File ID found in cache: {file_id in files_cache}")
|
| 190 |
+
if file_id in files_cache:
|
| 191 |
+
record = files_cache[file_id]
|
| 192 |
files.append({
|
| 193 |
"base64_data": record.get('_data', ''),
|
| 194 |
"media_type": record.get('content_type', 'image/png'),
|
| 195 |
"filename": record.get('filename', f"file_{uuid.uuid4().hex[:8]}"),
|
| 196 |
"onyx_file_id": record.get('onyx_file_id'),
|
| 197 |
+
"proxy_id": file_id,
|
| 198 |
"type": "image"
|
| 199 |
})
|
| 200 |
|
|
|
|
| 336 |
"id": file_data['onyx_file_id'],
|
| 337 |
"type": file_data.get('type', 'document'),
|
| 338 |
"name": file_data.get('filename', 'file'),
|
| 339 |
+
"proxy_id": file_data.get('proxy_id'),
|
| 340 |
}
|
| 341 |
|
| 342 |
# Decode base64 to binary
|
|
|
|
| 388 |
"id": f_id,
|
| 389 |
"type": file_data.get('type', 'document'),
|
| 390 |
"name": file_data['filename'],
|
| 391 |
+
"proxy_id": file_data.get('proxy_id'),
|
| 392 |
}
|
| 393 |
|
| 394 |
# If the response includes the full descriptor, use it
|
|
|
|
| 770 |
# If files were uploaded, add a note to the message
|
| 771 |
file_note = ""
|
| 772 |
if file_descriptors:
|
| 773 |
+
file_info = []
|
| 774 |
+
host_url = request.host_url.rstrip('/')
|
| 775 |
+
for f in file_descriptors:
|
| 776 |
+
name = f.get('name', 'file')
|
| 777 |
+
proxy_id = f.get('proxy_id')
|
| 778 |
+
if proxy_id:
|
| 779 |
+
url = f"{host_url}/v1/files/{proxy_id}/content"
|
| 780 |
+
file_info.append(f"{name} ({url})")
|
| 781 |
+
else:
|
| 782 |
+
file_info.append(name)
|
| 783 |
+
|
| 784 |
+
file_names_str = ", ".join(file_info)
|
| 785 |
+
file_note = f"\n[Note: {len(file_descriptors)} file(s) ({file_names_str}) have been attached to this message. These links expire in 5 minutes. Please analyze them as requested.]\n"
|
| 786 |
|
| 787 |
|
| 788 |
# Construct the full message
|
|
|
|
| 875 |
# If files were uploaded, add a note
|
| 876 |
file_note = ""
|
| 877 |
if file_descriptors:
|
| 878 |
+
file_info = []
|
| 879 |
+
host_url = request.host_url.rstrip('/')
|
| 880 |
+
for f in file_descriptors:
|
| 881 |
+
name = f.get('name', 'file')
|
| 882 |
+
proxy_id = f.get('proxy_id')
|
| 883 |
+
if proxy_id:
|
| 884 |
+
url = f"{host_url}/v1/files/{proxy_id}/content"
|
| 885 |
+
file_info.append(f"{name} ({url})")
|
| 886 |
+
else:
|
| 887 |
+
file_info.append(name)
|
| 888 |
+
|
| 889 |
+
file_names_str = ", ".join(file_info)
|
| 890 |
+
file_note = f"\n[Note: {len(file_descriptors)} file(s) ({file_names_str}) have been attached to this message. These links expire in 5 minutes. Please analyze them as requested.]\n"
|
| 891 |
|
| 892 |
|
| 893 |
# Construct full message
|
|
|
|
| 1046 |
try:
|
| 1047 |
print(f"Trying endpoint: {url}")
|
| 1048 |
|
| 1049 |
+
with requests.post(url, json=payload, headers=get_headers(), stream=True, timeout=300) as response:
|
| 1050 |
print(f"Response status: {response.status_code}")
|
| 1051 |
|
| 1052 |
if response.status_code != 200:
|
|
|
|
| 1149 |
|
| 1150 |
is_streaming_request = payload.get('stream', False)
|
| 1151 |
|
| 1152 |
+
with requests.post(url, json=payload, headers=get_headers(), stream=is_streaming_request, timeout=300) as response:
|
| 1153 |
print(f"Response status: {response.status_code}")
|
| 1154 |
|
| 1155 |
if response.status_code == 404:
|
|
|
|
| 1307 |
|
| 1308 |
for url in endpoints:
|
| 1309 |
try:
|
| 1310 |
+
with requests.post(url, json=payload, headers=get_headers(), stream=True, timeout=300) as response:
|
| 1311 |
if response.status_code != 200:
|
| 1312 |
continue
|
| 1313 |
|
|
|
|
| 1460 |
try:
|
| 1461 |
is_streaming_request = payload.get('stream', False)
|
| 1462 |
|
| 1463 |
+
with requests.post(url, json=payload, headers=get_headers(), stream=is_streaming_request, timeout=300) as response:
|
| 1464 |
if response.status_code == 404:
|
| 1465 |
continue
|
| 1466 |
|