Spaces:
Sleeping
Sleeping
| import os | |
| import anthropic | |
| import openai | |
| from openai import OpenAI | |
| from anthropic import Anthropic | |
| # β Debug: log versions and proxy env vars | |
| print(f"Anthropic version: {anthropic.__version__}") | |
| print(f"OpenAI version: {openai.__version__}") | |
| print(f"Proxy env vars before cleanup: HTTP_PROXY={os.getenv('HTTP_PROXY')}, HTTPS_PROXY={os.getenv('HTTPS_PROXY')}, NO_PROXY={os.getenv('NO_PROXY')}") | |
| # β Forcefully disable proxies for HF Spaces | |
| os.environ["HTTP_PROXY"] = "" | |
| os.environ["HTTPS_PROXY"] = "" | |
| os.environ["NO_PROXY"] = "*" | |
| # β Patch any client constructors to ignore 'proxies' | |
| def safe_init(client_cls): | |
| """Wrap client __init__ to strip 'proxies' kwarg injected by Spaces.""" | |
| orig_init = client_cls.__init__ | |
| def wrapped_init(self, *args, **kwargs): | |
| if "proxies" in kwargs: | |
| print(f"[Patch] Stripped unexpected 'proxies' from {client_cls.__name__}") | |
| kwargs.pop("proxies", None) | |
| return orig_init(self, *args, **kwargs) | |
| client_cls.__init__ = wrapped_init | |
| # Apply patch to both clients | |
| safe_init(OpenAI) | |
| safe_init(Anthropic) | |
| def init_clients(): | |
| """Initialize API clients using HF Spaces Repository Secrets.""" | |
| try: | |
| openai_key = os.getenv("OPENAI_API_KEY") | |
| anthropic_key = os.getenv("ANTHROPIC_API_KEY") | |
| deepseek_key = os.getenv("DEEPSEEK_API_KEY") | |
| if not all([openai_key, anthropic_key, deepseek_key]): | |
| raise ValueError("Missing one or more API keys in HF Spaces Repository Secrets.") | |
| # β Initialize OpenAI client | |
| openai_client = OpenAI(api_key=openai_key) | |
| # β Initialize Anthropic client | |
| anthropic_client = Anthropic(api_key=anthropic_key) | |
| # β Initialize DeepSeek client (via OpenAI interface) | |
| deepseek_client = OpenAI( | |
| api_key=deepseek_key, | |
| base_url="https://api.deepseek.com/v1" | |
| ) | |
| return openai_client, anthropic_client, deepseek_client | |
| except Exception as e: | |
| raise Exception(f"Failed to initialize API clients: {str(e)}") | |
| def gpt4_mini_backend(system_msg, user_prompt, temperature): | |
| """Call GPT-4o Mini API.""" | |
| openai_client, _, _ = init_clients() | |
| try: | |
| r = openai_client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": system_msg}, | |
| {"role": "user", "content": user_prompt} | |
| ], | |
| temperature=temperature | |
| ) | |
| return r.choices[0].message.content, r.usage.total_tokens | |
| except Exception as e: | |
| raise Exception(f"GPT-4o-mini error: {str(e)}") | |
| def anthropic_backend(system_msg, user_prompt, temperature): | |
| """Call Anthropic Claude API.""" | |
| _, anthropic_client, _ = init_clients() | |
| try: | |
| r = anthropic_client.messages.create( | |
| model="claude-3-5-sonnet-20241022", | |
| system=system_msg, | |
| messages=[{"role": "user", "content": user_prompt}], | |
| max_tokens=2000, | |
| temperature=temperature | |
| ) | |
| text = r.content[0].text.strip() | |
| toks = r.usage.input_tokens + r.usage.output_tokens | |
| return text, toks | |
| except Exception as e: | |
| raise Exception(f"Anthropic error: {str(e)}") | |
| def deepseek_backend(system_msg, user_prompt, temperature): | |
| """Call DeepSeek API.""" | |
| _, _, deepseek_client = init_clients() | |
| try: | |
| r = deepseek_client.chat.completions.create( | |
| model="deepseek-chat", | |
| messages=[ | |
| {"role": "system", "content": system_msg}, | |
| {"role": "user", "content": user_prompt} | |
| ], | |
| temperature=temperature | |
| ) | |
| return r.choices[0].message.content, r.usage.total_tokens | |
| except Exception as e: | |
| raise Exception(f"DeepSeek error: {str(e)}") | |
| # β Register backends | |
| BACKENDS = { | |
| "GPT-4o Mini": gpt4_mini_backend, | |
| "Claude 3.5 Sonnet": anthropic_backend, | |
| "DeepSeek Chat": deepseek_backend | |
| } | |