Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """ | |
| HF Router (OpenAI-compatible) chat-completions wrapper for Hugging Face Spaces. | |
| Uses: | |
| POST https://router.huggingface.co/v1/chat/completions | |
| Requirements: | |
| - HF_TOKEN must have "Inference Providers" permission | |
| - If model is gated, accept license with the same HF account | |
| """ | |
| import os | |
| import traceback | |
| from typing import List, Dict | |
| import httpx | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| # İstersen provider'ı zorlamak için: "google/gemma-3-4b-it:hf-inference" | |
| MODEL_ID = os.getenv("MODEL_ID", "meta-llama/Llama-3.2-3B-Instruct") | |
| def generate_response( | |
| messages: List[Dict], | |
| max_tokens: int = 512, | |
| temperature: float = 0.7, | |
| ) -> str: | |
| if not HF_TOKEN: | |
| return "Error: HF_TOKEN is not set. Add it in Space Settings -> Secrets." | |
| url = "https://router.huggingface.co/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {HF_TOKEN}", | |
| "Content-Type": "application/json", | |
| } | |
| payload = { | |
| "model": MODEL_ID, | |
| "messages": messages, | |
| "max_tokens": max_tokens, | |
| "temperature": temperature, | |
| } | |
| try: | |
| with httpx.Client(timeout=90) as http: | |
| r = http.post(url, headers=headers, json=payload) | |
| if r.status_code >= 400: | |
| # Body'yi bas: 401/403/404/429 vs hemen anlaşılır | |
| return f"Error: HTTP {r.status_code}\n\n{r.text}" | |
| data = r.json() | |
| return data["choices"][0]["message"]["content"].strip() | |
| except Exception as e: | |
| return f"Error: {repr(e)}\n\n{traceback.format_exc()}" | |
| def calculate_expression(expression: str) -> str: | |
| """Simple calculator for financial expressions (safe eval).""" | |
| import re | |
| import math | |
| allowed_names = { | |
| "abs": abs, | |
| "round": round, | |
| "min": min, | |
| "max": max, | |
| "pow": pow, | |
| "sqrt": math.sqrt, | |
| "log": math.log, | |
| "exp": math.exp, | |
| "pi": math.pi, | |
| "e": math.e, | |
| } | |
| try: | |
| expr = expression.strip() | |
| if not re.match(r"^[\d\s\+\-\*\/\.\(\)\^]+$", expr.replace("**", "^")): | |
| return "Calculation error: invalid characters in expression." | |
| result = eval(expr, {"__builtins__": {}}, allowed_names) | |
| return f"{result:,.2f}" | |
| except Exception as e: | |
| return f"Calculation error: {str(e)}" | |