ishan-25 commited on
Commit
79f2eab
Β·
verified Β·
1 Parent(s): fc59151

Update engine.py

Browse files
Files changed (1) hide show
  1. engine.py +20 -27
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 raw requests.
39
- Uses HF_TOKEN from environment variables.
40
  """
41
  try:
42
- token = os.environ.get("HF_TOKEN")
43
- headers = {"Authorization": f"Bearer {token}"}
44
- url = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
45
 
46
- payload = {
47
- "inputs": prompt,
48
- "parameters": {
49
- "max_new_tokens": max_tokens,
50
- "temperature": temperature,
51
- "return_full_text": False
52
- }
53
- }
54
-
55
- response = requests.post(url, headers=headers, json=payload, timeout=60)
56
 
57
- if response.status_code != 200:
58
- print(f"\n[API ERROR] Status: {response.status_code}")
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 f"❌ HF API Error: {str(e)}. Please check your HF_TOKEN."
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) ─────────────────────