NexusInstruments commited on
Commit
c332c52
·
verified ·
1 Parent(s): 30f62e3

Create utils/backend.py

Browse files
Files changed (1) hide show
  1. utils/backend.py +50 -0
utils/backend.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # Default: Hugging Face or Ollama fallback
4
+ def run_llm(prompt: str) -> str:
5
+ """
6
+ Route prompt to an available LLM backend.
7
+ - Prefers Hugging Face Inference API if HF_API_TOKEN is set
8
+ - Falls back to Ollama (local) if OLLAMA_MODEL is set
9
+ - Otherwise returns a static placeholder response
10
+ """
11
+ # Hugging Face API
12
+ hf_token = os.getenv("HF_API_TOKEN")
13
+ hf_model = os.getenv("HF_MODEL", "mistralai/Mistral-7B-Instruct-v0.3")
14
+ if hf_token:
15
+ try:
16
+ import requests
17
+ response = requests.post(
18
+ f"https://api-inference.huggingface.co/models/{hf_model}",
19
+ headers={"Authorization": f"Bearer {hf_token}"},
20
+ json={"inputs": prompt},
21
+ timeout=30
22
+ )
23
+ if response.status_code == 200:
24
+ return response.json()[0]["generated_text"]
25
+ else:
26
+ return f"⚠️ HF error {response.status_code}: {response.text}"
27
+ except Exception as e:
28
+ return f"⚠️ HF backend error: {e}"
29
+
30
+ # Ollama API
31
+ ollama_model = os.getenv("OLLAMA_MODEL", "llama2")
32
+ try:
33
+ import requests
34
+ response = requests.post(
35
+ "http://localhost:11434/api/generate",
36
+ json={"model": ollama_model, "prompt": prompt},
37
+ timeout=30,
38
+ )
39
+ text = ""
40
+ for line in response.iter_lines():
41
+ if line:
42
+ chunk = line.decode("utf-8")
43
+ if '"response":"' in chunk:
44
+ text += chunk.split('"response":"')[1].split('"')[0]
45
+ return text if text else "⚠️ Ollama returned no output"
46
+ except Exception:
47
+ pass
48
+
49
+ # Fallback static
50
+ return "⚠️ No LLM backend configured. Please set HF_API_TOKEN or run Ollama."