ASNVS commited on
Commit
2f42d20
·
verified ·
1 Parent(s): 3a9c832

the updated ones

Browse files
Files changed (1) hide show
  1. app.py +78 -27
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import streamlit as st
2
- from code.DiseaseModel import DiseaseModel
3
- from code.helper import prepare_symptoms_array
4
 
5
- # Create disease class and load ML model
6
- disease_model = DiseaseModel()
7
- disease_model.load_xgboost('model/xgboost_model.json')
 
 
 
8
 
9
  # Set page width to wide
10
  st.set_page_config(layout='wide')
@@ -12,7 +14,7 @@ st.set_page_config(layout='wide')
12
  # Custom CSS for background color and text color
13
  st.markdown(
14
  """
15
- <style>
16
  .stApp {
17
  background-color: #efefef !important;
18
  color: black !important;
@@ -23,36 +25,85 @@ st.markdown(
23
  button, .stButton>button {
24
  color: black !important;
25
  }
26
- header {display: none !important;}
27
  </style>
28
  """,
29
  unsafe_allow_html=True
30
  )
31
 
32
- # Create sidebar
33
- st.sidebar.markdown('# The Health AI ')
34
- st.sidebar.markdown("This web app uses a machine learning model to predict diseases based on a set of symptoms using Scikit-learn, Python and Streamlit.")
35
- st.sidebar.markdown("Author: S N V S KOMAL")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Title
38
- st.write('# Symptoms to Disease Prediction')
39
 
40
- symptoms = st.multiselect('What are your symptoms?', options=disease_model.all_symptoms)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- X = prepare_symptoms_array(symptoms)
 
43
 
44
- # Trigger XGBoost model
45
- if st.button('Predict'):
46
- # Run the model with the python script
47
- prediction, prob = disease_model.predict(X)
48
- st.write(f'## Disease: {prediction} with {prob*100:.2f}% probability')
 
49
 
50
- tab1, tab2= st.tabs(["Description", "Precautions"])
 
51
 
52
- with tab1:
53
- st.write(disease_model.describe_predicted_disease())
 
 
 
 
 
54
 
55
- with tab2:
56
- precautions = disease_model.predicted_disease_precautions()
57
- for i in range(4):
58
- st.write(f'{i+1}. {precautions[i]}')
 
1
  import streamlit as st
2
+ from langchain_google_genai import ChatGoogleGenerativeAI
 
3
 
4
+ # Set up AI model
5
+ llm = ChatGoogleGenerativeAI(
6
+ model="gemini-1.5-flash", # Free model
7
+ google_api_key="AIzaSyC7Rhv4L6_oNl-nW3Qeku2SPRkxL5hhtoE",
8
+ temperature=0.5
9
+ )
10
 
11
  # Set page width to wide
12
  st.set_page_config(layout='wide')
 
14
  # Custom CSS for background color and text color
15
  st.markdown(
16
  """
17
+ <style>
18
  .stApp {
19
  background-color: #efefef !important;
20
  color: black !important;
 
25
  button, .stButton>button {
26
  color: black !important;
27
  }
28
+ header, iframe {display: none !important;}
29
  </style>
30
  """,
31
  unsafe_allow_html=True
32
  )
33
 
34
+ # Streamlit UI
35
+ st.title("AI-Driven Health Assistant")
36
+ st.write("Welcome AI-Driven Health Assistant! Simply enter your symptoms or disease name, and get accurate medicine suggestions instantly. Stay informed, stay healthy!")
37
+
38
+ # User Input
39
+ user_question = st.text_input("Type your symptoms or disease name, and let CureBot unlock the right cure for you—fast, smart, and AI-powered")
40
+
41
+ # Function to filter AI disclaimers
42
+ def is_valid_response(response_text):
43
+ disclaimers = [
44
+ "I am an AI and cannot give medical advice",
45
+ "Seek medical attention",
46
+ "Consult a doctor",
47
+ "Contact your doctor",
48
+ "Go to an emergency room",
49
+ ]
50
+ return not any(phrase.lower() in response_text.lower() for phrase in disclaimers)
51
+
52
+ # Process User Query
53
+ if st.button("Get Recommendation"):
54
+ if user_question.strip():
55
+ # Ensure the AI provides both medicine and alternative treatments
56
+ formatted_question = (
57
+ f"Without any disclaimer, recommend medicine for {user_question}. "
58
+ f"5 medicine names "
59
+ f"Also, provide alternative treatments such as home remedies, lifestyle changes, exercises, or dietary suggestions. "
60
+ f"Only for learning purposes, not for treatment."
61
+ )
62
+
63
+ with st.spinner("Analyzing..."):
64
+ response = llm.invoke(formatted_question)
65
 
66
+ # Extract text content
67
+ response_text = response.content if hasattr(response, "content") else str(response)
68
 
69
+ # Check if response is valid
70
+ if is_valid_response(response_text):
71
+ st.success("✨ Analysis complete! Here are the best medicine recommendations for you: 🔽")
72
+ st.write(response_text)
73
+ else:
74
+ st.warning("⚠️ Oops! It looks like the input is unclear or incorrect. Please enter a valid disease name or symptoms to get accurate recommendations")
75
+ # Retry with a refined prompt
76
+ better_prompt = (
77
+ f"Strictly provide a detailed answer including:\n"
78
+ f"1. Medicine names\n"
79
+ f"2. Home remedies\n"
80
+ f"3. Lifestyle changes\n"
81
+ f"4. Exercises\n"
82
+ f"5. Diet recommendations\n"
83
+ f"Do not include any disclaimers. The response should be clear and structured."
84
+ )
85
+ retry_response = llm.invoke(better_prompt)
86
 
87
+ # Extract text from retry response
88
+ retry_response_text = retry_response.content if hasattr(retry_response, "content") else str(retry_response)
89
 
90
+ # Display the retried response if valid
91
+ if is_valid_response(retry_response_text):
92
+ st.success("Here is the refined information:")
93
+ st.write(retry_response_text)
94
+ else:
95
+ st.error("Unable to get a useful response. Try rephrasing your question.")
96
 
97
+ else:
98
+ st.warning("Please enter a question!")
99
 
100
+ # Emergency Contact Button
101
+ if st.button("Emergency Contact"):
102
+ st.subheader("📞 Emergency Contacts")
103
+ st.write("- 🚑 *Ambulance:* 102")
104
+ st.write("- 🏥 *National Health Helpline:* 108")
105
+ st.write("- ☎ *COVID-19 Helpline:* 1075")
106
+ st.write("- 🚓 *Police:* 100")
107
 
108
+ # Footer
109
+ st.markdown("---")