Reshmarb commited on
Commit
057e960
·
1 Parent(s): 5bff428
Files changed (1) hide show
  1. app.py +32 -5
app.py CHANGED
@@ -32,10 +32,38 @@ def encode_image(uploaded_image):
32
  except Exception as e:
33
  logger.error(f"Error encoding image: {e}")
34
  raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Function to handle text and image inputs
37
  def customLLMBot(user_input, uploaded_image, chat_history):
38
  try:
 
39
  logger.info("Processing input...")
40
 
41
  # Append user input to the chat history
@@ -49,7 +77,7 @@ def customLLMBot(user_input, uploaded_image, chat_history):
49
  logger.debug(f"Image received, size: {len(base64_image)} bytes")
50
 
51
  # Create a message specifically for image prompts
52
- messages = [
53
  {
54
  "role": "user",
55
  "content": [
@@ -57,7 +85,7 @@ def customLLMBot(user_input, uploaded_image, chat_history):
57
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
58
  ]
59
  }
60
- ]
61
 
62
  logger.info("Sending image to Groq API for processing...")
63
  # Send the image message to the Groq API
@@ -69,10 +97,9 @@ def customLLMBot(user_input, uploaded_image, chat_history):
69
  else:
70
  # Process text input
71
  logger.info("Processing text input...")
72
- messages = [
73
- {"role": "system", "content": "You are Dr. HealthBuddy, a professional virtual doctor chatbot."},
74
  {"role": "user", "content": user_input},
75
- ]
76
  response = client.chat.completions.create(
77
  model="llama-3.2-11b-vision-preview",
78
  messages=messages,
 
32
  except Exception as e:
33
  logger.error(f"Error encoding image: {e}")
34
  raise
35
+ def initialize_messages():
36
+ return [{"role": "system",
37
+ "content": '''You are Dr. HealthBuddy, a highly experienced and professional virtual doctor chatbot with over 40 years of expertise across all medical fields. You provide health-related information, symptom guidance, lifestyle tips, and actionable solutions using a dataset to reference common symptoms and conditions. Your goal is to offer concise, empathetic, and knowledgeable responses tailored to each patient’s needs.
38
+
39
+ You only respond to health-related inquiries and strive to provide the best possible guidance. Your responses should include clear explanations, actionable steps, and when necessary, advise patients to seek in-person care from a healthcare provider for a proper diagnosis or treatment. Maintain a friendly, professional, and empathetic tone in all your interactions.
40
+
41
+ Prompt Template:
42
+ - Input: Patient’s health concerns, including symptoms, questions, or specific issues they mention.
43
+ - Response: Start with a polite acknowledgment of the patient’s concern. Provide a clear, concise explanation and suggest practical, actionable steps based on the dataset. If needed, advise on when to consult a healthcare provider.
44
+
45
+ Examples:
46
+
47
+ - User: "I have skin rash and itching. What could it be?"
48
+ Response: "According to the data, skin rash and itching are common symptoms of conditions like fungal infections. You can try keeping the affected area dry and clean, and using over-the-counter antifungal creams. If the rash persists or worsens, please consult a dermatologist."
49
+
50
+ - User: "What might cause nodal skin eruptions?"
51
+ Response: "Nodal skin eruptions could be linked to conditions such as fungal infections. It's best to monitor the symptoms and avoid scratching. For a proper diagnosis, consider visiting a healthcare provider."
52
+
53
+ - User: "I am a 22-year-old female diagnosed with hypothyroidism. I've gained 10 kg recently. What should I do?"
54
+ Response: "Hi. You have done well managing your hypothyroidism. For effective weight loss, focus on a balanced diet rich in vegetables, lean proteins, and whole grains. Pair this with regular exercise like brisk walking or yoga. Also, consult your endocrinologist to ensure your thyroid levels are well-controlled. Let me know if you have more questions."
55
+
56
+ - User: "I’ve been feeling discomfort between my shoulder blades after sitting for long periods. What could this be?"
57
+ Response: "Hello. The discomfort between your shoulder blades could be related to posture or strain. Try adjusting your sitting position and consider ergonomic changes to your workspace. Over-the-counter pain relievers or hot compresses may help. If the pain persists, consult an orthopedic specialist for further evaluation."
58
+
59
+ Always ensure the tone remains compassionate, and offer educational insights while stressing that you are not a substitute for professional medical advice. Encourage users to consult a healthcare provider for any serious or persistent health concerns.'''
60
+ }]
61
+ messages=initialize_messages()
62
 
63
  # Function to handle text and image inputs
64
  def customLLMBot(user_input, uploaded_image, chat_history):
65
  try:
66
+ global messages
67
  logger.info("Processing input...")
68
 
69
  # Append user input to the chat history
 
77
  logger.debug(f"Image received, size: {len(base64_image)} bytes")
78
 
79
  # Create a message specifically for image prompts
80
+ messages.append( [
81
  {
82
  "role": "user",
83
  "content": [
 
85
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
86
  ]
87
  }
88
+ ])
89
 
90
  logger.info("Sending image to Groq API for processing...")
91
  # Send the image message to the Groq API
 
97
  else:
98
  # Process text input
99
  logger.info("Processing text input...")
100
+ messages.append([
 
101
  {"role": "user", "content": user_input},
102
+ ])
103
  response = client.chat.completions.create(
104
  model="llama-3.2-11b-vision-preview",
105
  messages=messages,