Update main.py
Browse files5 unmodified lines678910113 unmodified lines151617181920216 unmodified lines2829303132331 unmodified line353637383940414243444546474849505152535455565718 unmodified lines767778798081825 unmodified linesimport torchimport numpy as npimport spacyimport timeapp = FastAPI(3 unmodified linesapp.add_middleware(CORSMiddleware,allow_origins=["*"],allow_methods=["*"],allow_headers=["*"],)6 unmodified linestext: strnum_sentences: int = 3@app.post("/api/summarize")def summarize_medical_report(request: ReportRequest):start_time = time.time()1 unmodified lineglobal tokenizer, model, nlpif model is None:print("Initializing SciBERT and SpaCy... This takes a moment.")# Load SciBERTmodel_name = "allenai/scibert_scivocab_uncased"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModel.from_pretrained(model_name)try:nlp = spacy.load("en_core_web_sm")except OSError:import spacy.clispacy.cli.download("en_core_web_sm")nlp = spacy.load("en_core_web_sm")print("Models loaded successfully!")# 1. Safely split text into sentences using SpaCy NLPdoc = nlp(request.text)sentences = [sent.text.strip() for sent in doc.sents if len(sent.text.strip()) > 5]# Edge case: Report is too shortif len(sentences) <= request.num_sentences:return {"summary": request.text, "metadata": {"status": "too_short"}}18 unmodified linesidx = np.argmin(np.linalg.norm(embeddings - kmeans.cluster_centers_[i], axis=1))avg.append(idx)# 4. Sort indices chronologically to maintain report flowavg = sorted(list(set(avg)))final_summary = " ".join([sentences[i] for i in avg])5 unmodified lines67891011123 unmodified lines161718192021226 unmodified lines29303132333435363738394041421 unmodified line444546474849505152535455565758596061626364656667686918 unmodified lines888990919293945 unmodified linesimport torchimport numpy as npimport spacyimport spacy.cliimport timeapp = FastAPI(3 unmodified linesapp.add_middleware(CORSMiddleware,allow_origins=["*"],allow_methods=["*"],allow_headers=["*"],)6 unmodified linestext: strnum_sentences: int = 3@app.get("/")def health_check():return {"status": "Engine is running","message": "Send POST requests to /api/summarize","docs": "Visit /docs for the Swagger UI"}@app.post("/api/summarize")def summarize_medical_report(request: ReportRequest):start_time = time.time()1 unmodified lineglobal tokenizer, model, nlpif model is None:print("Initializing SciBERT and SpaCy... This takes a moment.")# Load SciBERTmodel_name = "allenai/scibert_scivocab_uncased"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModel.from_pretrained(model_name)try:nlp = spacy.load("en_core_web_sm")except OSError:print("Downloading SpaCy English model...")spacy.cli.download("en_core_web_sm")nlp = spacy.load("en_core_web_sm")print("Models loaded successfully!")# 1. Safely split text into sentences using SpaCy NLPdoc = nlp(request.text)sentences = [sent.text.strip() for sent in doc.sents if len(sent.text.strip()) > 5]# Edge case: Report is too short to summarizeif len(sentences) <= request.num_sentences:return {"summary": request.text, "metadata": {"status": "too_short"}}18 unmodified linesidx = np.argmin(np.linalg.norm(embeddings - kmeans.cluster_centers_[i], axis=1))avg.append(idx)# 4. Sort indices chronologically to maintain original report flowavg = sorted(list(set(avg)))final_summary = " ".join([sentences[i] for i in avg])
main.py+16-4