Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import base64
|
|
|
|
| 4 |
import random
|
| 5 |
import json
|
| 6 |
import requests
|
|
@@ -11,6 +12,7 @@ from huggingface_hub import InferenceClient
|
|
| 11 |
from zoneinfo import ZoneInfo
|
| 12 |
import re
|
| 13 |
from playwright.sync_api import sync_playwright
|
|
|
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
|
@@ -320,6 +322,33 @@ def serpapi_search(query: str, location=None, num_results=15):
|
|
| 320 |
print(f"[SEARCH] ❌ Error: {e}")
|
| 321 |
return f"Unable to find results for: {query}"
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
# =======================================
|
| 324 |
# 💬 Streaming Chat (with API Key Fallback)
|
| 325 |
# =======================================
|
|
@@ -735,6 +764,7 @@ def chat():
|
|
| 735 |
base64.b64decode(test_b64, validate=True)
|
| 736 |
except Exception:
|
| 737 |
return Response("Invalid base64 image", mimetype="text/plain", status=400)
|
|
|
|
| 738 |
cohere_url = "https://api.cohere.ai/v2/chat"
|
| 739 |
payload = {
|
| 740 |
"model": "c4ai-aya-vision-32b",
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import base64
|
| 4 |
+
import io
|
| 5 |
import random
|
| 6 |
import json
|
| 7 |
import requests
|
|
|
|
| 12 |
from zoneinfo import ZoneInfo
|
| 13 |
import re
|
| 14 |
from playwright.sync_api import sync_playwright
|
| 15 |
+
from PIL import Image
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
| 18 |
|
|
|
|
| 322 |
print(f"[SEARCH] ❌ Error: {e}")
|
| 323 |
return f"Unable to find results for: {query}"
|
| 324 |
|
| 325 |
+
def adaptive_compress_base64_image(image_base64, max_size=1_000_000):
|
| 326 |
+
header = ""
|
| 327 |
+
if image_base64.startswith("data:"):
|
| 328 |
+
header, image_base64 = image_base64.split(",", 1)
|
| 329 |
+
header += ","
|
| 330 |
+
|
| 331 |
+
img = Image.open(io.BytesIO(base64.b64decode(image_base64))).convert("RGB")
|
| 332 |
+
|
| 333 |
+
max_dim = 1400
|
| 334 |
+
quality = 85
|
| 335 |
+
|
| 336 |
+
while True:
|
| 337 |
+
tmp = img.copy()
|
| 338 |
+
tmp.thumbnail((max_dim, max_dim))
|
| 339 |
+
|
| 340 |
+
buf = io.BytesIO()
|
| 341 |
+
tmp.save(buf, "JPEG", quality=quality, optimize=True)
|
| 342 |
+
b64 = base64.b64encode(buf.getvalue()).decode()
|
| 343 |
+
|
| 344 |
+
if len(b64) <= max_size or max_dim < 400:
|
| 345 |
+
return header + b64
|
| 346 |
+
|
| 347 |
+
if quality > 40:
|
| 348 |
+
quality -= 10
|
| 349 |
+
else:
|
| 350 |
+
max_dim = int(max_dim * 0.8)
|
| 351 |
+
quality = 85
|
| 352 |
# =======================================
|
| 353 |
# 💬 Streaming Chat (with API Key Fallback)
|
| 354 |
# =======================================
|
|
|
|
| 764 |
base64.b64decode(test_b64, validate=True)
|
| 765 |
except Exception:
|
| 766 |
return Response("Invalid base64 image", mimetype="text/plain", status=400)
|
| 767 |
+
image_base64 = adaptive_compress_base64_image(image_base64)
|
| 768 |
cohere_url = "https://api.cohere.ai/v2/chat"
|
| 769 |
payload = {
|
| 770 |
"model": "c4ai-aya-vision-32b",
|