File size: 4,779 Bytes
f064541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import joblib
import gradio as gr

# Load the trained classifier model
model = joblib.load('model_pkl')

# OpenAI API key (assumed to be set in Hugging Face environment)
openai.api_key = 'sk-jtt46VTrnym9B57gkkS_QAJ2E0RYodrEDHodAgliVqT3BlbkFJKooyGzBYbWkWkIt4-MG6ASNt4pMguQs2Mob6kpkaEA'

# Function to simulate the medical assistant's interaction
def medical_assistant_interaction(pulse_rate, blood_pressure_systolic, blood_pressure_diastolic, temperature_celsius, 

                                  headache, muscle_pain, sore_throat, nausea_vomiting, diarrhea, joint_pains, cough):
    try:
        # Prepare symptom responses
        symptom_responses = {
            "Headache": headache,
            "Muscle Pain": muscle_pain,
            "Sore Throat": sore_throat,
            "Nausea and Vomiting": nausea_vomiting,
            "Diarrhea": diarrhea,
            "Joint Pains": joint_pains,
            "Dry Cough or Coughing Blood": cough
        }

        # Combine responses into a single text input
        patient_responses = ', '.join([f"{symptom}: {response}" for symptom, response in symptom_responses.items()])

        # Compile sensor data for the classifier
        features = pd.DataFrame({
            'Pulse_Rate': [pulse_rate],
            'Blood_Pressure_Systolic': [blood_pressure_systolic],
            'Blood_Pressure_Diastolic': [blood_pressure_diastolic],
            'Temperature_Celsius': [temperature_celsius]
        })

        # Make a prediction using the trained classifier
        prediction = model.predict(features)[0]

        # Use OpenAI to make a combined inference
        response_text = f"""

        A patient has the following sensor readings:

        - Pulse Rate: {pulse_rate} bpm

        - Systolic Blood Pressure: {blood_pressure_systolic} mmHg

        - Diastolic Blood Pressure: {blood_pressure_diastolic} mmHg

        - Temperature: {temperature_celsius}°C



        The patient reported the following symptoms: {patient_responses}.

        

        Based on these readings and symptoms, what is the likelihood of Lassa fever? Provide additional follow-up questions if necessary.

        """

        # Call OpenAI's API for a response
        response = openai.Completion.create(
            engine="text-davinci-003",
            prompt=response_text,
            max_tokens=150
        )
        assistant_message = response['choices'][0]['text'].strip()

        # Combine the assistant's message with the model's prediction
        if prediction == 1:
            model_inference = "Based on the sensor readings, the classifier suggests a high likelihood of Lassa fever."
        else:
            model_inference = "Based on the sensor readings, the classifier suggests a low likelihood of Lassa fever."

        final_inference = f"{assistant_message}\n\n{model_inference}"
        
        return final_inference

    except Exception as e:
        return f"An error occurred: {str(e)}"

# Gradio interface for interaction
def gradio_interface(pulse_rate, blood_pressure_systolic, blood_pressure_diastolic, temperature_celsius, 

                     headache, muscle_pain, sore_throat, nausea_vomiting, diarrhea, joint_pains, cough):
    return medical_assistant_interaction(
        pulse_rate, blood_pressure_systolic, blood_pressure_diastolic, temperature_celsius, 
        headache, muscle_pain, sore_throat, nausea_vomiting, diarrhea, joint_pains, cough
    )

# Create a Gradio interface
interface = gr.Interface(
    fn=gradio_interface,
    inputs=[
        gr.Number(label="Pulse Rate (bpm)"),
        gr.Number(label="Systolic Blood Pressure (mmHg)"),
        gr.Number(label="Diastolic Blood Pressure (mmHg)"),
        gr.Number(label="Temperature (Celsius)"),
        gr.Radio(choices=["Yes", "No"], label="Are you experiencing headaches?"),
        gr.Radio(choices=["Yes", "No"], label="Are you experiencing muscle pain?"),
        gr.Radio(choices=["Yes", "No"], label="Are you experiencing a sore throat?"),
        gr.Radio(choices=["Yes", "No"], label="Are you experiencing nausea and vomiting?"),
        gr.Radio(choices=["Yes", "No"], label="Are you experiencing diarrhea?"),
        gr.Radio(choices=["Yes", "No"], label="Are you experiencing joint pains?"),
        gr.Radio(choices=["Yes", "No"], label="Are you experiencing a dry cough or coughing blood?")
    ],
    outputs="text",
    title="Lassa Fever Medical Assistant",
    description="This assistant uses sensor data and patient symptom responses to infer the likelihood of Lassa fever."
)

# Launch the Gradio interface
interface.launch()