Update engine.py
Browse files
engine.py
CHANGED
|
@@ -33,41 +33,34 @@ from reportlab.lib.units import inch
|
|
| 33 |
# ββ Hugging Face LLM call ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
import requests
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def ask_llm(prompt: str, temperature: float = 0.7, max_tokens: int = 512) -> str:
|
| 37 |
"""
|
| 38 |
-
Hugging Face Serverless Inference API call using
|
| 39 |
-
|
| 40 |
"""
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
url = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
}
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
response = requests.post(url, headers=headers, json=payload, timeout=60)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
print(f"[API ERROR] Body: {response.text}\n")
|
| 60 |
-
|
| 61 |
-
response.raise_for_status()
|
| 62 |
-
data = response.json()
|
| 63 |
|
| 64 |
-
if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
|
| 65 |
-
return data[0]["generated_text"]
|
| 66 |
-
return str(data)
|
| 67 |
except Exception as e:
|
| 68 |
-
print(f"\n[EXCEPTION] {str(e)}\n")
|
| 69 |
-
return
|
| 70 |
-
|
| 71 |
|
| 72 |
|
| 73 |
# ββ Agent instances (singletons, created once at startup) βββββββββββββββββββββ
|
|
|
|
| 33 |
# ββ Hugging Face LLM call ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
import requests
|
| 35 |
|
| 36 |
+
from huggingface_hub import InferenceClient
|
| 37 |
+
import os
|
| 38 |
+
|
| 39 |
+
# Initialize the official Hugging Face production inference engine
|
| 40 |
+
client = InferenceClient(model=HF_MODEL, token=os.environ.get("HF_TOKEN"))
|
| 41 |
+
|
| 42 |
def ask_llm(prompt: str, temperature: float = 0.7, max_tokens: int = 512) -> str:
|
| 43 |
"""
|
| 44 |
+
Hugging Face Serverless Inference API call using the official InferenceClient.
|
| 45 |
+
This automatically manages API cold-starts and internal retry flags.
|
| 46 |
"""
|
| 47 |
try:
|
| 48 |
+
# Hugging Face API requirements demand a temperature reading strictly > 0
|
| 49 |
+
safe_temp = temperature if temperature > 0 else 0.01
|
|
|
|
| 50 |
|
| 51 |
+
response = client.text_generation(
|
| 52 |
+
prompt,
|
| 53 |
+
max_new_tokens=max_tokens,
|
| 54 |
+
temperature=safe_temp,
|
| 55 |
+
return_full_text=False
|
| 56 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Strip structural infrastructure tokens to keep ValidatorAgent clean
|
| 59 |
+
return response.replace("<s>", "").replace("</s>", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
+
print(f"\n[API EXCEPTION] {str(e)}\n")
|
| 63 |
+
return "" # Safe fallback triggers programmatic error routing rather than crashing UI
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
# ββ Agent instances (singletons, created once at startup) βββββββββββββββββββββ
|