File size: 12,094 Bytes
b1de9b2
a444494
 
 
 
 
 
c8d430c
 
b1de9b2
e53bd9c
c8d430c
94dc8bb
c8d430c
94dc8bb
e53bd9c
b1de9b2
94dc8bb
b1de9b2
 
e53bd9c
a444494
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e53bd9c
a444494
 
 
 
 
 
 
 
e53bd9c
a444494
 
 
 
 
 
 
 
e53bd9c
 
 
a444494
c8d430c
e53bd9c
 
b1de9b2
 
 
94dc8bb
e53bd9c
c8d430c
 
 
 
 
e53bd9c
c8d430c
 
a444494
 
e53bd9c
a444494
 
 
 
 
 
 
 
 
 
e53bd9c
a444494
 
 
 
 
 
 
 
 
 
e53bd9c
a444494
 
 
 
 
 
 
 
 
e53bd9c
 
 
b1de9b2
 
 
 
a444494
b1de9b2
 
 
 
 
 
 
 
a444494
b1de9b2
 
 
 
 
 
 
a444494
b1de9b2
 
 
 
a444494
b1de9b2
 
a444494
 
b1de9b2
 
 
 
 
a444494
b1de9b2
 
 
 
 
 
 
94dc8bb
b1de9b2
 
 
 
 
94dc8bb
b1de9b2
 
e53bd9c
b1de9b2
 
94dc8bb
b1de9b2
 
 
 
 
 
 
94dc8bb
 
 
 
b1de9b2
 
 
 
 
 
 
 
 
 
 
94dc8bb
b1de9b2
 
 
 
 
 
 
 
 
 
 
 
 
a444494
e53bd9c
 
 
 
 
 
 
 
 
 
 
 
c8d430c
e53bd9c
 
c8d430c
b1de9b2
 
 
e53bd9c
94dc8bb
b1de9b2
94dc8bb
b1de9b2
 
94dc8bb
b1de9b2
 
e53bd9c
b1de9b2
a444494
94dc8bb
a444494
 
e53bd9c
c8d430c
 
 
 
 
 
a444494
 
 
 
 
 
b1de9b2
a444494
 
 
 
 
 
94dc8bb
e53bd9c
 
c8d430c
 
 
e53bd9c
a444494
 
e53bd9c
a444494
 
94dc8bb
 
 
 
 
 
 
 
 
 
 
e53bd9c
a444494
 
94dc8bb
 
 
 
 
 
 
 
 
 
 
 
a444494
 
 
94dc8bb
a444494
 
 
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from typing import List, Optional, Dict
import gradio as gr
import json
from enum import Enum
import re
import os
import time
import gc
from contextlib import asynccontextmanager
from huggingface_hub import hf_hub_download
from llama_cpp import Llama

# Configuration variables that can be set through environment variables
# These allow for flexible deployment configuration without code changes
MODEL_REPO_ID = os.getenv("MODEL_REPO_ID", "mradermacher/Llama3-Med42-8B-GGUF")
MODEL_FILENAME = os.getenv("MODEL_FILENAME", "Llama3-Med42-8B.Q5_K_M.gguf")
N_THREADS = int(os.getenv("N_THREADS", "4"))

# Data models for API request/response handling
class ConsultationState(Enum):
    INITIAL = "initial"
    GATHERING_INFO = "gathering_info"
    DIAGNOSIS = "diagnosis"

class Message(BaseModel):
    role: str
    content: str

class ChatRequest(BaseModel):
    messages: List[Message]

class ChatResponse(BaseModel):
    response: str
    finished: bool

# Standardized health assessment questions for consistent patient evaluation
HEALTH_ASSESSMENT_QUESTIONS = [
    "What are your current symptoms and how long have you been experiencing them?",
    "Do you have any pre-existing medical conditions or chronic illnesses?",
    "Are you currently taking any medications? If yes, please list them.",
    "Is there any relevant family medical history I should know about?",
    "Have you had any similar symptoms in the past? If yes, what treatments worked?"
]

