File size: 2,333 Bytes
28a0f3c
e4c209e
bfe842a
ce7981f
26d513b
 
ce7981f
bfe842a
26d513b
 
e4c209e
 
 
28a0f3c
bfe842a
28a0f3c
ce7981f
 
e4c209e
26d513b
f85caca
28a0f3c
e4c209e
bfe842a
ce7981f
bfe842a
 
ce7981f
 
 
 
26d513b
bfe842a
 
 
 
ce7981f
 
 
 
 
 
 
bfe842a
 
 
ce7981f
26d513b
 
 
ce7981f
26d513b
bfe842a
ce7981f
e4c209e
ae3c027
e4c209e
28a0f3c
e4c209e
28a0f3c
e4c209e
 
28a0f3c
e4c209e
28a0f3c
 
 
 
 
 
 
 
 
 
e4c209e
28a0f3c
e4c209e
 
28a0f3c
 
 
e4c209e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- 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)}"