Spaces:
Paused
Paused
Fix OpenAI/DeepSeek HF Spaces connectivity: use custom httpx.Client to handle asyncio conflicts
Browse files2 unmodified lines345678672 unmodified lines68168268368468568668768825 unmodified lines71471571671771871972072172272372472572634 unmodified lines7617627637647657667677682 unmodified linesimport jsonimport timeimport boto3from pathlib import Pathfrom typing import Dict, List, Optional, Tuple, Anyfrom anthropic import Anthropic672 unmodified linesraise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")if provider == ModelProvider.OPENAI.value:# Use default OpenAI client settings for async compatibility in HF Spacesclient = OpenAI(api_key=OPENAI_API_KEY, timeout=120.0)# Retry logic for connection errorsmax_retries = 325 unmodified linescompletion_params["max_completion_tokens"] = MAX_TOKENS_CONFIG["openai"]else:completion_params["max_tokens"] = MAX_TOKENS_CONFIG["openai"]# Handle thinking/reasoningif thinking_enabled and is_reasoning_model:completion_params["reasoning_effort"] = thinking_level.lower()# Reasoning models usually don't support temperature or it must be 1.0else:completion_params["temperature"] = GENERATION_TEMPERATURE# Execute with retriesfor attempt in range(max_retries):34 unmodified lines}if provider == ModelProvider.DEEPSEEK.value:# Use default client settings for async compatibility in HF Spacesclient = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0)# Retry logic for DeepSeekmax_retries = 32 unmodified lines3456789672 unmodified lines68268368468568668768868969069169269369469569669725 unmodified lines72372472572672772872973073173273373434 unmodified lines7697707717727737747757767777787797807817827837842 unmodified linesimport jsonimport timeimport boto3import httpxfrom pathlib import Pathfrom typing import Dict, List, Optional, Tuple, Anyfrom anthropic import Anthropic672 unmodified linesraise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")if provider == ModelProvider.OPENAI.value:# Use custom httpx client to avoid async loop issues on HF Spaces# This is critical for stable connection in threaded environments like Gradiohttp_client = httpx.Client(timeout=120.0,transport=httpx.HTTPTransport(retries=3))client = OpenAI(api_key=OPENAI_API_KEY,http_client=http_client)# Retry logic for connection errorsmax_retries = 325 unmodified linescompletion_params["max_completion_tokens"] = MAX_TOKENS_CONFIG["openai"]else:completion_params["max_tokens"] = MAX_TOKENS_CONFIG["openai"]completion_params["temperature"] = GENERATION_TEMPERATURE# Handle thinking/reasoningif thinking_enabled and is_reasoning_model:completion_params["reasoning_effort"] = thinking_level.lower()# Reasoning models usually don't support temperature or it must be 1.0# Execute with retriesfor attempt in range(max_retries):34 unmodified lines}if provider == ModelProvider.DEEPSEEK.value:# Use custom httpx client for DeepSeek on HF Spaceshttp_client = httpx.Client(timeout=120.0,transport=httpx.HTTPTransport(retries=3))client = OpenAI(api_key=DEEPSEEK_API_KEY,base_url="https://api.deepseek.com",http_client=http_client)# Retry logic for DeepSeekmax_retries = 3
main.py+23-7