Spaces:
Sleeping
Sleeping
Update api/index.py
Browse files- api/index.py +35 -62
api/index.py
CHANGED
|
@@ -3,12 +3,12 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
-
from typing import List, Optional, Dict
|
| 7 |
from groq import Groq
|
| 8 |
|
| 9 |
-
# --- 1. SETUP
|
| 10 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 11 |
-
app = FastAPI(title="Beploo
|
| 12 |
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
|
@@ -18,22 +18,22 @@ app.add_middleware(
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
KB = {}
|
| 23 |
-
def
|
| 24 |
-
path = f"data/{
|
| 25 |
if os.path.exists(path):
|
| 26 |
with open(path, "r", encoding="utf-8") as f:
|
| 27 |
return json.load(f)
|
| 28 |
return {}
|
| 29 |
|
| 30 |
-
print("🧠 Loading
|
| 31 |
-
KB["legal"] =
|
| 32 |
-
KB["tree"] =
|
| 33 |
-
KB["commodities"] =
|
| 34 |
-
print("✅
|
| 35 |
|
| 36 |
-
# ---
|
| 37 |
class ContextAuditRequest(BaseModel):
|
| 38 |
productName: str
|
| 39 |
description: str
|
|
@@ -41,65 +41,38 @@ class ContextAuditRequest(BaseModel):
|
|
| 41 |
form: str
|
| 42 |
remaining_chapters: List[str]
|
| 43 |
|
| 44 |
-
|
| 45 |
-
note_text: str
|
| 46 |
-
product_signals: str
|
| 47 |
-
|
| 48 |
-
class SplitterRequest(BaseModel):
|
| 49 |
-
remaining_ids: List[str]
|
| 50 |
-
product_signals: str
|
| 51 |
-
|
| 52 |
-
# --- 3. AUDIT LAYERS ---
|
| 53 |
|
| 54 |
@app.get("/")
|
| 55 |
def home():
|
| 56 |
return {"status": "online", "mode": "Customs Grade Reasoning"}
|
| 57 |
|
| 58 |
-
# 🛡️ LAYER 1: CONTEXT INTELLIGENCE
|
| 59 |
@app.post("/analyze_audit_context")
|
| 60 |
def analyze_audit_context(req: ContextAuditRequest):
|
|
|
|
|
|
|
|
|
|
| 61 |
system_prompt = """
|
| 62 |
-
You are a Senior Customs Auditor.
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
"""
|
|
|
|
| 66 |
user_input = f"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
"""
|
| 72 |
-
|
| 73 |
-
# Force JSON structure as requested
|
| 74 |
-
completion = client.chat.completions.create(
|
| 75 |
-
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}],
|
| 76 |
-
model="llama-3.3-70b-versatile",
|
| 77 |
-
response_format={"type": "json_object"}
|
| 78 |
-
)
|
| 79 |
-
return json.loads(completion.choices[0].message.content)
|
| 80 |
-
|
| 81 |
-
# ⚖️ LAYER 2: LEGAL NOTE INTERPRETER
|
| 82 |
-
@app.post("/interpret_legal_note")
|
| 83 |
-
def interpret_legal_note(req: LegalNoteRequest):
|
| 84 |
-
system_prompt = "You are a Legal Expert. Does this legal note exclude the product? Answer strictly YES or NO with reasoning."
|
| 85 |
-
user_input = f"Legal Note: {req.note_text}\nProduct Context: {req.product_signals}"
|
| 86 |
-
|
| 87 |
-
completion = client.chat.completions.create(
|
| 88 |
-
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}],
|
| 89 |
-
model="llama-3.3-70b-versatile",
|
| 90 |
-
response_format={"type": "json_object"}
|
| 91 |
-
)
|
| 92 |
-
return json.loads(completion.choices[0].message.content)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
response_format={"type": "json_object"}
|
| 104 |
-
)
|
| 105 |
-
return json.loads(completion.choices[0].message.content)
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
+
from typing import List, Optional, Dict
|
| 7 |
from groq import Groq
|
| 8 |
|
| 9 |
+
# --- 1. SETUP ---
|
| 10 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 11 |
+
app = FastAPI(title="Beploo Audit-Grade Reasoning Engine")
|
| 12 |
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
|
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# --- 2. KNOWLEDGE BASE LOADER ---
|
| 22 |
KB = {}
|
| 23 |
+
def load_kb(filename):
|
| 24 |
+
path = f"data/{filename}"
|
| 25 |
if os.path.exists(path):
|
| 26 |
with open(path, "r", encoding="utf-8") as f:
|
| 27 |
return json.load(f)
|
| 28 |
return {}
|
| 29 |
|
| 30 |
+
print("🧠 Loading Customs Data...")
|
| 31 |
+
KB["legal"] = load_kb("beploo_legal_notes.json")
|
| 32 |
+
KB["tree"] = load_kb("beploo_wizard_tree_grouped.json")
|
| 33 |
+
KB["commodities"] = load_kb("commodities.json")
|
| 34 |
+
print("✅ KB Ready.")
|
| 35 |
|
| 36 |
+
# --- 3. MODELS ---
|
| 37 |
class ContextAuditRequest(BaseModel):
|
| 38 |
productName: str
|
| 39 |
description: str
|
|
|
|
| 41 |
form: str
|
| 42 |
remaining_chapters: List[str]
|
| 43 |
|
| 44 |
+
# --- 4. ENDPOINTS ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
@app.get("/")
|
| 47 |
def home():
|
| 48 |
return {"status": "online", "mode": "Customs Grade Reasoning"}
|
| 49 |
|
|
|
|
| 50 |
@app.post("/analyze_audit_context")
|
| 51 |
def analyze_audit_context(req: ContextAuditRequest):
|
| 52 |
+
# Retrieve local commodity hints for the AI
|
| 53 |
+
hint = next((v for k, v in KB["commodities"].items() if k.lower() in req.productName.lower()), "")
|
| 54 |
+
|
| 55 |
system_prompt = """
|
| 56 |
+
You are a Senior Customs Auditor.
|
| 57 |
+
Use GRI 1 and GRI 3(b) to explain logic.
|
| 58 |
+
Do NOT declare final HS codes. Suggest reasoning direction only.
|
| 59 |
+
Return STRICT JSON.
|
| 60 |
"""
|
| 61 |
+
|
| 62 |
user_input = f"""
|
| 63 |
+
TARGET: {req.productName}
|
| 64 |
+
DESC: {req.description}
|
| 65 |
+
SIGNALS: {req.material}, {req.form}
|
| 66 |
+
KB HINT: {hint}
|
| 67 |
+
IN-SCOPE CHAPTERS: {req.remaining_chapters}
|
| 68 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
try:
|
| 71 |
+
completion = client.chat.completions.create(
|
| 72 |
+
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}],
|
| 73 |
+
model="llama-3.3-70b-versatile",
|
| 74 |
+
response_format={"type": "json_object"}
|
| 75 |
+
)
|
| 76 |
+
return json.loads(completion.choices[0].message.content)
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|