refactor: centralize DIFY settings in config.py and cleanup env files
- Update config.py to read both .env and .env.local (with .env.local priority) - Move DIFY API settings from hardcoded values to environment configuration - Remove unused PADDLEOCR_MODEL_DIR setting (models stored in ~/.paddleocr/) - Remove deprecated argostranslate translation settings - Add DIFY settings: base_url, api_key, timeout, max_retries, batch limits - Update dify_client.py to use settings from config.py - Update translation_service.py to use settings instead of constants - Fix frontend env files to use correct variable name VITE_API_BASE_URL - Update setup_dev_env.sh with correct PaddlePaddle version (3.2.0) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,6 @@ Tool_OCR - DIFY AI Client
|
||||
HTTP client for DIFY translation API with batch support
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
@@ -12,20 +11,10 @@ from typing import Dict, List, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# DIFY API Configuration
|
||||
DIFY_BASE_URL = "https://dify.theaken.com/v1"
|
||||
DIFY_API_KEY = "app-YOPrF2ro5fshzMkCZviIuUJd"
|
||||
DIFY_TIMEOUT = 120.0 # seconds (increased for batch)
|
||||
DIFY_MAX_RETRIES = 3
|
||||
|
||||
# Batch translation limits
|
||||
# Conservative limits to avoid gateway timeouts
|
||||
# DIFY server may have processing time limits
|
||||
MAX_BATCH_CHARS = 5000
|
||||
MAX_BATCH_ITEMS = 20
|
||||
|
||||
# Language name mapping
|
||||
LANGUAGE_NAMES = {
|
||||
"en": "English",
|
||||
@@ -77,22 +66,39 @@ class DifyClient:
|
||||
- Blocking mode API calls
|
||||
- Automatic retry with exponential backoff
|
||||
- Token and latency tracking
|
||||
|
||||
Configuration is loaded from settings (config.py / .env.local):
|
||||
- DIFY_BASE_URL: API base URL
|
||||
- DIFY_API_KEY: API key (required)
|
||||
- DIFY_TIMEOUT: Request timeout in seconds
|
||||
- DIFY_MAX_RETRIES: Max retry attempts
|
||||
- DIFY_MAX_BATCH_CHARS: Max characters per batch
|
||||
- DIFY_MAX_BATCH_ITEMS: Max items per batch
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url: str = DIFY_BASE_URL,
|
||||
api_key: str = DIFY_API_KEY,
|
||||
timeout: float = DIFY_TIMEOUT,
|
||||
max_retries: int = DIFY_MAX_RETRIES
|
||||
base_url: Optional[str] = None,
|
||||
api_key: Optional[str] = None,
|
||||
timeout: Optional[float] = None,
|
||||
max_retries: Optional[int] = None
|
||||
):
|
||||
self.base_url = base_url
|
||||
self.api_key = api_key
|
||||
self.timeout = timeout
|
||||
self.max_retries = max_retries
|
||||
# Use settings as defaults when not explicitly provided
|
||||
self.base_url = base_url or settings.dify_base_url
|
||||
self.api_key = api_key or settings.dify_api_key
|
||||
self.timeout = timeout if timeout is not None else settings.dify_timeout
|
||||
self.max_retries = max_retries if max_retries is not None else settings.dify_max_retries
|
||||
self.max_batch_chars = settings.dify_max_batch_chars
|
||||
self.max_batch_items = settings.dify_max_batch_items
|
||||
self._total_tokens = 0
|
||||
self._total_requests = 0
|
||||
|
||||
# Warn if API key is not configured
|
||||
if not self.api_key:
|
||||
logger.warning(
|
||||
"DIFY_API_KEY not configured. Set DIFY_API_KEY in .env.local for translation to work."
|
||||
)
|
||||
|
||||
def _get_language_name(self, lang_code: str) -> str:
|
||||
"""Convert language code to full name for prompt"""
|
||||
return LANGUAGE_NAMES.get(lang_code, lang_code)
|
||||
|
||||
Reference in New Issue
Block a user