Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
-
import os, re, json, time, sqlite3, hashlib, secrets, random
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
from pathlib import Path
|
| 4 |
import requests
|
| 5 |
-
|
| 6 |
-
# ── Třetí strany ──
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
try:
|
|
@@ -41,7 +39,6 @@ PRICE_IDS = {
|
|
| 41 |
if STRIPE_OK and STRIPE_SECRET_KEY:
|
| 42 |
_stripe.api_key = STRIPE_SECRET_KEY
|
| 43 |
|
| 44 |
-
# Používáme Mistral přes bezplatné HF Inference API
|
| 45 |
HF_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 46 |
HF_API = "https://router.huggingface.co/hf-inference/models/" + HF_MODEL
|
| 47 |
HF_HDR = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
|
@@ -184,12 +181,13 @@ def ai_extract(text: str):
|
|
| 184 |
)
|
| 185 |
|
| 186 |
try:
|
|
|
|
| 187 |
r = requests.post(
|
| 188 |
HF_API, headers=HF_HDR,
|
| 189 |
json={"inputs": prompt, "parameters": {"max_new_tokens": 512, "temperature": 0.05, "return_full_text": False, "do_sample": False}},
|
| 190 |
timeout=45
|
| 191 |
)
|
| 192 |
-
if r.status_code == 503:
|
| 193 |
time.sleep(5)
|
| 194 |
r = requests.post(HF_API, headers=HF_HDR, json={"inputs": prompt, "parameters": {"max_new_tokens": 512, "return_full_text": False}}, timeout=45)
|
| 195 |
if r.status_code != 200:
|
|
@@ -211,7 +209,6 @@ def _parse_json(text: str):
|
|
| 211 |
except: pass
|
| 212 |
return None
|
| 213 |
|
| 214 |
-
# (Regex fallback a Demo data zůstávají nezměněny pro plynulost kódu)
|
| 215 |
def _parse_amount(s: str):
|
| 216 |
try:
|
| 217 |
s = re.sub(r'[^\d,.]', '', s).replace(",", ".")
|
|
@@ -266,6 +263,16 @@ def make_demo(filename: str, user_id: int) -> dict:
|
|
| 266 |
# ═════════════════════════════════════════════════════════════════════════════
|
| 267 |
# PARSE PIPELINE
|
| 268 |
# ═════════════════════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
def parse_and_save(file_path: str, filename: str, user_id: int, plan: str) -> dict:
|
| 270 |
ext = Path(filename).suffix.lower()
|
| 271 |
text = ocr_file(file_path, ext)
|
|
@@ -356,16 +363,6 @@ def fn_upload(files, token):
|
|
| 356 |
lim_after = check_limit(user["id"], user["plan"])
|
| 357 |
return f"✅ Processed **{len(results)}** file(s). Usage: {_usage_bar(lim_after['used'], lim_after['limit'])}", "\n\n---\n\n".join(results), json.dumps(last_data, ensure_ascii=False, indent=2, default=str)
|
| 358 |
|
| 359 |
-
def check_limit(user_id, plan):
|
| 360 |
-
month = datetime.now().strftime("%Y-%m")
|
| 361 |
-
conn = db()
|
| 362 |
-
n = conn.execute("SELECT COUNT(*) FROM invoices WHERE user_id=? AND strftime('%Y-%m',created_at)=?", (user_id, month)).fetchone()[0]
|
| 363 |
-
conn.close()
|
| 364 |
-
lim = PLANS[plan]["limit"]
|
| 365 |
-
if lim == -1: return {"ok": True, "used": n, "limit": "∞"}
|
| 366 |
-
rem = lim - n
|
| 367 |
-
return {"ok": rem > 0, "used": n, "limit": lim, "msg": f"Monthly limit of {lim} invoices reached." if rem <= 0 else ""}
|
| 368 |
-
|
| 369 |
def fn_list(token):
|
| 370 |
user = get_user(token)
|
| 371 |
if not user: return "❌ Not logged in."
|
|
|
|
| 1 |
+
import os, re, json, time, sqlite3, hashlib, secrets, random
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
from pathlib import Path
|
| 4 |
import requests
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
try:
|
|
|
|
| 39 |
if STRIPE_OK and STRIPE_SECRET_KEY:
|
| 40 |
_stripe.api_key = STRIPE_SECRET_KEY
|
| 41 |
|
|
|
|
| 42 |
HF_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 43 |
HF_API = "https://router.huggingface.co/hf-inference/models/" + HF_MODEL
|
| 44 |
HF_HDR = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
try:
|
| 184 |
+
# Použijeme text-generation endpoint přes HF Router
|
| 185 |
r = requests.post(
|
| 186 |
HF_API, headers=HF_HDR,
|
| 187 |
json={"inputs": prompt, "parameters": {"max_new_tokens": 512, "temperature": 0.05, "return_full_text": False, "do_sample": False}},
|
| 188 |
timeout=45
|
| 189 |
)
|
| 190 |
+
if r.status_code == 503: # Model se nabootovává
|
| 191 |
time.sleep(5)
|
| 192 |
r = requests.post(HF_API, headers=HF_HDR, json={"inputs": prompt, "parameters": {"max_new_tokens": 512, "return_full_text": False}}, timeout=45)
|
| 193 |
if r.status_code != 200:
|
|
|
|
| 209 |
except: pass
|
| 210 |
return None
|
| 211 |
|
|
|
|
| 212 |
def _parse_amount(s: str):
|
| 213 |
try:
|
| 214 |
s = re.sub(r'[^\d,.]', '', s).replace(",", ".")
|
|
|
|
| 263 |
# ═════════════════════════════════════════════════════════════════════════════
|
| 264 |
# PARSE PIPELINE
|
| 265 |
# ═════════════════════════════════════════════════════════════════════════════
|
| 266 |
+
def check_limit(user_id, plan):
|
| 267 |
+
month = datetime.now().strftime("%Y-%m")
|
| 268 |
+
conn = db()
|
| 269 |
+
n = conn.execute("SELECT COUNT(*) FROM invoices WHERE user_id=? AND strftime('%Y-%m',created_at)=?", (user_id, month)).fetchone()[0]
|
| 270 |
+
conn.close()
|
| 271 |
+
lim = PLANS[plan]["limit"]
|
| 272 |
+
if lim == -1: return {"ok": True, "used": n, "limit": "∞"}
|
| 273 |
+
rem = lim - n
|
| 274 |
+
return {"ok": rem > 0, "used": n, "limit": lim, "msg": f"Monthly limit of {lim} invoices reached." if rem <= 0 else ""}
|
| 275 |
+
|
| 276 |
def parse_and_save(file_path: str, filename: str, user_id: int, plan: str) -> dict:
|
| 277 |
ext = Path(filename).suffix.lower()
|
| 278 |
text = ocr_file(file_path, ext)
|
|
|
|
| 363 |
lim_after = check_limit(user["id"], user["plan"])
|
| 364 |
return f"✅ Processed **{len(results)}** file(s). Usage: {_usage_bar(lim_after['used'], lim_after['limit'])}", "\n\n---\n\n".join(results), json.dumps(last_data, ensure_ascii=False, indent=2, default=str)
|
| 365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
def fn_list(token):
|
| 367 |
user = get_user(token)
|
| 368 |
if not user: return "❌ Not logged in."
|