Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,106 @@
|
|
| 1 |
-
import
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
else:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
else:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# β
Load IBM Granite model with cache to speed up
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model_id = "ibm-granite/granite-3.3-2b-instruct"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 11 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 12 |
+
|
| 13 |
+
generator = load_model()
|
| 14 |
+
|
| 15 |
+
# β
Set Streamlit page configuration
|
| 16 |
+
st.title("π©Ί HealthAI β Intelligent Healthcare Assistant")
|
| 17 |
+
|
| 18 |
+
# β
Define tabs
|
| 19 |
+
tab1, tab2, tab3, tab4 = st.tabs([
|
| 20 |
+
"π§ Patient Chat", "π§Ύ Disease Prediction",
|
| 21 |
+
"π Treatment Plans", "π Health Analytics"
|
| 22 |
+
])
|
| 23 |
+
|
| 24 |
+
# ------------------------------
|
| 25 |
+
# π§ TAB 1: Patient Chat
|
| 26 |
+
# ------------------------------
|
| 27 |
+
with tab1:
|
| 28 |
+
st.subheader("Ask any health-related question")
|
| 29 |
+
query = st.text_area("Enter your question here")
|
| 30 |
+
|
| 31 |
+
if st.button("Get Advice", key="chat"):
|
| 32 |
+
if query.strip() == "":
|
| 33 |
+
st.warning("Please enter a question.")
|
| 34 |
else:
|
| 35 |
+
with st.spinner("Thinking..."):
|
| 36 |
+
response = generator(query, max_new_tokens=200)[0]["generated_text"]
|
| 37 |
+
st.success("AI Response:")
|
| 38 |
+
st.markdown(f"markdown\n{response}\n")
|
| 39 |
+
|
| 40 |
+
# ------------------------------
|
| 41 |
+
# π§Ύ TAB 2: Disease Prediction
|
| 42 |
+
# ------------------------------
|
| 43 |
+
with tab2:
|
| 44 |
+
st.subheader("Enter your symptoms (comma-separated)")
|
| 45 |
+
symptoms = st.text_input("E.g. persistent fever, fatigue, dry cough")
|
| 46 |
+
|
| 47 |
+
if st.button("AI Diagnose", key="predict"):
|
| 48 |
+
if symptoms.strip() == "":
|
| 49 |
+
st.warning("Please enter your symptoms.")
|
| 50 |
else:
|
| 51 |
+
prompt = (
|
| 52 |
+
f"I am feeling unwell. My symptoms are: {symptoms}.\n"
|
| 53 |
+
"Can you please suggest what possible conditions I might have based on this?\n"
|
| 54 |
+
"List top 3 possible diseases with a short reason for each, and give a seriousness score out of 10."
|
| 55 |
+
)
|
| 56 |
+
with st.spinner("Analyzing symptoms..."):
|
| 57 |
+
result = generator(prompt, max_new_tokens=300, do_sample=True)[0]['generated_text']
|
| 58 |
+
st.success("AI Prediction:")
|
| 59 |
+
st.markdown(f"markdown\n{result}\n")
|
| 60 |
+
|
| 61 |
+
# ------------------------------
|
| 62 |
+
# π TAB 3: Treatment Plans
|
| 63 |
+
# ------------------------------
|
| 64 |
+
with tab3:
|
| 65 |
+
st.header("π Treatment Plan Generator")
|
| 66 |
+
condition = st.text_input("Enter the known condition (e.g., Asthma, Diabetes)")
|
| 67 |
+
|
| 68 |
+
if st.button("Get Full Treatment Plan"):
|
| 69 |
+
if not condition.strip():
|
| 70 |
+
st.warning("Please enter a condition.")
|
| 71 |
+
else:
|
| 72 |
+
with st.spinner("Generating treatment plan..."):
|
| 73 |
+
|
| 74 |
+
def get_response(prompt):
|
| 75 |
+
return generator(prompt, max_new_tokens=1000, temperature=0.7, do_sample=True)[0]['generated_text'].strip()
|
| 76 |
+
|
| 77 |
+
prompts = {
|
| 78 |
+
"1οΈβ£ Medications": f"What medications are usually prescribed for {condition}?",
|
| 79 |
+
"2οΈβ£ Diet": f"What diet is recommended for someone with {condition}?",
|
| 80 |
+
"3οΈβ£ Exercise": f"What type of physical activities should a person with {condition} follow?",
|
| 81 |
+
"4οΈβ£ Follow-Up & Monitoring": f"What follow-up steps and monitoring should be done for {condition}?",
|
| 82 |
+
"5οΈβ£ Precautions": f"What precautions should be taken by someone with {condition}?",
|
| 83 |
+
"6οΈβ£ Mental Health & Stress": f"How can someone with {condition} manage stress and mental health?"
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
for section, prompt in prompts.items():
|
| 87 |
+
st.subheader(section)
|
| 88 |
+
st.markdown(f"markdown\n{get_response(prompt)}\n")
|
| 89 |
+
|
| 90 |
+
# ------------------------------
|
| 91 |
+
# π TAB 4: Health Analytics
|
| 92 |
+
# ------------------------------
|
| 93 |
+
with tab4:
|
| 94 |
+
st.subheader("Track your health data over time")
|
| 95 |
+
uploaded = st.file_uploader("Upload your CSV file (with columns like 'blood_pressure', 'heart_rate')", type=["csv"])
|
| 96 |
+
|
| 97 |
+
if uploaded:
|
| 98 |
+
df = pd.read_csv(uploaded)
|
| 99 |
+
st.dataframe(df)
|
| 100 |
+
|
| 101 |
+
for col in df.select_dtypes(include=['float', 'int']).columns:
|
| 102 |
+
st.line_chart(df[col])
|
| 103 |
+
if df[col].mean() > df[col].iloc[-1]:
|
| 104 |
+
st.info(f"π {col} is improving.")
|
| 105 |
+
else:
|
| 106 |
+
st.warning(f"π {col} is rising β consider medical advice.")
|