File size: 8,241 Bytes
1f15859
 
 
 
30ca71a
1f15859
 
 
30ca71a
a8c3b23
30ca71a
a8c3b23
30ca71a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8c3b23
1f15859
 
 
 
 
 
 
 
a8c3b23
1f15859
 
 
 
a8c3b23
1f15859
 
 
 
 
 
 
 
a8c3b23
30ca71a
 
 
 
 
 
 
a8c3b23
30ca71a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8c3b23
1f15859
 
 
 
 
30ca71a
 
 
 
 
 
 
1f15859
 
30ca71a
1f15859
 
 
30ca71a
1f15859
30ca71a
1f15859
30ca71a
 
 
 
 
 
 
 
 
 
 
 
 
 
1f15859
 
30ca71a
 
1f15859
 
 
 
 
a8c3b23
1f15859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30ca71a
1f15859
 
 
a8c3b23
1f15859
 
 
 
 
 
 
30ca71a
1f15859
 
a8c3b23
1f15859
 
 
 
 
 
30ca71a
 
1f15859
30ca71a
 
 
 
1f15859
 
30ca71a
1f15859
 
 
30ca71a
1f15859
30ca71a
1f15859
30ca71a
 
 
 
 
 
 
 
 
 
 
 
 
 
1f15859
 
30ca71a
 
 
1f15859
 
 
 
 
 
a8c3b23
1f15859
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import os
from datasets import load_dataset
import random
from typing import Optional, List, Tuple, Union
import gradio as gr
from contextlib import asynccontextmanager

# Global variables
model = None
tokenizer = None
dataset = None

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup: Load the model
    global model, tokenizer, dataset
    try:
        # Load your fine-tuned model and tokenizer
        model_name = os.getenv("MODEL_NAME", "rgb2gbr/BioXP-0.5B-MedMCQA")
        model = AutoModelForCausalLM.from_pretrained(model_name)
        tokenizer = AutoTokenizer.from_pretrained(model_name)
        
        # Load MedMCQA dataset
        dataset = load_dataset("openlifescienceai/medmcqa")
        
        # Move model to GPU if available
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        model = model.to(device)
        model.eval()
    except Exception as e:
        print(f"Error loading model: {str(e)}")
        raise e
    
    yield  # This is where FastAPI serves the application
    
    # Shutdown: Clean up resources if needed
    if model is not None:
        del model
    if tokenizer is not None:
        del tokenizer
    if dataset is not None:
        del dataset
    torch.cuda.empty_cache()

app = FastAPI(lifespan=lifespan)

# Add CORS middleware for Gradio
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Define input models
class QuestionRequest(BaseModel):
    question: str
    options: list[str]  # List of 4 options

class DatasetQuestion(BaseModel):
    question: str
    opa: str
    opb: str
    opc: str
    opd: str
    cop: Optional[int] = None  # Correct option (0-3)
    exp: Optional[str] = None  # Explanation if available

def format_prompt(question: str, options: List[str]) -> str:
    """Format the prompt for the model"""
    prompt = f"Question: {question}\n\nOptions:\n"
    for i, opt in enumerate(options):
        prompt += f"{chr(65+i)}. {opt}\n"
    prompt += "\nAnswer:"
    return prompt

def get_question(index: Optional[int] = None, random_question: bool = False, format: str = "api") -> Union[DatasetQuestion, Tuple[str, str, str, str, str]]:
    """
    Get a question from the dataset.
    Args:
        index: Optional question index
        random_question: Whether to get a random question
        format: 'api' for DatasetQuestion object, 'gradio' for tuple
    """
    if dataset is None:
        raise Exception("Dataset not loaded")
    
    if random_question:
        index = random.randint(0, len(dataset['train']) - 1)
    elif index is None:
        raise ValueError("Either index or random_question must be provided")
    
    question_data = dataset['train'][index]
    
    if format == "gradio":
        return (
            question_data['question'],
            question_data['opa'],
            question_data['opb'],
            question_data['opc'],
            question_data['opd']
        )
    
    return DatasetQuestion(
        question=question_data['question'],
        opa=question_data['opa'],
        opb=question_data['opb'],
        opc=question_data['opc'],
        opd=question_data['opd'],
        cop=question_data['cop'] if 'cop' in question_data else None,
        exp=question_data['exp'] if 'exp' in question_data else None
    )

