Spaces:
Sleeping
Sleeping
Upload lassa_pred.py
Browse files- lassa_pred.py +106 -0
lassa_pred.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 4 |
+
import joblib
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load the trained classifier model
|
| 8 |
+
model = joblib.load('model_pkl')
|
| 9 |
+
|
| 10 |
+
# OpenAI API key (assumed to be set in Hugging Face environment)
|
| 11 |
+
openai.api_key = 'sk-jtt46VTrnym9B57gkkS_QAJ2E0RYodrEDHodAgliVqT3BlbkFJKooyGzBYbWkWkIt4-MG6ASNt4pMguQs2Mob6kpkaEA'
|
| 12 |
+
|
| 13 |
+
# Function to simulate the medical assistant's interaction
|
| 14 |
+
def medical_assistant_interaction(pulse_rate, blood_pressure_systolic, blood_pressure_diastolic, temperature_celsius,
|
| 15 |
+
headache, muscle_pain, sore_throat, nausea_vomiting, diarrhea, joint_pains, cough):
|
| 16 |
+
try:
|
| 17 |
+
# Prepare symptom responses
|
| 18 |
+
symptom_responses = {
|
| 19 |
+
"Headache": headache,
|
| 20 |
+
"Muscle Pain": muscle_pain,
|
| 21 |
+
"Sore Throat": sore_throat,
|
| 22 |
+
"Nausea and Vomiting": nausea_vomiting,
|
| 23 |
+
"Diarrhea": diarrhea,
|
| 24 |
+
"Joint Pains": joint_pains,
|
| 25 |
+
"Dry Cough or Coughing Blood": cough
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Combine responses into a single text input
|
| 29 |
+
patient_responses = ', '.join([f"{symptom}: {response}" for symptom, response in symptom_responses.items()])
|
| 30 |
+
|
| 31 |
+
# Compile sensor data for the classifier
|
| 32 |
+
features = pd.DataFrame({
|
| 33 |
+
'Pulse_Rate': [pulse_rate],
|
| 34 |
+
'Blood_Pressure_Systolic': [blood_pressure_systolic],
|
| 35 |
+
'Blood_Pressure_Diastolic': [blood_pressure_diastolic],
|
| 36 |
+
'Temperature_Celsius': [temperature_celsius]
|
| 37 |
+
})
|
| 38 |
+
|
| 39 |
+
# Make a prediction using the trained classifier
|
| 40 |
+
prediction = model.predict(features)[0]
|
| 41 |
+
|
| 42 |
+
# Use OpenAI to make a combined inference
|
| 43 |
+
response_text = f"""
|
| 44 |
+
A patient has the following sensor readings:
|
| 45 |
+
- Pulse Rate: {pulse_rate} bpm
|
| 46 |
+
- Systolic Blood Pressure: {blood_pressure_systolic} mmHg
|
| 47 |
+
- Diastolic Blood Pressure: {blood_pressure_diastolic} mmHg
|
| 48 |
+
- Temperature: {temperature_celsius}°C
|
| 49 |
+
|
| 50 |
+
The patient reported the following symptoms: {patient_responses}.
|
| 51 |
+
|
| 52 |
+
Based on these readings and symptoms, what is the likelihood of Lassa fever? Provide additional follow-up questions if necessary.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
# Call OpenAI's API for a response
|
| 56 |
+
response = openai.Completion.create(
|
| 57 |
+
engine="text-davinci-003",
|
| 58 |
+
prompt=response_text,
|
| 59 |
+
max_tokens=150
|
| 60 |
+
)
|
| 61 |
+
assistant_message = response['choices'][0]['text'].strip()
|
| 62 |
+
|
| 63 |
+
# Combine the assistant's message with the model's prediction
|
| 64 |
+
if prediction == 1:
|
| 65 |
+
model_inference = "Based on the sensor readings, the classifier suggests a high likelihood of Lassa fever."
|
| 66 |
+
else:
|
| 67 |
+
model_inference = "Based on the sensor readings, the classifier suggests a low likelihood of Lassa fever."
|
| 68 |
+
|
| 69 |
+
final_inference = f"{assistant_message}\n\n{model_inference}"
|
| 70 |
+
|
| 71 |
+
return final_inference
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return f"An error occurred: {str(e)}"
|
| 75 |
+
|
| 76 |
+
# Gradio interface for interaction
|
| 77 |
+
def gradio_interface(pulse_rate, blood_pressure_systolic, blood_pressure_diastolic, temperature_celsius,
|
| 78 |
+
headache, muscle_pain, sore_throat, nausea_vomiting, diarrhea, joint_pains, cough):
|
| 79 |
+
return medical_assistant_interaction(
|
| 80 |
+
pulse_rate, blood_pressure_systolic, blood_pressure_diastolic, temperature_celsius,
|
| 81 |
+
headache, muscle_pain, sore_throat, nausea_vomiting, diarrhea, joint_pains, cough
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Create a Gradio interface
|
| 85 |
+
interface = gr.Interface(
|
| 86 |
+
fn=gradio_interface,
|
| 87 |
+
inputs=[
|
| 88 |
+
gr.Number(label="Pulse Rate (bpm)"),
|
| 89 |
+
gr.Number(label="Systolic Blood Pressure (mmHg)"),
|
| 90 |
+
gr.Number(label="Diastolic Blood Pressure (mmHg)"),
|
| 91 |
+
gr.Number(label="Temperature (Celsius)"),
|
| 92 |
+
gr.Radio(choices=["Yes", "No"], label="Are you experiencing headaches?"),
|
| 93 |
+
gr.Radio(choices=["Yes", "No"], label="Are you experiencing muscle pain?"),
|
| 94 |
+
gr.Radio(choices=["Yes", "No"], label="Are you experiencing a sore throat?"),
|
| 95 |
+
gr.Radio(choices=["Yes", "No"], label="Are you experiencing nausea and vomiting?"),
|
| 96 |
+
gr.Radio(choices=["Yes", "No"], label="Are you experiencing diarrhea?"),
|
| 97 |
+
gr.Radio(choices=["Yes", "No"], label="Are you experiencing joint pains?"),
|
| 98 |
+
gr.Radio(choices=["Yes", "No"], label="Are you experiencing a dry cough or coughing blood?")
|
| 99 |
+
],
|
| 100 |
+
outputs="text",
|
| 101 |
+
title="Lassa Fever Medical Assistant",
|
| 102 |
+
description="This assistant uses sensor data and patient symptom responses to infer the likelihood of Lassa fever."
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Launch the Gradio interface
|
| 106 |
+
interface.launch()
|