fintech-orchestrator / hf_model.py
TurkishCodeMan's picture
Upload folder using huggingface_hub
c71c58f verified
raw
history blame
1.85 kB
"""
HuggingFace Inference API Model Wrapper
Uses Gemma 3 27B for fintech orchestrator
"""
import os
from huggingface_hub import InferenceClient
# Initialize client
HF_TOKEN = os.getenv("HF_TOKEN")
MODEL_ID = "google/gemma-3-4b-it" # Gemma 3 27B Instruct
client = InferenceClient(token=HF_TOKEN)
def generate_response(
messages: list[dict],
max_tokens: int = 1024,
temperature: float = 0.7,
) -> str:
"""
Generate response using HuggingFace Inference API.
Args:
messages: List of message dicts with 'role' and 'content'
max_tokens: Maximum tokens to generate
temperature: Sampling temperature
Returns:
Generated text response
"""
try:
response = client.chat.completions.create(
model=MODEL_ID,
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
def calculate_expression(expression: str) -> str:
"""Simple calculator for financial expressions."""
import re
import math
# Safe eval with limited functions
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:
# Clean the expression
expr = expression.strip()
# Basic validation
if not re.match(r'^[\d\s\+\-\*\/\.\(\)\^]+$', expr.replace('**', '^')):
# Try to extract numbers and operators more flexibly
pass
result = eval(expr, {"__builtins__": {}}, allowed_names)
return f"{result:,.2f}"
except Exception as e:
return f"Calculation error: {str(e)}"