mirror of
https://github.com/Zippland/Snap-Solver.git
synced 2026-02-22 08:28:15 +08:00
优化WebSocket连接和图像处理的稳定性
This commit is contained in:
26
app.py
26
app.py
@@ -10,7 +10,7 @@ from PIL import Image, ImageDraw
|
||||
import pyperclip
|
||||
from models import ModelFactory
|
||||
app = Flask(__name__)
|
||||
socketio = SocketIO(app, cors_allowed_origins="*")
|
||||
socketio = SocketIO(app, cors_allowed_origins="*", ping_timeout=30, ping_interval=5, max_http_buffer_size=50 * 1024 * 1024)
|
||||
|
||||
# Commented out due to model file issues
|
||||
# from pix2text import Pix2Text
|
||||
@@ -188,6 +188,11 @@ def handle_text_extraction(data):
|
||||
image_data = data['image']
|
||||
if not isinstance(image_data, str):
|
||||
raise ValueError("Invalid image data format")
|
||||
|
||||
# 检查图像大小,避免处理过大的图像导致断开连接
|
||||
image_size_bytes = len(image_data) * 3 / 4 # 估算base64的实际大小
|
||||
if image_size_bytes > 10 * 1024 * 1024: # 10MB
|
||||
raise ValueError("Image too large, please crop to a smaller area")
|
||||
|
||||
settings = data.get('settings', {})
|
||||
if not isinstance(settings, dict):
|
||||
@@ -197,6 +202,13 @@ def handle_text_extraction(data):
|
||||
if not mathpix_key:
|
||||
raise ValueError("Mathpix API key is required")
|
||||
|
||||
# 先回复客户端,确认已收到请求,防止超时断开
|
||||
# 注意:这里不能使用return,否则后续代码不会执行
|
||||
socketio.emit('request_acknowledged', {
|
||||
'status': 'received',
|
||||
'message': 'Image received, text extraction in progress'
|
||||
}, room=request.sid)
|
||||
|
||||
try:
|
||||
app_id, app_key = mathpix_key.split(':')
|
||||
if not app_id.strip() or not app_key.strip():
|
||||
@@ -298,6 +310,11 @@ def handle_analyze_image(data):
|
||||
if not image_data:
|
||||
raise ValueError("No image data provided")
|
||||
|
||||
# 检查图像大小,避免处理过大的图像导致断开连接
|
||||
image_size_bytes = len(image_data) * 3 / 4 # 估算base64的实际大小
|
||||
if image_size_bytes > 10 * 1024 * 1024: # 10MB
|
||||
raise ValueError("Image too large, please crop to a smaller area or use text extraction")
|
||||
|
||||
settings = data.get('settings', {})
|
||||
|
||||
# 不需要分割了,因为前端已经做了分割
|
||||
@@ -327,6 +344,13 @@ def handle_analyze_image(data):
|
||||
}
|
||||
|
||||
try:
|
||||
# 先回复客户端,确认已收到请求,防止超时断开
|
||||
# 注意:这里不能使用return,否则后续代码不会执行
|
||||
socketio.emit('request_acknowledged', {
|
||||
'status': 'received',
|
||||
'message': 'Image received, analysis in progress'
|
||||
}, room=request.sid)
|
||||
|
||||
# Create model instance using factory
|
||||
model = ModelFactory.create_model(
|
||||
model_name=model_name,
|
||||
|
||||
Reference in New Issue
Block a user