Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,21 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from typing import List, Optional, Dict
|
4 |
-
from llama_cpp import Llama
|
5 |
import gradio as gr
|
6 |
import json
|
7 |
from enum import Enum
|
8 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
class ConsultationState(Enum):
|
11 |
INITIAL = "initial"
|
@@ -23,7 +33,7 @@ class ChatResponse(BaseModel):
|
|
23 |
response: str
|
24 |
finished: bool
|
25 |
|
26 |
-
# Standard health assessment questions
|
27 |
HEALTH_ASSESSMENT_QUESTIONS = [
|
28 |
"What are your current symptoms and how long have you been experiencing them?",
|
29 |
"Do you have any pre-existing medical conditions or chronic illnesses?",
|
@@ -32,7 +42,6 @@ HEALTH_ASSESSMENT_QUESTIONS = [
|
|
32 |
"Have you had any similar symptoms in the past? If yes, what treatments worked?"
|
33 |
]
|
34 |
|
35 |
-
# Personality prompts for Nurse Oge
|
36 |
NURSE_OGE_IDENTITY = """
|
37 |
You are Nurse Oge, a medical AI assistant focused on serving patients in Nigeria. Always be empathetic,
|
38 |
professional, and thorough in your assessments. When asked about your identity, explain that you are
|
@@ -42,14 +51,32 @@ health information before providing any medical advice.
|
|
42 |
|
43 |
class NurseOgeAssistant:
|
44 |
def __init__(self):
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
|
|
|
53 |
def _is_identity_question(self, message: str) -> bool:
|
54 |
identity_patterns = [
|
55 |
r"who are you",
|
@@ -123,16 +150,14 @@ class NurseOgeAssistant:
|
|
123 |
)
|
124 |
else:
|
125 |
self.consultation_states[conversation_id] = ConsultationState.DIAGNOSIS
|
126 |
-
# Prepare complete context for final response
|
127 |
context = "\n".join([
|
128 |
f"Q: {q}\nA: {a}" for q, a in
|
129 |
zip(HEALTH_ASSESSMENT_QUESTIONS, self.gathered_info[conversation_id])
|
130 |
])
|
131 |
|
132 |
-
# Generate final response using the model
|
133 |
messages = [
|
134 |
{"role": "system", "content": NURSE_OGE_IDENTITY},
|
135 |
-
{"role": "user", "content": f"Based on the following patient information, provide a thorough assessment
|
136 |
]
|
137 |
|
138 |
response = self.llm.create_chat_completion(
|
@@ -141,7 +166,6 @@ class NurseOgeAssistant:
|
|
141 |
temperature=0.7
|
142 |
)
|
143 |
|
144 |
-
# Reset state for next consultation
|
145 |
self.consultation_states[conversation_id] = ConsultationState.INITIAL
|
146 |
self.gathered_info[conversation_id] = []
|
147 |
|
@@ -150,22 +174,36 @@ class NurseOgeAssistant:
|
|
150 |
finished=True
|
151 |
)
|
152 |
|
153 |
-
# Initialize FastAPI
|
154 |
app = FastAPI()
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
@app.post("/chat")
|
158 |
async def chat_endpoint(request: ChatRequest):
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
160 |
conversation_id = "default"
|
161 |
|
162 |
-
# Extract the latest message
|
163 |
if not request.messages:
|
164 |
raise HTTPException(status_code=400, detail="No messages provided")
|
165 |
|
166 |
latest_message = request.messages[-1].content
|
167 |
|
168 |
-
# Process the message
|
169 |
response = await nurse_oge.process_message(
|
170 |
conversation_id=conversation_id,
|
171 |
message=latest_message,
|
@@ -174,8 +212,11 @@ async def chat_endpoint(request: ChatRequest):
|
|
174 |
|
175 |
return response
|
176 |
|
177 |
-
#
|
178 |
def gradio_chat(message, history):
|
|
|
|
|
|
|
179 |
response = nurse_oge.process_message("gradio_user", message, history)
|
180 |
return response.response
|
181 |
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from typing import List, Optional, Dict
|
|
|
4 |
import gradio as gr
|
5 |
import json
|
6 |
from enum import Enum
|
7 |
import re
|
8 |
+
import os
|
9 |
+
import time
|
10 |
+
from huggingface_hub import hf_hub_download
|
11 |
+
|
12 |
+
# We'll import llama_cpp in a way that provides better error messages
|
13 |
+
try:
|
14 |
+
from llama_cpp import Llama
|
15 |
+
LLAMA_IMPORT_ERROR = None
|
16 |
+
except Exception as e:
|
17 |
+
LLAMA_IMPORT_ERROR = str(e)
|
18 |
+
print(f"Warning: Failed to import llama_cpp: {e}")
|
19 |
|
20 |
class ConsultationState(Enum):
|
21 |
INITIAL = "initial"
|
|
|
33 |
response: str
|
34 |
finished: bool
|
35 |
|
36 |
+
# Standard health assessment questions
|
37 |
HEALTH_ASSESSMENT_QUESTIONS = [
|
38 |
"What are your current symptoms and how long have you been experiencing them?",
|
39 |
"Do you have any pre-existing medical conditions or chronic illnesses?",
|
|
|
42 |
"Have you had any similar symptoms in the past? If yes, what treatments worked?"
|
43 |
]
|
44 |
|
|
|
45 |
NURSE_OGE_IDENTITY = """
|
46 |
You are Nurse Oge, a medical AI assistant focused on serving patients in Nigeria. Always be empathetic,
|
47 |
professional, and thorough in your assessments. When asked about your identity, explain that you are
|
|
|
51 |
|
52 |
class NurseOgeAssistant:
|
53 |
def __init__(self):
|
54 |
+
if LLAMA_IMPORT_ERROR:
|
55 |
+
raise ImportError(f"Cannot initialize NurseOgeAssistant due to llama_cpp import error: {LLAMA_IMPORT_ERROR}")
|
56 |
+
|
57 |
+
# Download the model file
|
58 |
+
try:
|
59 |
+
model_path = hf_hub_download(
|
60 |
+
repo_id="mradermacher/Llama3-Med42-8B-GGUF",
|
61 |
+
filename="Llama3-Med42-8B.IQ3_M.gguf",
|
62 |
+
resume_download=True
|
63 |
+
)
|
64 |
+
|
65 |
+
# Initialize the model with the downloaded file
|
66 |
+
self.llm = Llama(
|
67 |
+
model_path=model_path,
|
68 |
+
n_ctx=2048, # Context window
|
69 |
+
n_threads=4 # Number of CPU threads to use
|
70 |
+
)
|
71 |
+
|
72 |
+
except Exception as e:
|
73 |
+
raise RuntimeError(f"Failed to initialize the model: {str(e)}")
|
74 |
+
|
75 |
+
self.consultation_states = {}
|
76 |
+
self.gathered_info = {}
|
77 |
|
78 |
+
# ... (rest of the NurseOgeAssistant class methods remain the same)
|
79 |
+
|
80 |
def _is_identity_question(self, message: str) -> bool:
|
81 |
identity_patterns = [
|
82 |
r"who are you",
|
|
|
150 |
)
|
151 |
else:
|
152 |
self.consultation_states[conversation_id] = ConsultationState.DIAGNOSIS
|
|
|
153 |
context = "\n".join([
|
154 |
f"Q: {q}\nA: {a}" for q, a in
|
155 |
zip(HEALTH_ASSESSMENT_QUESTIONS, self.gathered_info[conversation_id])
|
156 |
])
|
157 |
|
|
|
158 |
messages = [
|
159 |
{"role": "system", "content": NURSE_OGE_IDENTITY},
|
160 |
+
{"role": "user", "content": f"Based on the following patient information, provide a thorough assessment and recommendations:\n\n{context}\n\nOriginal query: {message}"}
|
161 |
]
|
162 |
|
163 |
response = self.llm.create_chat_completion(
|
|
|
166 |
temperature=0.7
|
167 |
)
|
168 |
|
|
|
169 |
self.consultation_states[conversation_id] = ConsultationState.INITIAL
|
170 |
self.gathered_info[conversation_id] = []
|
171 |
|
|
|
174 |
finished=True
|
175 |
)
|
176 |
|
177 |
+
# Initialize FastAPI
|
178 |
app = FastAPI()
|
179 |
+
|
180 |
+
# Create a global variable for our assistant
|
181 |
+
nurse_oge = None
|
182 |
+
|
183 |
+
@app.on_event("startup")
|
184 |
+
async def startup_event():
|
185 |
+
global nurse_oge
|
186 |
+
try:
|
187 |
+
nurse_oge = NurseOgeAssistant()
|
188 |
+
except Exception as e:
|
189 |
+
print(f"Failed to initialize NurseOgeAssistant: {e}")
|
190 |
+
# We'll continue running but the /chat endpoint will return errors
|
191 |
|
192 |
@app.post("/chat")
|
193 |
async def chat_endpoint(request: ChatRequest):
|
194 |
+
if nurse_oge is None:
|
195 |
+
raise HTTPException(
|
196 |
+
status_code=503,
|
197 |
+
detail="The medical assistant is not available at the moment. Please try again later."
|
198 |
+
)
|
199 |
+
|
200 |
conversation_id = "default"
|
201 |
|
|
|
202 |
if not request.messages:
|
203 |
raise HTTPException(status_code=400, detail="No messages provided")
|
204 |
|
205 |
latest_message = request.messages[-1].content
|
206 |
|
|
|
207 |
response = await nurse_oge.process_message(
|
208 |
conversation_id=conversation_id,
|
209 |
message=latest_message,
|
|
|
212 |
|
213 |
return response
|
214 |
|
215 |
+
# Gradio interface
|
216 |
def gradio_chat(message, history):
|
217 |
+
if nurse_oge is None:
|
218 |
+
return "The medical assistant is not available at the moment. Please try again later."
|
219 |
+
|
220 |
response = nurse_oge.process_message("gradio_user", message, history)
|
221 |
return response.response
|
222 |
|