Omartificial-Intelligence-Space commited on
Commit
914c532
·
verified ·
1 Parent(s): bab8b3a

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +13 -2
utils.py CHANGED
@@ -17,8 +17,9 @@ from html import escape
17
 
18
  # === Env & Clients ===
19
  load_dotenv()
20
- openai_api_key = os.getenv("OPENAI_API_KEY")
21
- anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
 
22
 
23
  # Both clients read keys from env by default; explicit is also fine:
24
  openai_client = OpenAI(api_key=openai_api_key) if openai_api_key else OpenAI()
@@ -28,6 +29,9 @@ anthropic_client = Anthropic(api_key=anthropic_api_key) if anthropic_api_key els
28
  def get_response(model: str, prompt: str) -> str:
29
  """Get response from LLM (OpenAI or Anthropic)."""
30
  if "claude" in model.lower() or "anthropic" in model.lower():
 
 
 
31
  # Anthropic Claude format
32
  message = anthropic_client.messages.create(
33
  model=model,
@@ -36,6 +40,9 @@ def get_response(model: str, prompt: str) -> str:
36
  )
37
  return message.content[0].text
38
  else:
 
 
 
39
  # Default to OpenAI format for all other models (gpt-4, o3-mini, o1, etc.)
40
  response = openai_client.responses.create(
41
  model=model,
@@ -87,6 +94,8 @@ def image_anthropic_call(model_name: str, prompt: str, media_type: str, b64: str
87
  Call Anthropic Claude (messages.create) with text+image and return *all* text blocks concatenated.
88
  Adds a system message to enforce strict JSON output.
89
  """
 
 
90
  msg = anthropic_client.messages.create(
91
  model=model_name,
92
  max_tokens=2000,
@@ -114,6 +123,8 @@ def image_anthropic_call(model_name: str, prompt: str, media_type: str, b64: str
114
 
115
  def image_openai_call(model_name: str, prompt: str, media_type: str, b64: str) -> str:
116
  """Call OpenAI with text+image input."""
 
 
117
  data_url = f"data:{media_type};base64,{b64}"
118
  resp = openai_client.responses.create(
119
  model=model_name,
 
17
 
18
  # === Env & Clients ===
19
  load_dotenv()
20
+ # Strip whitespace/newlines from API keys (common issue with env vars)
21
+ openai_api_key = os.getenv("OPENAI_API_KEY", "").strip()
22
+ anthropic_api_key = os.getenv("ANTHROPIC_API_KEY", "").strip()
23
 
24
  # Both clients read keys from env by default; explicit is also fine:
25
  openai_client = OpenAI(api_key=openai_api_key) if openai_api_key else OpenAI()
 
29
  def get_response(model: str, prompt: str) -> str:
30
  """Get response from LLM (OpenAI or Anthropic)."""
31
  if "claude" in model.lower() or "anthropic" in model.lower():
32
+ # Check if Anthropic API key is available
33
+ if not anthropic_api_key:
34
+ raise ValueError("ANTHROPIC_API_KEY not set. Please add it as a secret in HuggingFace Spaces settings.")
35
  # Anthropic Claude format
36
  message = anthropic_client.messages.create(
37
  model=model,
 
40
  )
41
  return message.content[0].text
42
  else:
43
+ # Check if OpenAI API key is available
44
+ if not openai_api_key:
45
+ raise ValueError("OPENAI_API_KEY not set. Please add it as a secret in HuggingFace Spaces settings.")
46
  # Default to OpenAI format for all other models (gpt-4, o3-mini, o1, etc.)
47
  response = openai_client.responses.create(
48
  model=model,
 
94
  Call Anthropic Claude (messages.create) with text+image and return *all* text blocks concatenated.
95
  Adds a system message to enforce strict JSON output.
96
  """
97
+ if not anthropic_api_key:
98
+ raise ValueError("ANTHROPIC_API_KEY not set. Please add it as a secret in HuggingFace Spaces settings.")
99
  msg = anthropic_client.messages.create(
100
  model=model_name,
101
  max_tokens=2000,
 
123
 
124
  def image_openai_call(model_name: str, prompt: str, media_type: str, b64: str) -> str:
125
  """Call OpenAI with text+image input."""
126
+ if not openai_api_key:
127
+ raise ValueError("OPENAI_API_KEY not set. Please add it as a secret in HuggingFace Spaces settings.")
128
  data_url = f"data:{media_type};base64,{b64}"
129
  resp = openai_client.responses.create(
130
  model=model_name,