Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
_FREE_API.format(model=model),
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
text = result[0].get("generated_text", "").strip()
|
| 69 |
-
if text:
|
| 70 |
-
return text
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
return "Could not reach AI.
|
| 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 ───────────────────────────────────────────────────────────
|