Spaces:
Paused
Paused
Add detailed error reporting inside summarize endpoint
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import sys
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import torch
|
| 5 |
import uvicorn
|
| 6 |
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
| 7 |
|
| 8 |
-
print("=== STARTING LEGALPARAM FASTAPI SERVICE ===")
|
| 9 |
print("CUDA Available:", torch.cuda.is_available())
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
print("Device Name:", torch.cuda.get_device_name(0))
|
|
@@ -20,12 +21,8 @@ try:
|
|
| 20 |
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
|
| 21 |
config.rope_scaling = {"type": "linear", "factor": 1.0}
|
| 22 |
|
| 23 |
-
print("Loading tokenizer with
|
| 24 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 25 |
-
model_id,
|
| 26 |
-
use_fast=False,
|
| 27 |
-
trust_remote_code=False
|
| 28 |
-
)
|
| 29 |
|
| 30 |
print("Loading model onto GPU with bfloat16...")
|
| 31 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -62,38 +59,43 @@ def summarize(req: SummaryRequest):
|
|
| 62 |
if load_error or model is None:
|
| 63 |
raise HTTPException(status_code=500, detail=f"Model not loaded: {load_error}")
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 79 |
-
available_output = max(256, 2048 - inputs.input_ids.shape[1] - 10)
|
| 80 |
-
max_gen_tokens = min(800, available_output)
|
| 81 |
-
|
| 82 |
-
with torch.no_grad():
|
| 83 |
-
outputs = model.generate(
|
| 84 |
-
**inputs,
|
| 85 |
-
max_new_tokens=max_gen_tokens,
|
| 86 |
-
temperature=0.2,
|
| 87 |
-
do_sample=True,
|
| 88 |
-
top_p=0.95,
|
| 89 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 90 |
-
use_cache=False,
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import sys
|
| 2 |
+
import traceback
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import torch
|
| 6 |
import uvicorn
|
| 7 |
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
| 8 |
|
| 9 |
+
print("=== STARTING LEGALPARAM FASTAPI DOCKER SERVICE ===")
|
| 10 |
print("CUDA Available:", torch.cuda.is_available())
|
| 11 |
if torch.cuda.is_available():
|
| 12 |
print("Device Name:", torch.cuda.get_device_name(0))
|
|
|
|
| 21 |
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
|
| 22 |
config.rope_scaling = {"type": "linear", "factor": 1.0}
|
| 23 |
|
| 24 |
+
print("Loading tokenizer with trust_remote_code=False...")
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
print("Loading model onto GPU with bfloat16...")
|
| 28 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 59 |
if load_error or model is None:
|
| 60 |
raise HTTPException(status_code=500, detail=f"Model not loaded: {load_error}")
|
| 61 |
|
| 62 |
+
try:
|
| 63 |
+
prompt = (
|
| 64 |
+
"<user>\n"
|
| 65 |
+
"You are an expert Indian Insolvency Law editor. Synthesize and summarize the following multi-source raw case law summaries into a clean legal digest.\n"
|
| 66 |
+
"DO NOT include provider names, folder names, or lines like 'SOURCE 1'.\n\n"
|
| 67 |
+
"Format the output strictly as:\n"
|
| 68 |
+
"## Facts\n"
|
| 69 |
+
"[Brief facts]\n\n"
|
| 70 |
+
"## Issue & Holding\n"
|
| 71 |
+
"[Core question of law and the decision of the Tribunal/Court]\n\n"
|
| 72 |
+
f"RAW CASE LAW CONTENT:\n{req.text}\n"
|
| 73 |
+
"<assistant>\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 77 |
+
available_output = max(256, 2048 - inputs.input_ids.shape[1] - 10)
|
| 78 |
+
max_gen_tokens = min(800, available_output)
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
outputs = model.generate(
|
| 82 |
+
**inputs,
|
| 83 |
+
max_new_tokens=max_gen_tokens,
|
| 84 |
+
temperature=0.2,
|
| 85 |
+
do_sample=True,
|
| 86 |
+
top_p=0.95,
|
| 87 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 88 |
+
use_cache=False,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
summary = tokenizer.decode(
|
| 92 |
+
outputs[0][inputs.input_ids.shape[1] :], skip_special_tokens=True
|
| 93 |
+
)
|
| 94 |
+
return {"summary": summary}
|
| 95 |
+
except Exception as gen_err:
|
| 96 |
+
err_msg = f"Generation Exception: {gen_err}\n{traceback.format_exc()}"
|
| 97 |
+
print("❌ GENERATION ERROR:\n", err_msg, file=sys.stderr)
|
| 98 |
+
raise HTTPException(status_code=500, detail=err_msg)
|
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|