Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
from config import MODEL_PATH, ENCODER_DIR, OPENAI_API_KEY, OPENAI_BASE_URL
|
5 |
+
from utils import load_model, load_label_encoders
|
6 |
+
from prediction import predict_susceptibility
|
7 |
+
from ai_assistant import initialize_openai_client, get_ai_response
|
8 |
+
|
9 |
+
# Load assets
|
10 |
+
model = load_model(MODEL_PATH)
|
11 |
+
encoders = load_label_encoders(ENCODER_DIR)
|
12 |
+
|
13 |
+
# Initialize OpenAI client
|
14 |
+
client = initialize_openai_client(OPENAI_API_KEY, OPENAI_BASE_URL)
|
15 |
+
|
16 |
+
# Streamlit UI
|
17 |
+
st.set_page_config(page_title="Microbial Susceptibility Analyzer", layout="wide")
|
18 |
+
|
19 |
+
st.sidebar.title("Navigation")
|
20 |
+
page = st.sidebar.radio("Go to", ["Home", "Susceptibility Analysis", "Data Upload", "About"])
|
21 |
+
|
22 |
+
# Home Page
|
23 |
+
if page == "Home":
|
24 |
+
st.title("Microbial Susceptibility Analyzer")
|
25 |
+
st.image("bacteria.jpeg", use_container_width=True)
|
26 |
+
st.markdown("""
|
27 |
+
**Welcome to the Microbial Susceptibility Analyzer!**
|
28 |
+
This app helps analyze **antibiotic resistance** using **machine learning and rule-based decisions**.
|
29 |
+
- Predict microbial susceptibility.
|
30 |
+
- Ask an AI assistant for expert advice.
|
31 |
+
- Upload datasets for batch predictions.
|
32 |
+
""")
|
33 |
+
|
34 |
+
# Susceptibility Analysis Page
|
35 |
+
elif page == "Susceptibility Analysis":
|
36 |
+
st.title("Susceptibility Prediction")
|
37 |
+
|
38 |
+
# Initialize session state for messages if not exists
|
39 |
+
if 'messages' not in st.session_state:
|
40 |
+
st.session_state.messages = []
|
41 |
+
|
42 |
+
# Create two columns for layout
|
43 |
+
col1, col2 = st.columns([1, 1])
|
44 |
+
|
45 |
+
with col1:
|
46 |
+
with st.form("prediction_form"):
|
47 |
+
organism = st.selectbox('Organism', options=encoders['organism'].keys())
|
48 |
+
antibiotic = st.selectbox('Antibiotic', options=encoders['antibiotic'].keys())
|
49 |
+
was_positive = st.selectbox('Was Positive', options=[1, 0])
|
50 |
+
submit_button = st.form_submit_button("Predict")
|
51 |
+
|
52 |
+
if submit_button:
|
53 |
+
# Store inputs in session state
|
54 |
+
st.session_state['current_organism'] = organism
|
55 |
+
st.session_state['current_antibiotic'] = antibiotic
|
56 |
+
st.session_state['current_was_positive'] = was_positive
|
57 |
+
|
58 |
+
result = predict_susceptibility({
|
59 |
+
'was_positive': was_positive,
|
60 |
+
'organism': organism,
|
61 |
+
'antibiotic': antibiotic
|
62 |
+
}, model, encoders)
|
63 |
+
|
64 |
+
st.subheader("Prediction Results")
|
65 |
+
if "Error" in result:
|
66 |
+
st.error(result["Error"])
|
67 |
+
else:
|
68 |
+
st.write(f"**Final Decision:** {result['Final Output']}")
|
69 |
+
st.write(f"**Rule-Based Guidance:** {result['Rule Guidance']}")
|
70 |
+
st.write(f"**Model Prediction:** {result['Model Prediction']}")
|
71 |
+
st.write(f"**Decision Explanation:** {result['Decision Reason']}")
|
72 |
+
|
73 |
+
# Clear previous messages when new prediction is made
|
74 |
+
st.session_state.messages = []
|
75 |
+
|
76 |
+
with col2:
|
77 |
+
st.subheader("DeepSeek AI Assistant")
|
78 |
+
|
79 |
+
# Only show assistant if a prediction has been made
|
80 |
+
if 'current_organism' in st.session_state:
|
81 |
+
st.markdown(f"Ask about **{st.session_state.get('current_organism')}** and **{st.session_state.get('current_antibiotic')}**:")
|
82 |
+
|
83 |
+
# Example prompts as buttons
|
84 |
+
example_prompts = [
|
85 |
+
"Explain why this combination might show resistance",
|
86 |
+
"Suggest alternative antibiotics for this organism",
|
87 |
+
"What resistance mechanisms are common here?",
|
88 |
+
"How should this result influence treatment decisions?"
|
89 |
+
]
|
90 |
+
|
91 |
+
# Create a unique key for each button
|
92 |
+
for i, prompt in enumerate(example_prompts):
|
93 |
+
if st.button(prompt, key=f"prompt_{i}"):
|
94 |
+
# Create context-enhanced prompt
|
95 |
+
enhanced_prompt = f"For organism {st.session_state.get('current_organism')} " \
|
96 |
+
f"and antibiotic {st.session_state.get('current_antibiotic')}: {prompt}"
|
97 |
+
response = get_ai_response(client, enhanced_prompt)
|
98 |
+
|
99 |
+
# Display the user prompt and AI response
|
100 |
+
st.chat_message("user").markdown(prompt)
|
101 |
+
st.chat_message("assistant").markdown(response)
|
102 |
+
|
103 |
+
# Chat input
|
104 |
+
user_prompt = st.chat_input("Ask about this result...")
|
105 |
+
if user_prompt:
|
106 |
+
# Create context-enhanced prompt
|
107 |
+
enhanced_prompt = f"For organism {st.session_state.get('current_organism')} " \
|
108 |
+
f"and antibiotic {st.session_state.get('current_antibiotic')}: {user_prompt}"
|
109 |
+
response = get_ai_response(client, enhanced_prompt)
|
110 |
+
|
111 |
+
# Display the user prompt and AI response
|
112 |
+
st.chat_message("user").markdown(user_prompt)
|
113 |
+
st.chat_message("assistant").markdown(response)
|
114 |
+
else:
|
115 |
+
st.info("Make a prediction first to get specific AI assistance")
|
116 |
+
|
117 |
+
# Data Upload Page
|
118 |
+
elif page == "Data Upload":
|
119 |
+
st.title("Batch Prediction: Upload CSV")
|
120 |
+
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
|
121 |
+
|
122 |
+
if uploaded_file:
|
123 |
+
df = pd.read_csv(uploaded_file)
|
124 |
+
st.write("Uploaded Data Preview:", df.head())
|
125 |
+
|
126 |
+
if st.button("Predict for Dataset"):
|
127 |
+
df["Prediction"] = df.apply(lambda row: predict_susceptibility(row.to_dict(), model, encoders)["Final Output"], axis=1)
|
128 |
+
st.write("Prediction Results", df)
|
129 |
+
|
130 |
+
# About Page
|
131 |
+
elif page == "About":
|
132 |
+
st.title("About this App")
|
133 |
+
st.markdown("""
|
134 |
+
- Developed by **Anulunko Chukwuebuka**
|
135 |
+
- Uses **Machine Learning & Rule-based AI**
|
136 |
+
- Integrated with **DeepSeek AI** for advanced queries
|
137 |
+
- Contact: [email protected]
|
138 |
+
""")
|