Spaces:
Sleeping
Sleeping
Download backend/llm_proxy.py from PROSTLE/StockSense-AI: direct link, hf CLI and curl.
- Browser
- Download file 4.84 kB
-
https://huggingface.co/spaces/PROSTLE/StockSense-AI/resolve/main/backend/llm_proxy.py
- Command line
-
hf download hf://spaces/PROSTLE/StockSense-AI/backend/llm_proxy.py
-
curl -L -o llm_proxy.py https://huggingface.co/spaces/PROSTLE/StockSense-AI/resolve/main/backend/llm_proxy.py
4.84 kB
| import os | |
| import json | |
| import base64 | |
| from openai import OpenAI | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| class LLMProxy: | |
| """ | |
| Unified LLM Client that routes calls through the OpenEnv proxy if available, | |
| otherwise falls back to Google Gemini for local development. | |
| """ | |
| def __init__(self): | |
| # Grader-injected environment variables | |
| self.api_base_url = os.getenv("API_BASE_URL") | |
| self.api_key = os.getenv("API_KEY") or os.getenv("HF_TOKEN") | |
| self.model_name = os.getenv("MODEL_NAME") | |
| # Local fallback variables | |
| self.gemini_key = os.getenv("GEMINI_API_KEY") | |
| self.client = None | |
| self.mode = "none" | |
| # 1. Prioritize Grader Proxy (OpenAI-compatible) | |
| print(f"[LLM] Check: base_url={self.api_base_url}, api_key={'[SET]' if self.api_key else '[MISSING]'}") | |
| # 1. Prioritize Grader Proxy (OpenAI-compatible) | |
| if self.api_base_url: | |
| # If base_url is set, we MUST use it to satisfy the OpenEnv grader. | |
| # Use a dummy key if API_KEY and HF_TOKEN are missing. | |
| active_key = self.api_key or "openenv-proxy-key" | |
| try: | |
| self.client = OpenAI( | |
| api_key=active_key, | |
| base_url=self.api_base_url | |
| ) | |
| self.mode = "proxy" | |
| print(f"[LLM] FORCING Proxy Mode: {self.api_base_url}") | |
| if not self.api_key: | |
| print("[LLM] Warning: No API_KEY/HF_TOKEN found, using placeholder.") | |
| except Exception as e: | |
| print(f"[LLM] Proxy initialization failed: {e}") | |
| # 2. Fallback to Gemini (Local Development) | |
| elif self.gemini_key: | |
| try: | |
| from google import genai | |
| self.client = genai.Client(api_key=self.gemini_key) | |
| self.mode = "gemini" | |
| print("[LLM] Fallback Mode: Google Gemini (Local/Dev)") | |
| except Exception as e: | |
| print(f"[LLM] Gemini fallback failed: {e}") | |
| else: | |
| print("[LLM] WARNING: No API credentials found. AI analysis will be disabled.") | |
| def generate_content(self, prompt: str, image_bytes: bytes = None) -> str: | |
| """ | |
| Generic interface to generate content from text and optional image. | |
| """ | |
| if not self.client: | |
| return "Error: No LLM client initialized." | |
| if self.mode == "proxy": | |
| return self._generate_via_proxy(prompt, image_bytes) | |
| elif self.mode == "gemini": | |
| return self._generate_via_gemini(prompt, image_bytes) | |
| return "Error: Unknown LLM mode." | |
| def _generate_via_proxy(self, prompt: str, image_bytes: bytes = None) -> str: | |
| model = self.model_name or "gpt-4o" # Default if not provided | |
| messages = [] | |
| if image_bytes: | |
| # OpenAI multimodal format | |
| base64_image = base64.b64encode(image_bytes).decode('utf-8') | |
| messages.append({ | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": prompt}, | |
| { | |
| "type": "image_url", | |
| "image_url": { | |
| "url": f"data:image/png;base64,{base64_image}" | |
| } | |
| } | |
| ] | |
| }) | |
| else: | |
| messages.append({"role": "user", "content": prompt}) | |
| try: | |
| response = self.client.chat.completions.create( | |
| model=model, | |
| messages=messages, | |
| response_format={ "type": "json_object" } if "JSON" in prompt.upper() else None | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| print(f"[LLM Proxy] Error: {e}") | |
| return f"Error: {str(e)}" | |
| def _generate_via_gemini(self, prompt: str, image_bytes: bytes = None) -> str: | |
| # Default models for Gemini fallback | |
| model = "gemini-2.0-flash" if image_bytes else "gemini-2.0-flash-lite" | |
| try: | |
| contents = [prompt] | |
| if image_bytes: | |
| from google.genai import types | |
| contents.append(types.Part.from_bytes(data=image_bytes, mime_type="image/png")) | |
| response = self.client.models.generate_content( | |
| model=model, | |
| contents=contents | |
| ) | |
| return response.text | |
| except Exception as e: | |
| print(f"[LLM Gemini] Error: {e}") | |
| return f"Error: {str(e)}" | |
| # Global singleton | |
| _proxy_instance = None | |
| def get_llm_client(): | |
| global _proxy_instance | |
| if _proxy_instance is None: | |
| _proxy_instance = LLMProxy() | |
| return _proxy_instance | |