DocUA commited on
Commit
60a364a
·
1 Parent(s): 71df805

Fix OpenAI/DeepSeek HF Spaces connectivity: use custom httpx.Client to handle asyncio conflicts

Browse files
Files changed (1) hide show
  1. main.py +23 -7
main.py CHANGED
@@ -3,6 +3,7 @@ import sys
3
  import json
4
  import time
5
  import boto3
 
6
  from pathlib import Path
7
  from typing import Dict, List, Optional, Tuple, Any
8
  from anthropic import Anthropic
@@ -681,8 +682,16 @@ def generate_legal_position(
681
  raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
682
 
683
  if provider == ModelProvider.OPENAI.value:
684
- # Use default OpenAI client settings for async compatibility in HF Spaces
685
- client = OpenAI(api_key=OPENAI_API_KEY, timeout=120.0)
 
 
 
 
 
 
 
 
686
 
687
  # Retry logic for connection errors
688
  max_retries = 3
@@ -714,13 +723,12 @@ def generate_legal_position(
714
  completion_params["max_completion_tokens"] = MAX_TOKENS_CONFIG["openai"]
715
  else:
716
  completion_params["max_tokens"] = MAX_TOKENS_CONFIG["openai"]
717
-
 
718
  # Handle thinking/reasoning
719
  if thinking_enabled and is_reasoning_model:
720
  completion_params["reasoning_effort"] = thinking_level.lower()
721
  # Reasoning models usually don't support temperature or it must be 1.0
722
- else:
723
- completion_params["temperature"] = GENERATION_TEMPERATURE
724
 
725
  # Execute with retries
726
  for attempt in range(max_retries):
@@ -761,8 +769,16 @@ def generate_legal_position(
761
  }
762
 
763
  if provider == ModelProvider.DEEPSEEK.value:
764
- # Use default client settings for async compatibility in HF Spaces
765
- client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0)
 
 
 
 
 
 
 
 
766
 
767
  # Retry logic for DeepSeek
768
  max_retries = 3
 
3
  import json
4
  import time
5
  import boto3
6
+ import httpx
7
  from pathlib import Path
8
  from typing import Dict, List, Optional, Tuple, Any
9
  from anthropic import Anthropic
 
682
  raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
683
 
684
  if provider == ModelProvider.OPENAI.value:
685
+ # Use custom httpx client to avoid async loop issues on HF Spaces
686
+ # This is critical for stable connection in threaded environments like Gradio
687
+ http_client = httpx.Client(
688
+ timeout=120.0,
689
+ transport=httpx.HTTPTransport(retries=3)
690
+ )
691
+ client = OpenAI(
692
+ api_key=OPENAI_API_KEY,
693
+ http_client=http_client
694
+ )
695
 
696
  # Retry logic for connection errors
697
  max_retries = 3
 
723
  completion_params["max_completion_tokens"] = MAX_TOKENS_CONFIG["openai"]
724
  else:
725
  completion_params["max_tokens"] = MAX_TOKENS_CONFIG["openai"]
726
+ completion_params["temperature"] = GENERATION_TEMPERATURE
727
+
728
  # Handle thinking/reasoning
729
  if thinking_enabled and is_reasoning_model:
730
  completion_params["reasoning_effort"] = thinking_level.lower()
731
  # Reasoning models usually don't support temperature or it must be 1.0
 
 
732
 
733
  # Execute with retries
734
  for attempt in range(max_retries):
 
769
  }
770
 
771
  if provider == ModelProvider.DEEPSEEK.value:
772
+ # Use custom httpx client for DeepSeek on HF Spaces
773
+ http_client = httpx.Client(
774
+ timeout=120.0,
775
+ transport=httpx.HTTPTransport(retries=3)
776
+ )
777
+ client = OpenAI(
778
+ api_key=DEEPSEEK_API_KEY,
779
+ base_url="https://api.deepseek.com",
780
+ http_client=http_client
781
+ )
782
 
783
  # Retry logic for DeepSeek
784
  max_retries = 3