Spaces:
Sleeping
Sleeping
Commit
Β·
460c5c1
1
Parent(s):
30e3a7e
fix the issues in the admin part is working
Browse files
app.py
CHANGED
|
@@ -619,7 +619,7 @@ def extract_rules_from_file(file_path) -> str:
|
|
| 619 |
return f"β Error reading file: {str(e)}"
|
| 620 |
|
| 621 |
|
| 622 |
-
def add_admin_rules(tenant_id: str, role: str, rules_text: str) -> str:
|
| 623 |
if not tenant_id or not tenant_id.strip():
|
| 624 |
return "β Tenant ID is required."
|
| 625 |
if not rules_text or not rules_text.strip():
|
|
@@ -655,9 +655,9 @@ def add_admin_rules(tenant_id: str, role: str, rules_text: str) -> str:
|
|
| 655 |
}
|
| 656 |
resp = requests.post(
|
| 657 |
f"{BACKEND_BASE_URL}/admin/rules",
|
| 658 |
-
params={"rule": rules[0], "enhance": "true"},
|
| 659 |
headers=headers,
|
| 660 |
-
timeout=
|
| 661 |
)
|
| 662 |
if resp.status_code == 200:
|
| 663 |
data = resp.json()
|
|
@@ -703,8 +703,8 @@ def add_admin_rules(tenant_id: str, role: str, rules_text: str) -> str:
|
|
| 703 |
f"{BACKEND_BASE_URL}/admin/rules/bulk",
|
| 704 |
json={"rules": chunk},
|
| 705 |
headers=headers,
|
| 706 |
-
params={"enhance": "true"},
|
| 707 |
-
timeout=
|
| 708 |
)
|
| 709 |
if resp.status_code == 200:
|
| 710 |
data = resp.json()
|
|
@@ -728,7 +728,7 @@ def add_admin_rules(tenant_id: str, role: str, rules_text: str) -> str:
|
|
| 728 |
else:
|
| 729 |
errors.append(f"Chunk {chunk_num}/{total_chunks} failed: {resp.status_code}: {resp.text}")
|
| 730 |
except requests.exceptions.Timeout:
|
| 731 |
-
errors.append(f"Chunk {chunk_num}/{total_chunks} timed out after
|
| 732 |
except Exception as exc:
|
| 733 |
errors.append(f"Chunk {chunk_num}/{total_chunks} error: {exc}")
|
| 734 |
|
|
@@ -759,19 +759,47 @@ def delete_admin_rule(tenant_id: str, role: str, rule: str) -> str:
|
|
| 759 |
|
| 760 |
tenant_id = tenant_id.strip()
|
| 761 |
rule = rule.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
|
| 763 |
try:
|
| 764 |
headers = {
|
| 765 |
"x-tenant-id": tenant_id,
|
| 766 |
"x-user-role": role if role else DEFAULT_ROLE
|
| 767 |
}
|
|
|
|
|
|
|
|
|
|
| 768 |
resp = requests.delete(
|
| 769 |
-
f"{BACKEND_BASE_URL}/admin/rules/{
|
| 770 |
headers=headers,
|
| 771 |
timeout=15
|
| 772 |
)
|
| 773 |
if resp.status_code == 200:
|
| 774 |
return f"ποΈ Deleted rule: {rule}"
|
|
|
|
|
|
|
| 775 |
return f"β Error {resp.status_code}: {resp.text}"
|
| 776 |
except requests.exceptions.ConnectionError:
|
| 777 |
return "β Could not reach backend. Ensure the FastAPI server is running."
|
|
@@ -781,7 +809,7 @@ def delete_admin_rule(tenant_id: str, role: str, rule: str) -> str:
|
|
| 781 |
return f"β Unexpected error: {exc}"
|
| 782 |
|
| 783 |
|
| 784 |
-
def add_rules_from_file(tenant_id: str, role: str, file_path):
|
| 785 |
"""
|
| 786 |
Extract rules from uploaded file and add them.
|
| 787 |
"""
|
|
@@ -804,13 +832,13 @@ def add_rules_from_file(tenant_id: str, role: str, file_path):
|
|
| 804 |
return "β No text could be extracted from the file.", summary, rows
|
| 805 |
|
| 806 |
# Add rules from extracted text
|
| 807 |
-
status = add_admin_rules(tenant_id, role, extracted_text)
|
| 808 |
summary, rows = fetch_admin_rules(tenant_id, role)
|
| 809 |
return status, summary, rows
|
| 810 |
|
| 811 |
|
| 812 |
-
def add_rules_and_refresh(tenant_id: str, role: str, rules_text: str):
|
| 813 |
-
status = add_admin_rules(tenant_id, role, rules_text)
|
| 814 |
summary, rows = fetch_admin_rules(tenant_id, role)
|
| 815 |
return status, summary, rows
|
| 816 |
|
|
@@ -2507,6 +2535,11 @@ with gr.Blocks(
|
|
| 2507 |
placeholder="Enter one rule per line...",
|
| 2508 |
lines=6
|
| 2509 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2510 |
upload_rules_button = gr.Button("Upload / Append Rules", variant="primary")
|
| 2511 |
|
| 2512 |
with gr.Column(scale=1):
|
|
@@ -2516,11 +2549,16 @@ with gr.Blocks(
|
|
| 2516 |
file_types=[".txt", ".pdf", ".doc", ".docx"],
|
| 2517 |
type="filepath"
|
| 2518 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2519 |
upload_file_button = gr.Button("Upload Rules from File", variant="primary")
|
| 2520 |
|
| 2521 |
delete_rule_input = gr.Textbox(
|
| 2522 |
label="Delete Rule",
|
| 2523 |
-
placeholder="Enter the
|
| 2524 |
)
|
| 2525 |
delete_rule_button = gr.Button("Delete Rule", variant="stop")
|
| 2526 |
|
|
@@ -2532,13 +2570,13 @@ with gr.Blocks(
|
|
| 2532 |
|
| 2533 |
upload_rules_button.click(
|
| 2534 |
fn=add_rules_and_refresh,
|
| 2535 |
-
inputs=[tenant_id_input, role_input, rules_input],
|
| 2536 |
outputs=[rules_status, rules_summary, rules_table]
|
| 2537 |
)
|
| 2538 |
|
| 2539 |
upload_file_button.click(
|
| 2540 |
-
fn=add_rules_from_file,
|
| 2541 |
-
inputs=[tenant_id_input, role_input, rules_file_upload],
|
| 2542 |
outputs=[rules_status, rules_summary, rules_table]
|
| 2543 |
)
|
| 2544 |
|
|
|
|
| 619 |
return f"β Error reading file: {str(e)}"
|
| 620 |
|
| 621 |
|
| 622 |
+
def add_admin_rules(tenant_id: str, role: str, rules_text: str, enhance: bool = True) -> str:
|
| 623 |
if not tenant_id or not tenant_id.strip():
|
| 624 |
return "β Tenant ID is required."
|
| 625 |
if not rules_text or not rules_text.strip():
|
|
|
|
| 655 |
}
|
| 656 |
resp = requests.post(
|
| 657 |
f"{BACKEND_BASE_URL}/admin/rules",
|
| 658 |
+
params={"rule": rules[0], "enhance": "true" if enhance else "false"},
|
| 659 |
headers=headers,
|
| 660 |
+
timeout=60 if enhance else 15
|
| 661 |
)
|
| 662 |
if resp.status_code == 200:
|
| 663 |
data = resp.json()
|
|
|
|
| 703 |
f"{BACKEND_BASE_URL}/admin/rules/bulk",
|
| 704 |
json={"rules": chunk},
|
| 705 |
headers=headers,
|
| 706 |
+
params={"enhance": "true" if enhance else "false"},
|
| 707 |
+
timeout=180 if enhance else 30 # Timeout per chunk (5 rules Γ 30s per rule + buffer if enhance, else quick)
|
| 708 |
)
|
| 709 |
if resp.status_code == 200:
|
| 710 |
data = resp.json()
|
|
|
|
| 728 |
else:
|
| 729 |
errors.append(f"Chunk {chunk_num}/{total_chunks} failed: {resp.status_code}: {resp.text}")
|
| 730 |
except requests.exceptions.Timeout:
|
| 731 |
+
errors.append(f"Chunk {chunk_num}/{total_chunks} timed out after 180s. Try adding rules without enhancement (set enhance=false) or add fewer rules at once.")
|
| 732 |
except Exception as exc:
|
| 733 |
errors.append(f"Chunk {chunk_num}/{total_chunks} error: {exc}")
|
| 734 |
|
|
|
|
| 759 |
|
| 760 |
tenant_id = tenant_id.strip()
|
| 761 |
rule = rule.strip()
|
| 762 |
+
|
| 763 |
+
# If user entered just a number, try to find the rule by index
|
| 764 |
+
if rule.isdigit():
|
| 765 |
+
try:
|
| 766 |
+
# Fetch rules to get the actual rule text by index
|
| 767 |
+
headers = {
|
| 768 |
+
"x-tenant-id": tenant_id,
|
| 769 |
+
"x-user-role": role if role else DEFAULT_ROLE
|
| 770 |
+
}
|
| 771 |
+
resp = requests.get(
|
| 772 |
+
f"{BACKEND_BASE_URL}/admin/rules",
|
| 773 |
+
headers=headers,
|
| 774 |
+
timeout=15
|
| 775 |
+
)
|
| 776 |
+
if resp.status_code == 200:
|
| 777 |
+
rules = resp.json().get("rules", [])
|
| 778 |
+
rule_idx = int(rule) - 1 # Convert to 0-based index
|
| 779 |
+
if 0 <= rule_idx < len(rules):
|
| 780 |
+
rule = rules[rule_idx] # Use the actual rule text
|
| 781 |
+
else:
|
| 782 |
+
return f"β Invalid rule number. Please enter a number between 1 and {len(rules)}, or enter the full rule text."
|
| 783 |
+
except Exception as e:
|
| 784 |
+
return f"β Error fetching rules: {e}"
|
| 785 |
|
| 786 |
try:
|
| 787 |
headers = {
|
| 788 |
"x-tenant-id": tenant_id,
|
| 789 |
"x-user-role": role if role else DEFAULT_ROLE
|
| 790 |
}
|
| 791 |
+
# URL encode the rule text to handle special characters
|
| 792 |
+
import urllib.parse
|
| 793 |
+
encoded_rule = urllib.parse.quote(rule, safe='')
|
| 794 |
resp = requests.delete(
|
| 795 |
+
f"{BACKEND_BASE_URL}/admin/rules/{encoded_rule}",
|
| 796 |
headers=headers,
|
| 797 |
timeout=15
|
| 798 |
)
|
| 799 |
if resp.status_code == 200:
|
| 800 |
return f"ποΈ Deleted rule: {rule}"
|
| 801 |
+
elif resp.status_code == 404:
|
| 802 |
+
return f"β Rule not found: '{rule}'. Please check the rules table and enter the exact rule text (or rule number)."
|
| 803 |
return f"β Error {resp.status_code}: {resp.text}"
|
| 804 |
except requests.exceptions.ConnectionError:
|
| 805 |
return "β Could not reach backend. Ensure the FastAPI server is running."
|
|
|
|
| 809 |
return f"β Unexpected error: {exc}"
|
| 810 |
|
| 811 |
|
| 812 |
+
def add_rules_from_file(tenant_id: str, role: str, file_path, enhance: bool = True):
|
| 813 |
"""
|
| 814 |
Extract rules from uploaded file and add them.
|
| 815 |
"""
|
|
|
|
| 832 |
return "β No text could be extracted from the file.", summary, rows
|
| 833 |
|
| 834 |
# Add rules from extracted text
|
| 835 |
+
status = add_admin_rules(tenant_id, role, extracted_text, enhance=enhance)
|
| 836 |
summary, rows = fetch_admin_rules(tenant_id, role)
|
| 837 |
return status, summary, rows
|
| 838 |
|
| 839 |
|
| 840 |
+
def add_rules_and_refresh(tenant_id: str, role: str, rules_text: str, enhance: bool = True):
|
| 841 |
+
status = add_admin_rules(tenant_id, role, rules_text, enhance=enhance)
|
| 842 |
summary, rows = fetch_admin_rules(tenant_id, role)
|
| 843 |
return status, summary, rows
|
| 844 |
|
|
|
|
| 2535 |
placeholder="Enter one rule per line...",
|
| 2536 |
lines=6
|
| 2537 |
)
|
| 2538 |
+
enhance_rules_checkbox = gr.Checkbox(
|
| 2539 |
+
label="π€ Enable LLM Enhancement (slower but provides better patterns and explanations)",
|
| 2540 |
+
value=True,
|
| 2541 |
+
info="Uncheck to add rules quickly without LLM enhancement"
|
| 2542 |
+
)
|
| 2543 |
upload_rules_button = gr.Button("Upload / Append Rules", variant="primary")
|
| 2544 |
|
| 2545 |
with gr.Column(scale=1):
|
|
|
|
| 2549 |
file_types=[".txt", ".pdf", ".doc", ".docx"],
|
| 2550 |
type="filepath"
|
| 2551 |
)
|
| 2552 |
+
enhance_file_checkbox = gr.Checkbox(
|
| 2553 |
+
label="π€ Enable LLM Enhancement",
|
| 2554 |
+
value=True,
|
| 2555 |
+
info="Uncheck to add rules quickly without LLM enhancement"
|
| 2556 |
+
)
|
| 2557 |
upload_file_button = gr.Button("Upload Rules from File", variant="primary")
|
| 2558 |
|
| 2559 |
delete_rule_input = gr.Textbox(
|
| 2560 |
label="Delete Rule",
|
| 2561 |
+
placeholder="Enter rule number (e.g., 1) or the full rule text to remove..."
|
| 2562 |
)
|
| 2563 |
delete_rule_button = gr.Button("Delete Rule", variant="stop")
|
| 2564 |
|
|
|
|
| 2570 |
|
| 2571 |
upload_rules_button.click(
|
| 2572 |
fn=add_rules_and_refresh,
|
| 2573 |
+
inputs=[tenant_id_input, role_input, rules_input, enhance_rules_checkbox],
|
| 2574 |
outputs=[rules_status, rules_summary, rules_table]
|
| 2575 |
)
|
| 2576 |
|
| 2577 |
upload_file_button.click(
|
| 2578 |
+
fn=lambda tenant_id, role, file_path, enhance: add_rules_from_file(tenant_id, role, file_path, enhance),
|
| 2579 |
+
inputs=[tenant_id_input, role_input, rules_file_upload, enhance_file_checkbox],
|
| 2580 |
outputs=[rules_status, rules_summary, rules_table]
|
| 2581 |
)
|
| 2582 |
|