File size: 6,815 Bytes
29dff86
7fd7699
 
 
 
 
 
29ba00a
 
7fd7699
 
 
29ba00a
29dff86
29ba00a
 
 
 
07ec94a
29dff86
29ba00a
 
 
 
 
29dff86
29ba00a
 
 
29dff86
 
 
29ba00a
 
7fd7699
 
 
2d5dc60
7fd7699
 
 
 
 
 
 
 
29dff86
 
29ba00a
 
 
29dff86
 
 
 
 
 
29ba00a
 
 
7fd7699
 
 
 
 
29ba00a
 
 
 
29dff86
 
29ba00a
 
 
29dff86
29ba00a
 
 
29dff86
 
 
 
 
 
29ba00a
 
29dff86
29ba00a
 
 
 
 
 
 
29dff86
 
29ba00a
 
 
 
 
 
 
29dff86
29ba00a
 
7fd7699
 
 
 
 
 
 
 
 
29dff86
 
 
 
 
7fd7699
29dff86
7fd7699
 
 
 
 
 
 
29dff86
 
7fd7699
 
 
 
 
29dff86
 
7fd7699
 
 
 
 
 
 
 
 
 
 
 
29dff86
 
 
 
 
7fd7699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29dff86
7fd7699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29ba00a
 
 
29dff86
29ba00a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from typing import List, Optional
from fastapi import FastAPI, Query, UploadFile, File, HTTPException
from fastapi.responses import RedirectResponse
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import uvicorn
import io
import csv
import os

from clinical_embedding import ModelManager

# Pydantic models for request/response
class EmbeddingRequest(BaseModel):
    sentences: List[str]
    pooling: str = 'cls'
    model: str = 'clinical_bert'

class EmbeddingResponse(BaseModel):
    embeddings: List[List[float]]
    shape: List[int]
    pooling: str
    model: str

# Initialize FastAPI app
app = FastAPI(
    title="Clinical Embedding API",
    description="API for generating embeddings using various models (ClinicalBERT, BERT, Word2Vec)",
    version="2.0.0"
)

# Add CORS middleware to allow web page access
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://santanche-clinical-embedding.hf.space"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Serve static files
app.mount("/app/static", StaticFiles(directory="static"), name="static")

# Initialize model manager (global instance)
model_manager = ModelManager()

@app.on_event("startup")
async def startup_event():
    """
    Load default model on startup.
    Other models will be loaded on demand (see ModelManager).
    """
    # Pre-load ClinicalBERT as it's the default
    model_manager.get_model('clinical_bert')

@app.get("/")
async def root():
    return RedirectResponse(url="/browser/")

@app.get("/browser/")
def get_browser():
    return FileResponse(os.path.join("static", "browser", "index.html"))

@app.get("/embeddings", response_model=EmbeddingResponse)
async def get_embeddings(
    sentences: List[str] = Query(..., description="List of sentences to embed"),
    pooling: str = Query('cls', description="Pooling strategy: mean, cls, or max"),
    model: str = Query('clinical_bert', description="Model to use: clinical_bert, bert, word2vec")
):
    """
    Generate embeddings for a list of sentences.
    Supports bracketed text for context-aware specific extraction.
    """
    # Validate pooling method
    if pooling not in ['mean', 'cls', 'max']:
        raise HTTPException(status_code=400, detail="Invalid pooling method. Choose from: mean, cls, max")
    
    try:
        embedder = model_manager.get_model(model)
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    
    # Generate embeddings
    embeddings = embedder.get_embeddings(sentences, pooling=pooling)
    
    # Convert to list for JSON serialization
    embeddings_list = embeddings.tolist()
    
    return EmbeddingResponse(
        embeddings=embeddings_list,
        shape=list(embeddings.shape),
        pooling=pooling,
        model=model
    )

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {
        "status": "healthy",
        "loaded_models": list(model_manager.models.keys())
    }

@app.post("/embeddings/batch")
async def post_embeddings_batch(request: EmbeddingRequest):
    """
    POST endpoint for batch embedding generation.
    """
    # Validate pooling method
    if request.pooling not in ['mean', 'cls', 'max']:
        raise HTTPException(status_code=400, detail="Invalid pooling method. Choose from: mean, cls, max")
    
    try:
        embedder = model_manager.get_model(request.model)
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    
    # Generate embeddings
    embeddings = embedder.get_embeddings(request.sentences, pooling=request.pooling)
    
    # Convert to list for JSON serialization
    embeddings_list = embeddings.tolist()
    
    return EmbeddingResponse(
        embeddings=embeddings_list,
        shape=list(embeddings.shape),
        pooling=request.pooling,
        model=request.model
    )

@app.post("/embeddings/file")
async def upload_file_embeddings(
    file: UploadFile = File(...),
    pooling: str = Query('cls', description="Pooling strategy: mean, cls, or max"),
    model: str = Query('clinical_bert', description="Model to use: clinical_bert, bert, word2vec")
):
    """
    Upload a CSV file with terms and get embeddings back as CSV.
    """
    # Validate file type
    if not file.filename.endswith('.csv'):
        raise HTTPException(status_code=400, detail="File must be a CSV")
    
    # Validate pooling method
    if pooling not in ['mean', 'cls', 'max']:
        raise HTTPException(status_code=400, detail="Invalid pooling method. Choose from: mean, cls, max")
    
    try:
        embedder = model_manager.get_model(model)
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    
    try:
        # Read CSV file
        contents = await file.read()
        csv_reader = csv.DictReader(io.StringIO(contents.decode('utf-8')))
        
        # Get column name (first column)
        fieldnames = csv_reader.fieldnames
        if not fieldnames or len(fieldnames) == 0:
            raise HTTPException(status_code=400, detail="CSV must have at least one column")
        
        column_name = fieldnames[0]
        
        # Extract terms
        terms = [row[column_name] for row in csv_reader if row[column_name].strip()]
        
        if not terms:
            raise HTTPException(status_code=400, detail="No terms found in CSV")
        
        # Generate embeddings
        embeddings = embedder.get_embeddings(terms, pooling=pooling)
        
        # Create output CSV
        output = io.StringIO()
        writer = csv.writer(output)
        
        # Write header (term + embedding dimensions)
        header = [column_name] + [f"dim_{i}" for i in range(embeddings.shape[1])]
        writer.writerow(header)
        
        # Write rows
        for term, embedding in zip(terms, embeddings):
            row = [term] + embedding.tolist()
            writer.writerow(row)
        
        # Prepare response
        output.seek(0)
        return StreamingResponse(
            io.BytesIO(output.getvalue().encode('utf-8')),
            media_type="text/csv",
            headers={"Content-Disposition": f"attachment; filename=embeddings_{file.filename}"}
        )
    
    except UnicodeDecodeError:
        raise HTTPException(status_code=400, detail="File must be UTF-8 encoded")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")

if __name__ == "__main__":
    # Run the server
    uvicorn.run(
        "server_clinical_embedding:app",
        host="0.0.0.0",
        port=8000,
        reload=False
    )