pushed to hugging face
Browse filesFiles changed (3)
- DockerfileAdded+20
- main.pyAdded+93
- requirements.txtAdded+7
1234567891011121314151617181920FROM python:3.10-slim
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH
WORKDIR $HOME/app
COPY --chown=user requirements.txt .
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY --chown=user . .
EXPOSE 7860
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293from fastapi import FastAPI, HTTPExceptionfrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModelfrom transformers import AutoTokenizer, AutoModelfrom sklearn.cluster import KMeansimport torchimport numpy as npimport spacyimport time
app = FastAPI( title="Clinical Extractive Summarization", description="SciBERT + KMeans NLP Engine for Medical Reports")
app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],)
tokenizer = Nonemodel = Nonenlp = None
class ReportRequest(BaseModel): text: str num_sentences: int = 3
@app.post("/api/summarize")def summarize_medical_report(request: ReportRequest): start_time = time.time() global tokenizer, model, nlp if model is None: print("Initializing SciBERT and SpaCy... This takes a moment.") # Load SciBERT model_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.cli 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 NLP doc = nlp(request.text) sentences = [sent.text.strip() for sent in doc.sents if len(sent.text.strip()) > 5] # Edge case: Report is too short if len(sentences) <= request.num_sentences: return {"summary": request.text, "metadata": {"status": "too_short"}}
# 2. Get embeddings for each sentence using SciBERT embeddings = [] for sent in sentences: inputs = tokenizer(sent, return_tensors="pt", truncation=True, padding=True, max_length=512) with torch.no_grad(): output = model(**inputs) # Extract the [CLS] token representation cls_embedding = output.last_hidden_state[0][0].numpy() embeddings.append(cls_embedding)
# 3. Use KMeans to cluster the embeddings and find the most central sentences # n_init='auto' suppresses sklearn warnings kmeans = KMeans(n_clusters=request.num_sentences, n_init='auto', random_state=42).fit(embeddings) avg = [] for i in range(request.num_sentences): # Find the sentence closest to the cluster centroid idx = np.argmin(np.linalg.norm(embeddings - kmeans.cluster_centers_[i], axis=1)) avg.append(idx)
# 4. Sort indices chronologically to maintain report flow avg = sorted(list(set(avg))) final_summary = " ".join([sentences[i] for i in avg]) process_time = round((time.time() - start_time) * 1000, 2) return { "summary": final_summary, "metadata": { "processing_time_ms": process_time, "original_length": len(sentences), "summary_length": len(avg), "engine": "SciBERT + KMeans" } }1234567fastapiuvicornpydantictransformerstorchscikit-learnspacyDockerfileAdded+20
main.pyAdded+93
requirements.txtAdded+7