Spaces:
Paused
Paused
Download services/lobby_chat.py from igardner/StockAnalysis: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/services/lobby_chat.py
- Command line
-
hf download hf://spaces/igardner/StockAnalysis/services/lobby_chat.py
-
curl -L -o lobby_chat.py https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/services/lobby_chat.py
2.08 kB
| """Provider boundary for safe, bounded lobby chat generation.""" | |
| import os | |
| import random | |
| import requests | |
| from config import Config | |
| SCRIPTED_LINES = ( | |
| "The parlor is open. What are you keeping an eye on?", | |
| "A new round is about to begin. Make your case before the bell.", | |
| "The room is warming up. Any theories yet?", | |
| "No rush—the next table is forming while the parlor stays open.", | |
| ) | |
| class LobbyChatGenerator: | |
| def generate(context_key="lobby"): | |
| """Generate one short, harmless line without requiring an AI provider.""" | |
| base_url = (Config.LOBBY_CHAT_API_URL or os.getenv("LOBBY_CHAT_API_URL", "")).strip() | |
| model = (Config.LOBBY_CHAT_MODEL or os.getenv("LOBBY_CHAT_MODEL", "")).strip() | |
| api_key = (Config.LOBBY_CHAT_API_KEY or os.getenv("LOBBY_CHAT_API_KEY", "")).strip() | |
| if not base_url or not model: | |
| return random.choice(SCRIPTED_LINES) | |
| url = base_url.rstrip("/") | |
| if not url.endswith("/chat/completions"): | |
| url += "/chat/completions" if url.endswith("/v1") else "/v1/chat/completions" | |
| headers = {"Content-Type": "application/json"} | |
| if api_key: | |
| headers["Authorization"] = f"Bearer {api_key}" | |
| response = requests.post( | |
| url, | |
| headers=headers, | |
| timeout=12, | |
| json={ | |
| "model": model, | |
| "messages": [ | |
| {"role": "system", "content": "Write one friendly, non-personal, single-sentence parlor lobby line. Never claim to be a human player. Maximum 180 characters."}, | |
| {"role": "user", "content": f"Scene: {context_key[:60]}"}, | |
| ], | |
| "temperature": 0.7, | |
| "max_tokens": 80, | |
| }, | |
| ) | |
| response.raise_for_status() | |
| content = response.json()["choices"][0]["message"]["content"] | |
| if not isinstance(content, str) or not content.strip(): | |
| raise ValueError("Chat provider returned empty content") | |
| return content.strip()[:180] | |