DocUA commited on
Commit
381cd06
·
1 Parent(s): 3e63a13

Disable HTTP/2 in httpx client for OpenAI and DeepSeek to avoid 421 Misdirected Request errors and ensure consistent connections in HF Spaces

Browse files
Files changed (2) hide show
  1. app.py +12 -3
  2. main.py +11 -6
app.py CHANGED
@@ -83,12 +83,21 @@ def run_network_diagnostics():
83
  try:
84
  import httpx
85
  print(f" httpx version: {httpx.__version__}")
 
86
  with httpx.Client(timeout=10) as client:
87
  resp = client.get("https://api.anthropic.com")
88
- print(f" ✅ httpx -> Anthropic HTTP {resp.status_code}")
89
- with httpx.Client(timeout=10) as client:
 
90
  resp = client.get("https://api.openai.com")
91
- print(f" ✅ httpx -> OpenAI HTTP {resp.status_code}")
 
 
 
 
 
 
 
92
  except Exception as e:
93
  print(f" ❌ httpx -> {type(e).__name__}: {e}")
94
 
 
83
  try:
84
  import httpx
85
  print(f" httpx version: {httpx.__version__}")
86
+ # Test with default settings
87
  with httpx.Client(timeout=10) as client:
88
  resp = client.get("https://api.anthropic.com")
89
+ print(f" ✅ httpx (default) -> Anthropic HTTP {resp.status_code}")
90
+ # Test OpenAI with HTTP/2 disabled (avoids 421 Misdirected Request)
91
+ with httpx.Client(timeout=10, http2=False) as client:
92
  resp = client.get("https://api.openai.com")
93
+ print(f" ✅ httpx (http1.1) -> OpenAI HTTP {resp.status_code}")
94
+ # Also test with default HTTP/2 to compare
95
+ try:
96
+ with httpx.Client(timeout=10) as client:
97
+ resp = client.get("https://api.openai.com")
98
+ print(f" ✅ httpx (default) -> OpenAI HTTP {resp.status_code}")
99
+ except Exception as e2:
100
+ print(f" ⚠️ httpx (default/http2) -> OpenAI FAILED: {type(e2).__name__}: {e2}")
101
  except Exception as e:
102
  print(f" ❌ httpx -> {type(e).__name__}: {e}")
103
 
main.py CHANGED
@@ -279,13 +279,15 @@ class LLMAnalyzer:
279
  if provider == ModelProvider.OPENAI:
280
  if not OPENAI_API_KEY:
281
  raise ValueError(f"OpenAI API key not configured. Please set OPENAI_API_KEY environment variable to use {provider.value} provider.")
282
- # Use default httpx settings for better compatibility with async environments
283
- self.client = openai.OpenAI(api_key=OPENAI_API_KEY, timeout=120.0)
 
284
  elif provider == ModelProvider.DEEPSEEK:
285
  if not DEEPSEEK_API_KEY:
286
  raise ValueError(f"DeepSeek API key not configured. Please set DEEPSEEK_API_KEY environment variable to use {provider.value} provider.")
287
- # Use default httpx settings for better compatibility with async environments
288
- self.client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0)
 
289
  elif provider == ModelProvider.ANTHROPIC:
290
  if not ANTHROPIC_API_KEY:
291
  raise ValueError(f"Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable to use {provider.value} provider.")
@@ -682,12 +684,14 @@ def generate_legal_position(
682
  raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
683
 
684
  if provider == ModelProvider.OPENAI.value:
685
- # Use context-managed httpx client to avoid socket/fd leaks in threaded Gradio env
 
686
  http_client = None
687
  response_text = None
688
  try:
689
  http_client = httpx.Client(
690
  timeout=httpx.Timeout(120.0, connect=30.0),
 
691
  )
692
  client = OpenAI(
693
  api_key=OPENAI_API_KEY,
@@ -779,12 +783,13 @@ def generate_legal_position(
779
  pass
780
 
781
  if provider == ModelProvider.DEEPSEEK.value:
782
- # Use context-managed httpx client for DeepSeek to avoid socket/fd leaks
783
  http_client = None
784
  response_text = None
785
  try:
786
  http_client = httpx.Client(
787
  timeout=httpx.Timeout(120.0, connect=30.0),
 
788
  )
789
  client = OpenAI(
790
  api_key=DEEPSEEK_API_KEY,
 
279
  if provider == ModelProvider.OPENAI:
280
  if not OPENAI_API_KEY:
281
  raise ValueError(f"OpenAI API key not configured. Please set OPENAI_API_KEY environment variable to use {provider.value} provider.")
282
+ # Disable HTTP/2 to avoid 421 Misdirected Request on HF Spaces
283
+ self._http_client = httpx.Client(timeout=httpx.Timeout(120.0, connect=30.0), http2=False)
284
+ self.client = openai.OpenAI(api_key=OPENAI_API_KEY, http_client=self._http_client)
285
  elif provider == ModelProvider.DEEPSEEK:
286
  if not DEEPSEEK_API_KEY:
287
  raise ValueError(f"DeepSeek API key not configured. Please set DEEPSEEK_API_KEY environment variable to use {provider.value} provider.")
288
+ # Disable HTTP/2 for consistent connections
289
+ self._http_client = httpx.Client(timeout=httpx.Timeout(120.0, connect=30.0), http2=False)
290
+ self.client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", http_client=self._http_client)
291
  elif provider == ModelProvider.ANTHROPIC:
292
  if not ANTHROPIC_API_KEY:
293
  raise ValueError(f"Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable to use {provider.value} provider.")
 
684
  raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
685
 
686
  if provider == ModelProvider.OPENAI.value:
687
+ # Use custom httpx client with HTTP/2 disabled to avoid 421 Misdirected Request
688
+ # errors on HF Spaces (Cloudflare HTTP/2 connection coalescing issue)
689
  http_client = None
690
  response_text = None
691
  try:
692
  http_client = httpx.Client(
693
  timeout=httpx.Timeout(120.0, connect=30.0),
694
+ http2=False,
695
  )
696
  client = OpenAI(
697
  api_key=OPENAI_API_KEY,
 
783
  pass
784
 
785
  if provider == ModelProvider.DEEPSEEK.value:
786
+ # Use custom httpx client with HTTP/2 disabled for consistent connections
787
  http_client = None
788
  response_text = None
789
  try:
790
  http_client = httpx.Client(
791
  timeout=httpx.Timeout(120.0, connect=30.0),
792
+ http2=False,
793
  )
794
  client = OpenAI(
795
  api_key=DEEPSEEK_API_KEY,