Basementup commited on
Commit
2728f58
·
verified ·
1 Parent(s): 7bb4b01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -32
app.py CHANGED
@@ -22,6 +22,7 @@ import pytesseract
22
  # Bypasses router.huggingface.co (PAID) entirely.
23
  # Uses api-inference.huggingface.co — completely free, same HF_TOKEN, no credits.
24
  import requests as _req
 
25
 
26
  _FREE_API = "https://api-inference.huggingface.co/models/{model}"
27
  _MODELS = [
@@ -35,44 +36,58 @@ def call_llama(prompt: str, max_tokens: int = 2000) -> str:
35
  headers = {"Authorization": f"Bearer {token}"} if token else {}
36
 
37
  for model in _MODELS:
38
- try:
39
- # Format prompt correctly for each model family
40
- if "zephyr" in model:
41
- fmt = f"<|system|>\nYou are a precise legal document analyst.</s>\n<|user|>\n{prompt}</s>\n<|assistant|>\n"
42
- elif "Phi-3" in model:
43
- fmt = f"<|user|>\n{prompt}<|end|>\n<|assistant|>\n"
44
- else:
45
- fmt = f"[INST] {prompt} [/INST]"
46
-
47
- def _post(f):
48
- return _req.post(
49
- _FREE_API.format(model=model), headers=headers,
50
- json={"inputs": f,
51
- "parameters": {"max_new_tokens": max_tokens,
52
- "temperature": 0.7,
53
- "return_full_text": False,
54
- "do_sample": True}},
55
- timeout=120
 
 
 
 
 
 
56
  )
57
 
58
- resp = _post(fmt)
 
 
 
 
 
59
 
60
- # Model loading — wait and retry once
61
- if resp.status_code == 503:
62
- import time; time.sleep(20)
63
- resp = _post(fmt)
64
 
65
- if resp.status_code == 200:
66
- result = resp.json()
67
- if isinstance(result, list) and result:
68
- text = result[0].get("generated_text", "").strip()
69
- if text:
70
- return text
71
 
72
- except Exception:
73
- continue
 
 
 
 
 
 
74
 
75
- return "Could not reach AI. Make sure HF_TOKEN is set in your Space secrets."
76
 
77
 
78
  # ── FILE EXTRACTION ───────────────────────────────────────────────────────────
 
22
  # Bypasses router.huggingface.co (PAID) entirely.
23
  # Uses api-inference.huggingface.co — completely free, same HF_TOKEN, no credits.
24
  import requests as _req
25
+ import time as _time
26
 
27
  _FREE_API = "https://api-inference.huggingface.co/models/{model}"
28
  _MODELS = [
 
36
  headers = {"Authorization": f"Bearer {token}"} if token else {}
37
 
38
  for model in _MODELS:
39
+ # Try each model up to 4 times to handle cold starts
40
+ for attempt in range(4):
41
+ try:
42
+ if "zephyr" in model:
43
+ fmt = f"<|system|>\nYou are a precise legal document analyst.</s>\n<|user|>\n{prompt}</s>\n<|assistant|>\n"
44
+ elif "Phi-3" in model:
45
+ fmt = f"<|user|>\n{prompt}<|end|>\n<|assistant|>\n"
46
+ else:
47
+ fmt = f"[INST] {prompt} [/INST]"
48
+
49
+ resp = _req.post(
50
+ _FREE_API.format(model=model),
51
+ headers=headers,
52
+ json={
53
+ "inputs": fmt,
54
+ "parameters": {
55
+ "max_new_tokens": max_tokens,
56
+ "temperature": 0.7,
57
+ "return_full_text": False,
58
+ "do_sample": True,
59
+ },
60
+ "options": {"wait_for_model": True}
61
+ },
62
+ timeout=180
63
  )
64
 
65
+ if resp.status_code == 200:
66
+ result = resp.json()
67
+ if isinstance(result, list) and result:
68
+ text = result[0].get("generated_text", "").strip()
69
+ if text:
70
+ return text
71
 
72
+ elif resp.status_code == 503:
73
+ # Still loading — wait longer each attempt
74
+ _time.sleep(30 * (attempt + 1))
75
+ continue
76
 
77
+ elif resp.status_code in [429, 402]:
78
+ # Rate limit or payment — try next model immediately
79
+ break
 
 
 
80
 
81
+ else:
82
+ # Other error — short wait then retry
83
+ _time.sleep(10)
84
+ continue
85
+
86
+ except Exception:
87
+ _time.sleep(10)
88
+ continue
89
 
90
+ return "Could not reach AI. The models may be loading — please try again in 30 seconds."
91
 
92
 
93
  # ── FILE EXTRACTION ───────────────────────────────────────────────────────────