def predict_gradio(question: str, option_a: str, option_b: str, option_c: str, option_d: str):
    """Gradio interface prediction function"""
    try:
        options = [option_a, option_b, option_c, option_d]
        
        # Format the prompt
        prompt = format_prompt(question, options)
        
        # Tokenize the input
        inputs = tokenizer(
            prompt,
            return_tensors="pt",
            padding=True,
            truncation=True,
            max_length=512
        )
        
        device = next(model.parameters()).device
        inputs = {k: v.to(device) for k, v in inputs.items()}
        
        # Generate prediction
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_new_tokens=10,
                num_return_sequences=1,
                temperature=0.7,
                do_sample=False,
                pad_token_id=tokenizer.eos_token_id
            )
        
        # Decode the output
        prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # Extract the answer from the prediction
        answer = prediction.split("Answer:")[-1].strip()
        
        # Format the output for Gradio
        result = f"Model Output:\n{prediction}\n\n"
        result += f"Extracted Answer: {answer}"
        
        return result
    
    except Exception as e:
        return f"Error: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="Medical MCQ Predictor") as demo:
    gr.Markdown("# Medical MCQ Predictor")
    gr.Markdown("Enter a medical question and its options, or get a random question from MedMCQA dataset.")
    
    with gr.Row():
        with gr.Column():
            question = gr.Textbox(label="Question", lines=3)
            option_a = gr.Textbox(label="Option A")
            option_b = gr.Textbox(label="Option B")
            option_c = gr.Textbox(label="Option C")
            option_d = gr.Textbox(label="Option D")
            
            with gr.Row():
                predict_btn = gr.Button("Predict")
                random_btn = gr.Button("Get Random Question")
            
            output = gr.Textbox(label="Prediction", lines=5)
    
    predict_btn.click(
        fn=predict_gradio,
        inputs=[question, option_a, option_b, option_c, option_d],
        outputs=output
    )
    
    random_btn.click(
        fn=lambda: get_question(random_question=True, format="gradio"),
        inputs=[],
        outputs=[question, option_a, option_b, option_c, option_d]
    )

# Mount Gradio app to FastAPI
app = gr.mount_gradio_app(app, demo, path="/")

@app.get("/dataset/question")
async def get_dataset_question(index: Optional[int] = None, random_question: bool = False):
    """Get a question from the MedMCQA dataset"""
    try:
        return get_question(index=index, random_question=random_question)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/predict")
async def predict(request: QuestionRequest):
    if len(request.options) != 4:
        raise HTTPException(status_code=400, detail="Exactly 4 options are required")
    
    try:
        # Format the prompt
        prompt = format_prompt(request.question, request.options)
        
        # Tokenize the input
        inputs = tokenizer(
            prompt,
            return_tensors="pt",
            padding=True,
            truncation=True,
            max_length=512
        )
        
        device = next(model.parameters()).device
        inputs = {k: v.to(device) for k, v in inputs.items()}
        
        # Generate prediction
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_new_tokens=10,
                num_return_sequences=1,
                temperature=0.7,
                do_sample=False,
                pad_token_id=tokenizer.eos_token_id
            )
        
        # Decode the output
        prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # Extract the answer from the prediction
        answer = prediction.split("Answer:")[-1].strip()
        
        response = {
            "model_output": prediction,
            "extracted_answer": answer,
            "full_response": prediction
        }
        
        return response
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/health")
async def health_check():
    return {
        "status": "healthy",
        "model_loaded": model is not None,
        "dataset_loaded": dataset is not None
    }