Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,18 +8,14 @@ import json_repair
|
|
| 8 |
|
| 9 |
# --- 1. CONFIGURATION ---
|
| 10 |
print(">>> INITIALIZING SomAI TEXT NODE...")
|
| 11 |
-
|
| 12 |
# Text Model Only
|
| 13 |
PHI3_REPO = "microsoft/Phi-3-mini-4k-instruct-gguf"
|
| 14 |
PHI3_FILE = "Phi-3-mini-4k-instruct-q4.gguf"
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
-
|
| 18 |
app.add_middleware(
|
| 19 |
CORSMiddleware,
|
| 20 |
-
allow_origins=["*"],
|
| 21 |
-
allow_credentials=True,
|
| 22 |
-
allow_methods=["*"],
|
| 23 |
allow_headers=["*"],
|
| 24 |
)
|
| 25 |
|
|
@@ -42,7 +38,7 @@ def validate_codes(pipeline):
|
|
| 42 |
for item in pipeline:
|
| 43 |
code = item.get('code', '').upper().replace('.', '')
|
| 44 |
match = next((k for k in VALID_ICD10 if k.replace('.', '') == code), None)
|
| 45 |
-
|
| 46 |
if match:
|
| 47 |
validated.append({
|
| 48 |
"type": item.get("type", "Unknown"),
|
|
@@ -89,7 +85,6 @@ class ChatRequest(BaseModel):
|
|
| 89 |
prompt: str
|
| 90 |
|
| 91 |
# --- 5. ROUTES ---
|
| 92 |
-
|
| 93 |
@app.get("/")
|
| 94 |
def health():
|
| 95 |
return {"status": "Active", "system": "SomAI Text Node"}
|
|
@@ -97,11 +92,11 @@ def health():
|
|
| 97 |
@app.post("/analyze")
|
| 98 |
def analyze(req: AnalysisRequest):
|
| 99 |
if not llm: raise HTTPException(503, "Text Model Unavailable")
|
| 100 |
-
|
| 101 |
formatted_prompt = f"<|user|>{req.prompt}<|end|><|assistant|>"
|
| 102 |
output = llm(formatted_prompt, max_tokens=400, temperature=0.1, stop=["<|end|>"])
|
| 103 |
raw_text = output['choices'][0]['text']
|
| 104 |
-
|
| 105 |
try:
|
| 106 |
data = json_repair.repair_json(raw_text, return_objects=True)
|
| 107 |
except:
|
|
@@ -115,7 +110,7 @@ def analyze(req: AnalysisRequest):
|
|
| 115 |
|
| 116 |
p_code = data.get('primaryConditionCode', {})
|
| 117 |
validated_p = validate_codes([{"code": p_code.get('code'), "type": "Primary", "description": p_code.get('description')}])
|
| 118 |
-
|
| 119 |
h_codes = data.get('historyCodes', [])
|
| 120 |
validated_h = validate_codes([{"code": h.get('code'), "type": "History", "description": h.get('description')} for h in h_codes])
|
| 121 |
|
|
@@ -126,12 +121,12 @@ def analyze(req: AnalysisRequest):
|
|
| 126 |
"historyCodes": validated_h,
|
| 127 |
"insuranceNote": data.get("insuranceNote") + " [Validated by SomAI Engine]"
|
| 128 |
}
|
| 129 |
-
|
| 130 |
return final_response
|
| 131 |
|
| 132 |
@app.post("/generate")
|
| 133 |
def generate(req: ChatRequest):
|
| 134 |
if not llm: raise HTTPException(503, "Text Model Unavailable")
|
|
|
|
| 135 |
formatted_prompt = f"<|user|>{req.prompt}<|end|><|assistant|>"
|
| 136 |
output = llm(formatted_prompt, max_tokens=250, temperature=0.6, stop=["<|end|>"])
|
| 137 |
return {"text": output['choices'][0]['text'].strip()}
|
|
|
|
| 8 |
|
| 9 |
# --- 1. CONFIGURATION ---
|
| 10 |
print(">>> INITIALIZING SomAI TEXT NODE...")
|
|
|
|
| 11 |
# Text Model Only
|
| 12 |
PHI3_REPO = "microsoft/Phi-3-mini-4k-instruct-gguf"
|
| 13 |
PHI3_FILE = "Phi-3-mini-4k-instruct-q4.gguf"
|
| 14 |
|
| 15 |
app = FastAPI()
|
|
|
|
| 16 |
app.add_middleware(
|
| 17 |
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"],
|
|
|
|
|
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
|
|
|
| 38 |
for item in pipeline:
|
| 39 |
code = item.get('code', '').upper().replace('.', '')
|
| 40 |
match = next((k for k in VALID_ICD10 if k.replace('.', '') == code), None)
|
| 41 |
+
|
| 42 |
if match:
|
| 43 |
validated.append({
|
| 44 |
"type": item.get("type", "Unknown"),
|
|
|
|
| 85 |
prompt: str
|
| 86 |
|
| 87 |
# --- 5. ROUTES ---
|
|
|
|
| 88 |
@app.get("/")
|
| 89 |
def health():
|
| 90 |
return {"status": "Active", "system": "SomAI Text Node"}
|
|
|
|
| 92 |
@app.post("/analyze")
|
| 93 |
def analyze(req: AnalysisRequest):
|
| 94 |
if not llm: raise HTTPException(503, "Text Model Unavailable")
|
| 95 |
+
|
| 96 |
formatted_prompt = f"<|user|>{req.prompt}<|end|><|assistant|>"
|
| 97 |
output = llm(formatted_prompt, max_tokens=400, temperature=0.1, stop=["<|end|>"])
|
| 98 |
raw_text = output['choices'][0]['text']
|
| 99 |
+
|
| 100 |
try:
|
| 101 |
data = json_repair.repair_json(raw_text, return_objects=True)
|
| 102 |
except:
|
|
|
|
| 110 |
|
| 111 |
p_code = data.get('primaryConditionCode', {})
|
| 112 |
validated_p = validate_codes([{"code": p_code.get('code'), "type": "Primary", "description": p_code.get('description')}])
|
| 113 |
+
|
| 114 |
h_codes = data.get('historyCodes', [])
|
| 115 |
validated_h = validate_codes([{"code": h.get('code'), "type": "History", "description": h.get('description')} for h in h_codes])
|
| 116 |
|
|
|
|
| 121 |
"historyCodes": validated_h,
|
| 122 |
"insuranceNote": data.get("insuranceNote") + " [Validated by SomAI Engine]"
|
| 123 |
}
|
|
|
|
| 124 |
return final_response
|
| 125 |
|
| 126 |
@app.post("/generate")
|
| 127 |
def generate(req: ChatRequest):
|
| 128 |
if not llm: raise HTTPException(503, "Text Model Unavailable")
|
| 129 |
+
|
| 130 |
formatted_prompt = f"<|user|>{req.prompt}<|end|><|assistant|>"
|
| 131 |
output = llm(formatted_prompt, max_tokens=250, temperature=0.6, stop=["<|end|>"])
|
| 132 |
return {"text": output['choices'][0]['text'].strip()}
|