Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- hf_model.py +72 -29
hf_model.py
CHANGED
|
@@ -1,66 +1,109 @@
|
|
|
|
|
| 1 |
"""
|
| 2 |
HuggingFace Inference API Model Wrapper
|
| 3 |
-
Uses
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
-
#
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
-
MODEL_ID = "google/gemma-3-4b-it"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
def generate_response(
|
| 17 |
-
messages:
|
| 18 |
-
max_tokens: int =
|
| 19 |
temperature: float = 0.7,
|
| 20 |
) -> str:
|
| 21 |
"""
|
| 22 |
-
Generate response using
|
| 23 |
-
|
| 24 |
Args:
|
| 25 |
messages: List of message dicts with 'role' and 'content'
|
| 26 |
-
max_tokens: Maximum tokens to generate
|
| 27 |
temperature: Sampling temperature
|
| 28 |
-
|
| 29 |
Returns:
|
| 30 |
-
Generated text response
|
| 31 |
"""
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
temperature=temperature,
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error: {repr(e)}\n\n{traceback.format_exc()}"
|
| 42 |
|
|
|
|
| 43 |
def calculate_expression(expression: str) -> str:
|
| 44 |
-
"""Simple calculator for financial expressions."""
|
| 45 |
import re
|
| 46 |
import math
|
| 47 |
-
|
| 48 |
-
# Safe eval with limited functions
|
| 49 |
allowed_names = {
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
-
# Clean the expression
|
| 57 |
expr = expression.strip()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
result = eval(expr, {"__builtins__": {}}, allowed_names)
|
| 64 |
return f"{result:,.2f}"
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"Calculation error: {str(e)}"
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
HuggingFace Inference API Model Wrapper
|
| 4 |
+
Uses HuggingFace InferenceClient with text_generation (more compatible than chat.completions).
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
+
import traceback
|
| 9 |
+
from typing import List, Dict
|
| 10 |
+
|
| 11 |
from huggingface_hub import InferenceClient
|
| 12 |
|
| 13 |
+
# ---- Config ----
|
| 14 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
+
MODEL_ID = os.getenv("MODEL_ID", "google/gemma-3-4b-it")
|
| 16 |
+
|
| 17 |
+
# Initialize client (bind model here so calls don't need model=...)
|
| 18 |
+
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _messages_to_prompt(messages: List[Dict]) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Convert OpenAI-style messages (role/content) to a simple prompt.
|
| 24 |
+
This is a generic format that works with text-generation endpoints.
|
| 25 |
+
"""
|
| 26 |
+
parts = []
|
| 27 |
+
for m in messages:
|
| 28 |
+
role = (m.get("role") or "user").lower()
|
| 29 |
+
content = m.get("content") or ""
|
| 30 |
+
|
| 31 |
+
if role == "system":
|
| 32 |
+
parts.append(f"System: {content}")
|
| 33 |
+
elif role == "assistant":
|
| 34 |
+
parts.append(f"Assistant: {content}")
|
| 35 |
+
else:
|
| 36 |
+
parts.append(f"User: {content}")
|
| 37 |
|
| 38 |
+
parts.append("Assistant:")
|
| 39 |
+
return "\n".join(parts)
|
| 40 |
|
| 41 |
|
| 42 |
def generate_response(
|
| 43 |
+
messages: List[Dict],
|
| 44 |
+
max_tokens: int = 512,
|
| 45 |
temperature: float = 0.7,
|
| 46 |
) -> str:
|
| 47 |
"""
|
| 48 |
+
Generate response using HF Inference API via text_generation.
|
| 49 |
+
|
| 50 |
Args:
|
| 51 |
messages: List of message dicts with 'role' and 'content'
|
| 52 |
+
max_tokens: Maximum new tokens to generate
|
| 53 |
temperature: Sampling temperature
|
| 54 |
+
|
| 55 |
Returns:
|
| 56 |
+
Generated text response (or detailed error)
|
| 57 |
"""
|
| 58 |
try:
|
| 59 |
+
if not HF_TOKEN:
|
| 60 |
+
return "Error: HF_TOKEN is not set. Add it in Space Settings -> Secrets."
|
| 61 |
+
|
| 62 |
+
prompt = _messages_to_prompt(messages)
|
| 63 |
+
|
| 64 |
+
out = client.text_generation(
|
| 65 |
+
prompt,
|
| 66 |
+
max_new_tokens=max_tokens,
|
| 67 |
temperature=temperature,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
return_full_text=False,
|
| 70 |
)
|
| 71 |
+
|
| 72 |
+
# InferenceClient.text_generation returns a string
|
| 73 |
+
return out.strip()
|
| 74 |
+
|
| 75 |
except Exception as e:
|
| 76 |
return f"Error: {repr(e)}\n\n{traceback.format_exc()}"
|
| 77 |
|
| 78 |
+
|
| 79 |
def calculate_expression(expression: str) -> str:
|
| 80 |
+
"""Simple calculator for financial expressions (safe eval)."""
|
| 81 |
import re
|
| 82 |
import math
|
| 83 |
+
|
|
|
|
| 84 |
allowed_names = {
|
| 85 |
+
"abs": abs,
|
| 86 |
+
"round": round,
|
| 87 |
+
"min": min,
|
| 88 |
+
"max": max,
|
| 89 |
+
"pow": pow,
|
| 90 |
+
"sqrt": math.sqrt,
|
| 91 |
+
"log": math.log,
|
| 92 |
+
"exp": math.exp,
|
| 93 |
+
"pi": math.pi,
|
| 94 |
+
"e": math.e,
|
| 95 |
}
|
| 96 |
+
|
| 97 |
try:
|
|
|
|
| 98 |
expr = expression.strip()
|
| 99 |
+
|
| 100 |
+
# Allow only digits/operators/parentheses/spaces/dots and ** for power
|
| 101 |
+
if not re.match(r"^[\d\s\+\-\*\/\.\(\)\^]+$", expr.replace("**", "^")):
|
| 102 |
+
# If it's not a pure math string, bail out gracefully
|
| 103 |
+
return "Calculation error: invalid characters in expression."
|
| 104 |
+
|
| 105 |
result = eval(expr, {"__builtins__": {}}, allowed_names)
|
| 106 |
return f"{result:,.2f}"
|
| 107 |
+
|
| 108 |
except Exception as e:
|
| 109 |
return f"Calculation error: {str(e)}"
|