student-abdullah commited on
Commit
11c5475
1 Parent(s): 0a6fc2c

Update a.py

Browse files
Files changed (1) hide show
  1. a.py +38 -5
a.py CHANGED
@@ -98,6 +98,25 @@ def truncate_at_full_stop(text, max_length=1024):
98
  return truncated
99
 
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  if st.session_state.selected_service == "Kendr Locator":
102
  st.title("MedBot")
103
 
@@ -107,13 +126,27 @@ if st.session_state.selected_service == "Kendr Locator":
107
  if user_input:
108
  if contains_profanity(user_input):
109
  st.markdown("<span style='color: red;'>Mind The Language Dear!</span>", unsafe_allow_html=True)
 
110
  else:
111
  formatted_prompt = template.format(user_input, "")
112
 
113
  response = llm.invoke(formatted_prompt)
114
 
115
- truncated_response = truncate_at_full_stop(response)
116
- st.markdown(f"**MedBot:** {truncated_response}", unsafe_allow_html=False)
117
-
118
-
119
- st.warning("Developer's notice : Responses are generated by AI and maybe inaccurate or inappropriate. Any received medical or financial consult is not a substitute for professional advice.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  return truncated
99
 
100
 
101
+ df1 = pd.read_csv(
102
+ 'med_name.csv', encoding='utf-8'
103
+ )
104
+
105
+ # Convert the 'Meds' column to a lowercase list
106
+ KNOWN_MEDICINES = df1['Meds '].str.lower().tolist()
107
+
108
+
109
+ def contains_medicine_terms(output):
110
+ """Check if the output contains terms that indicate a medicine name."""
111
+ return any(term in output for term in [" IP ", " mg ", " ml ", " Mg ", " Ml ", "mg ", "ml ",
112
+ " gm ", "gm ", " mcg ", "mcg "])
113
+
114
+
115
+ def is_valid_medicine_in_input(user_input):
116
+ """Check if the user input contains any valid medicine name."""
117
+ return any(med in user_input.lower() for med in KNOWN_MEDICINES)
118
+
119
+
120
  if st.session_state.selected_service == "Kendr Locator":
121
  st.title("MedBot")
122
 
 
126
  if user_input:
127
  if contains_profanity(user_input):
128
  st.markdown("<span style='color: red;'>Mind The Language Dear!</span>", unsafe_allow_html=True)
129
+
130
  else:
131
  formatted_prompt = template.format(user_input, "")
132
 
133
  response = llm.invoke(formatted_prompt)
134
 
135
+ if contains_medicine_terms(response):
136
+ # Generated response has medical terms
137
+ if is_valid_medicine_in_input(user_input):
138
+ truncated_response = truncate_at_full_stop(response)
139
+ st.markdown(f"**MedBot:** {truncated_response}", unsafe_allow_html=False)
140
+ else:
141
+ st.markdown("<span style='color: green;'>"
142
+ "Please consult to a Pharmacist at you nearest Janaushadi Kendr""<br>"
143
+ "Use Kendr Locator to locate one near you!"
144
+ "</span>", unsafe_allow_html=True)
145
+
146
+ else:
147
+ # No medicine-related terms, safe to display response
148
+ truncated_response = truncate_at_full_stop(response)
149
+ st.markdown(f"**MedBot:** {truncated_response}", unsafe_allow_html=False)
150
+
151
+ st.warning("Developer's notice : Responses are generated by AI and maybe inaccurate or inappropriate."
152
+ "Any received medical or financial consult is not a substitute for professional advice.")