# AI assistant's identity and role definition
NURSE_OGE_IDENTITY = """
You are Nurse Oge, a medical AI assistant focused on serving patients in Nigeria. Always be empathetic, 
professional, and thorough in your assessments. When asked about your identity, explain that you are 
Nurse Oge, a medical AI assistant serving Nigerian communities. Remember that you must gather complete 
health information before providing any medical advice.
"""

class NurseOgeAssistant:
    """
    Main assistant class that handles conversation management and medical consultations
    """
    def __init__(self):
        try:
            # Initialize the Llama model using from_pretrained as per documentation
            self.llm = Llama.from_pretrained(
                repo_id=MODEL_REPO_ID,
                filename=MODEL_FILENAME,
                n_ctx=2048,      # Context window size
                n_threads=N_THREADS,  # CPU threads to use
                n_gpu_layers=0   # CPU-only inference
            )
            
        except Exception as e:
            raise RuntimeError(f"Failed to initialize the model: {str(e)}")
            
        # State management for multiple concurrent conversations
        self.consultation_states = {}
        self.gathered_info = {}

    def _is_identity_question(self, message: str) -> bool:
        """Detect if the user is asking about the assistant's identity"""
        identity_patterns = [
            r"who are you",
            r"what are you",
            r"your name",
            r"what should I call you",
            r"tell me about yourself"
        ]
        return any(re.search(pattern, message.lower()) for pattern in identity_patterns)

    def _is_location_question(self, message: str) -> bool:
        """Detect if the user is asking about the assistant's location"""
        location_patterns = [
            r"where are you",
            r"which country",
            r"your location",
            r"where do you work",
            r"where are you based"
        ]
        return any(re.search(pattern, message.lower()) for pattern in location_patterns)

    def _get_next_assessment_question(self, conversation_id: str) -> Optional[str]:
        """Get the next health assessment question based on conversation progress"""
        if conversation_id not in self.gathered_info:
            self.gathered_info[conversation_id] = []
        
        questions_asked = len(self.gathered_info[conversation_id])
        if questions_asked < len(HEALTH_ASSESSMENT_QUESTIONS):
            return HEALTH_ASSESSMENT_QUESTIONS[questions_asked]
        return None

    async def process_message(self, conversation_id: str, message: str, history: List[Dict]) -> ChatResponse:
        """
        Process incoming messages and manage the conversation flow
        """
        try:
            # Initialize state for new conversations
            if conversation_id not in self.consultation_states:
                self.consultation_states[conversation_id] = ConsultationState.INITIAL

            # Handle identity questions
            if self._is_identity_question(message):
                return ChatResponse(
                    response="I am Nurse Oge, a medical AI assistant dedicated to helping patients in Nigeria. "
                            "I'm here to provide medical guidance while ensuring I gather all necessary health information "
                            "for accurate assessments.",
                    finished=True
                )

            # Handle location questions
            if self._is_location_question(message):
                return ChatResponse(
                    response="I am based in Nigeria and specifically trained to serve Nigerian communities, "
                            "taking into account local healthcare contexts and needs.",
                    finished=True
                )

            # Start health assessment for medical queries
            if self.consultation_states[conversation_id] == ConsultationState.INITIAL:
                self.consultation_states[conversation_id] = ConsultationState.GATHERING_INFO
                next_question = self._get_next_assessment_question(conversation_id)
                return ChatResponse(
                    response=f"Before I can provide any medical advice, I need to gather some important health information. "
                            f"{next_question}",
                    finished=False
                )

            # Continue gathering information
            if self.consultation_states[conversation_id] == ConsultationState.GATHERING_INFO:
                self.gathered_info[conversation_id].append(message)
                next_question = self._get_next_assessment_question(conversation_id)
                
                if next_question:
                    return ChatResponse(
                        response=f"Thank you for that information. {next_question}",
                        finished=False
                    )
                else:
                    self.consultation_states[conversation_id] = ConsultationState.DIAGNOSIS
                    # Prepare context from gathered information
                    context = "\n".join([
                        f"Q: {q}\nA: {a}" for q, a in 
                        zip(HEALTH_ASSESSMENT_QUESTIONS, self.gathered_info[conversation_id])
                    ])
                    
                    # Prepare messages for the model
                    messages = [
                        {"role": "system", "content": NURSE_OGE_IDENTITY},
                        {"role": "user", "content": f"Based on the following patient information, provide thorough assessment, diagnosis and recommendations:\n\n{context}\n\nOriginal query: {message}"}
                    ]
                    
                    # Implement retry logic for model inference
                    max_retries = 3
                    retry_delay = 2
                    
                    for attempt in range(max_retries):
                        try:
                            response = self.llm.create_chat_completion(
                                messages=messages,
                                max_tokens=512,
                                temperature=0.7,
                                top_p=0.95,
                                stop=["</s>"]
                            )
                            break
                        except Exception as e:
                            if attempt < max_retries - 1:
                                time.sleep(retry_delay)
                                continue
                            return ChatResponse(
                                response="I'm sorry, I'm experiencing some technical difficulties. Please try again in a moment.",
                                finished=True
                            )
                    
                    # Reset conversation state
                    self.consultation_states[conversation_id] = ConsultationState.INITIAL
                    self.gathered_info[conversation_id] = []
                    
                    return ChatResponse(
                        response=response['choices'][0]['message']['content'],
                        finished=True
                    )
                    
        except Exception as e:
            return ChatResponse(
                response=f"An error occurred while processing your request. Please try again.",
                finished=True
            )

# Define FastAPI lifespan for startup/shutdown events
@asynccontextmanager
async def lifespan(app: FastAPI):
    # Initialize on startup
    global nurse_oge
    try:
        nurse_oge = NurseOgeAssistant()
    except Exception as e:
        print(f"Failed to initialize NurseOgeAssistant: {e}")
    yield
    # Clean up on shutdown if needed
    # Add cleanup code here

# Initialize FastAPI with lifespan
app = FastAPI(lifespan=lifespan)

# Add memory management middleware
@app.middleware("http")
async def add_memory_management(request: Request, call_next):
    """Middleware to help manage memory usage"""
    gc.collect()
    response = await call_next(request)
    gc.collect()
    return response

# Health check endpoint
@app.get("/health")
async def health_check():
    """Endpoint to verify service health"""
    return {"status": "healthy", "model_loaded": nurse_oge is not None}

# Chat endpoint
@app.post("/chat")
async def chat_endpoint(request: ChatRequest):
    """Main chat endpoint for API interactions"""
    if nurse_oge is None:
        raise HTTPException(
            status_code=503,
            detail="The medical assistant is not available at the moment. Please try again later."
        )
    
    if not request.messages:
        raise HTTPException(status_code=400, detail="No messages provided")
    
    latest_message = request.messages[-1].content
    
    response = await nurse_oge.process_message(
        conversation_id="default",
        message=latest_message,
        history=request.messages[:-1]
    )
    
    return response

# Gradio chat interface function
async def gradio_chat(message, history):
    """Handler for Gradio chat interface"""
    if nurse_oge is None:
        return "The medical assistant is not available at the moment. Please try again later."
    
    response = await nurse_oge.process_message("gradio_user", message, history)
    return response.response

# Create and configure Gradio interface
demo = gr.ChatInterface(
    fn=gradio_chat,
    title="Nurse Oge - Medical Assistant",
    description="""Welcome to Nurse Oge, your AI medical assistant specialized in serving Nigerian communities. 
                   This system provides medical guidance while ensuring comprehensive health information gathering.""",
    examples=[
        ["What are the common symptoms of malaria?"],
        ["I've been having headaches for the past week"],
        ["How can I prevent typhoid fever?"],
    ],
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="purple",
    )
)

# Add custom CSS for better appearance
demo.css = """
    .gradio-container {
        font-family: 'Arial', sans-serif;
    }
    .chat-message {
        padding: 1rem;
        border-radius: 0.5rem;
        margin-bottom: 0.5rem;
    }
"""

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

# Run the application
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)