sunbal7 commited on
Commit
86ca02b
·
verified ·
1 Parent(s): 0789c88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -35
app.py CHANGED
@@ -189,6 +189,8 @@ def init_session_state():
189
  st.session_state.current_language = 'English'
190
  if 'groq_api_key' not in st.session_state:
191
  st.session_state.groq_api_key = ''
 
 
192
 
193
  # Healthcare quotes database
194
  HEALTHCARE_QUOTES = [
@@ -274,7 +276,7 @@ class HealthcareChatbot:
274
 
275
  def get_response(self, user_input, language='English'):
276
  # If Groq client is available, use it for more intelligent responses
277
- if self.groq_client:
278
  try:
279
  return self._get_groq_response(user_input, language)
280
  except Exception as e:
@@ -288,12 +290,17 @@ class HealthcareChatbot:
288
  Provide accurate, culturally appropriate medical advice in a compassionate manner.
289
  Focus on preventive care, symptom explanation, and when to seek medical attention.
290
  Always emphasize that you are an AI assistant and recommend consulting healthcare professionals for serious conditions.
291
- Be specific, practical, and provide actionable advice."""
 
 
 
 
 
292
 
293
  if language == 'Urdu':
294
- system_prompt += " Respond in Urdu with proper medical terminology and cultural sensitivity."
295
  else:
296
- system_prompt += " Respond in English with clear, professional medical advice."
297
 
298
  messages = [
299
  {"role": "system", "content": system_prompt},
@@ -995,10 +1002,9 @@ def load_models():
995
  if os.path.exists(file_path):
996
  try:
997
  model = joblib.load(file_path)
998
- st.success(f"✅ {model_name} model loaded successfully from {file_path}")
999
  return model
1000
  except Exception as e:
1001
- st.warning(f"⚠️ Error loading {model_name} from {file_path}: {str(e)}")
1002
  continue
1003
  return None
1004
 
@@ -1200,9 +1206,14 @@ def main():
1200
  local_css()
1201
  init_session_state()
1202
 
1203
- # Load ML models
1204
- with st.spinner("🔄 Loading AI models..."):
1205
- models = load_models()
 
 
 
 
 
1206
 
1207
  # Language selector at top right with Pakistan flag colors
1208
  col_lang, col_space = st.columns([1, 5])
@@ -1494,33 +1505,19 @@ def main():
1494
  st.markdown('<h2 style="color: #00401A; border-bottom: 3px solid #00401A; padding-bottom: 10px;">💬 سیحت اے آئی ہیلتھ اسسٹنٹ</h2>', unsafe_allow_html=True)
1495
  st.write("صحت سے متعلق سوالات پوچھیں اور فوری، قابل اعتماد طبی معلومات حاصل کریں")
1496
 
1497
- # Groq API Configuration
1498
- with st.expander("🔑 AI Assistant Configuration", expanded=False):
1499
- st.markdown('<div class="api-key-section">', unsafe_allow_html=True)
1500
- if language == "English":
1501
- groq_api_key = st.text_input("Enter Groq API Key:", type="password",
1502
- value=st.session_state.groq_api_key,
1503
- help="Get your API key from https://console.groq.com")
1504
- st.caption("💡 Using Groq API enhances response quality with advanced AI. Leave empty for basic rule-based responses.")
1505
- else:
1506
- groq_api_key = st.text_input("Groq API کلید درج کریں:", type="password",
1507
- value=st.session_state.groq_api_key,
1508
- help="API کلید حاصل کریں https://console.groq.com سے")
1509
- st.caption("💡 Groq API کا استعمال جدید AI کے ساتھ جوابات کی معیار کو بہتر بناتا ہے۔ بنیادی رول بیسڈ جوابات کے لیے خالی چھوڑ دیں۔")
1510
- st.markdown('</div>', unsafe_allow_html=True)
1511
-
1512
- if groq_api_key:
1513
- st.session_state.groq_api_key = groq_api_key
1514
- try:
1515
- groq_client = GroqClient(groq_api_key)
1516
- chatbot = HealthcareChatbot(groq_client)
1517
- st.success("✅ Groq API configured successfully! Using advanced AI responses.")
1518
- except Exception as e:
1519
- st.error(f"❌ Error configuring Groq API: {str(e)}")
1520
- chatbot = HealthcareChatbot()
1521
- else:
1522
  chatbot = HealthcareChatbot()
1523
- st.info("ℹ️ Using rule-based health assistant. For enhanced responses, add Groq API key.")
 
1524
 
1525
  # Display chat history
1526
  for message in st.session_state.chat_history:
 
189
  st.session_state.current_language = 'English'
190
  if 'groq_api_key' not in st.session_state:
191
  st.session_state.groq_api_key = ''
192
+ if 'models_loaded' not in st.session_state:
193
+ st.session_state.models_loaded = False
194
 
195
  # Healthcare quotes database
196
  HEALTHCARE_QUOTES = [
 
276
 
277
  def get_response(self, user_input, language='English'):
278
  # If Groq client is available, use it for more intelligent responses
279
+ if self.groq_client and self.groq_client.api_key:
280
  try:
281
  return self._get_groq_response(user_input, language)
282
  except Exception as e:
 
290
  Provide accurate, culturally appropriate medical advice in a compassionate manner.
291
  Focus on preventive care, symptom explanation, and when to seek medical attention.
292
  Always emphasize that you are an AI assistant and recommend consulting healthcare professionals for serious conditions.
293
+ Be specific, practical, and provide actionable advice.
294
+
295
+ IMPORTANT: Provide detailed, specific answers directly related to the user's query.
296
+ Do not give generic responses - tailor your answer specifically to what the user is asking.
297
+ If they ask about specific symptoms, conditions, or treatments, provide comprehensive information about that topic.
298
+ Use your medical knowledge to give practical, actionable advice."""
299
 
300
  if language == 'Urdu':
301
+ system_prompt += " Respond in Urdu with proper medical terminology and cultural sensitivity. Provide detailed, specific answers in Urdu."
302
  else:
303
+ system_prompt += " Respond in English with clear, professional medical advice. Provide detailed, specific answers in English."
304
 
305
  messages = [
306
  {"role": "system", "content": system_prompt},
 
1002
  if os.path.exists(file_path):
1003
  try:
1004
  model = joblib.load(file_path)
1005
+ # Success message removed from interface as requested
1006
  return model
1007
  except Exception as e:
 
1008
  continue
1009
  return None
1010
 
 
1206
  local_css()
1207
  init_session_state()
1208
 
1209
+ # Load ML models in background without showing messages
1210
+ if not st.session_state.models_loaded:
1211
+ with st.spinner("🔄 Loading AI models..."):
1212
+ models = load_models()
1213
+ st.session_state.models = models
1214
+ st.session_state.models_loaded = True
1215
+
1216
+ models = st.session_state.models
1217
 
1218
  # Language selector at top right with Pakistan flag colors
1219
  col_lang, col_space = st.columns([1, 5])
 
1505
  st.markdown('<h2 style="color: #00401A; border-bottom: 3px solid #00401A; padding-bottom: 10px;">💬 سیحت اے آئی ہیلتھ اسسٹنٹ</h2>', unsafe_allow_html=True)
1506
  st.write("صحت سے متعلق سوالات پوچھیں اور فوری، قابل اعتماد طبی معلومات حاصل کریں")
1507
 
1508
+ # Groq API Configuration - Hidden from interface as requested
1509
+ # Check if API key is available in environment or secrets
1510
+ groq_api_key = st.secrets.get("GROQ_API_KEY", "") if hasattr(st, 'secrets') else ""
1511
+
1512
+ if groq_api_key:
1513
+ st.session_state.groq_api_key = groq_api_key
1514
+ try:
1515
+ groq_client = GroqClient(groq_api_key)
1516
+ chatbot = HealthcareChatbot(groq_client)
1517
+ except Exception as e:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1518
  chatbot = HealthcareChatbot()
1519
+ else:
1520
+ chatbot = HealthcareChatbot()
1521
 
1522
  # Display chat history
1523
  for message in st.session_state.chat_history: