piper-assistant / config.py
indirapravianti's picture
Deploy Piper sticker restock manager Gradio app
1a65785
Raw
History Blame Contribute Delete
1.81 kB
import os
HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
HF_MODEL = "Qwen/Qwen2.5-7B-Instruct"
_use_ollama = False
_ollama_mod = None
_hf_client = None
_llm_available = False
if os.environ.get("FORCE_HF", "1") != "1":
try:
if not os.environ.get("SPACE_ID"):
import ollama as _ollama_mod
_ollama_mod.list()
_use_ollama = True
_llm_available = True
except Exception:
_use_ollama = False
if _use_ollama:
print("[startup] Using Ollama locally with qwen2.5:1.5b")
else:
try:
from huggingface_hub import InferenceClient
_hf_client = InferenceClient(model=HF_MODEL, token=HF_TOKEN)
_llm_available = bool(HF_TOKEN) or bool(os.environ.get("SPACE_ID"))
print(f"[startup] HF Inference API — model={HF_MODEL}, token={'yes' if HF_TOKEN else 'space/default'}")
except Exception as exc:
print(f"[startup] HF client init failed: {exc}")
_hf_client = None
_llm_available = False
def llm_available() -> bool:
return _llm_available and (_use_ollama or _hf_client is not None)
def call_llm(prompt: str, system: str = "") -> str:
if not llm_available():
return "[LLM Error: Model unavailable — using rule-based fallback]"
messages = []
if system:
messages.append({"role": "system", "content": system})
messages.append({"role": "user", "content": prompt})
try:
if _use_ollama:
resp = _ollama_mod.chat(model="qwen2.5:1.5b", messages=messages)
return resp["message"]["content"]
resp = _hf_client.chat_completion(messages, max_tokens=2048, temperature=0.1)
return resp.choices[0].message.content
except Exception as e:
return f"[LLM Error: {e}]"