File size: 8,669 Bytes
173e2fe
1ec0b16
 
 
d897c87
 
78b8ee2
d897c87
 
d32256a
173e2fe
d897c87
 
 
 
 
 
 
 
 
173e2fe
 
 
 
 
 
fa529e8
59b7150
d897c87
 
 
 
173e2fe
d32256a
d897c87
 
 
fa529e8
173e2fe
 
 
057e960
173e2fe
057e960
173e2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aa9f69
d897c87
 
057e960
d32256a
173e2fe
 
98205e6
dfd847e
d897c87
173e2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98a9a27
173e2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
082c62c
dfd847e
 
 
 
 
 
f684842
 
b1186d2
d897c87
173e2fe
d32256a
98205e6
f684842
173e2fe
d897c87
4dc0555
1b0a98b
3aa9f69
 
98205e6
 
 
173e2fe
1c66968
173e2fe
5bff428
d897c87
 
 
 
 
 
173e2fe
d897c87
 
 
 
 
 
98205e6
173e2fe
98205e6
173e2fe
d897c87
173e2fe
d897c87
 
98205e6
173e2fe
caff306
 
173e2fe
d897c87
 
98205e6
173e2fe
b36b54e
 
173e2fe
1b4e41d
98205e6
1b4e41d
98205e6
1b4e41d
b1e86a8
1ec0b16
b1186d2
173e2fe
d897c87
1b4e41d
173e2fe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from groq import Groq
import gradio as gr
from gtts import gTTS
import uuid
import base64
from io import BytesIO
import os
import logging

# Set up logger
logger = logging.getLogger(_name_)
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler('chatbot_log.log')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.addHandler(file_handler)

# Initialize Groq Client
client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))

# client = Groq(
#     api_key="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",
#  )

# Function to encode the image
def encode_image(uploaded_image):
    try:
        logger.debug("Encoding image...")
        buffered = BytesIO()
        uploaded_image.save(buffered, format="PNG")  # Ensure the correct format
        logger.debug("Image encoding complete.")
        return base64.b64encode(buffered.getvalue()).decode("utf-8")
    except Exception as e:
        logger.error(f"Error encoding image: {e}")
        raise
def initialize_messages():
    return [{"role": "system",
             "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.

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.

Prompt Template:
- Input: Patient’s health concerns, including symptoms, questions, or specific issues they mention.
- 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.

Examples:

- User: "I have skin rash and itching. What could it be?"
  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."

- User: "What might cause nodal skin eruptions?"
  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."

- User: "I am a 22-year-old female diagnosed with hypothyroidism. I've gained 10 kg recently. What should I do?"
  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."

- User: "I’ve been feeling discomfort between my shoulder blades after sitting for long periods. What could this be?"
  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."

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.'''
    }]
messages=initialize_messages()

def customLLMBot(user_input, uploaded_image, chat_history):
    try:
        global messages
        logger.info("Processing input...")

        # Append user input to the chat history
        chat_history.append(("user", user_input))

        if uploaded_image is not None:
            # Encode the image to base64
            base64_image = encode_image(uploaded_image)

            # Log the image size and type
            logger.debug(f"Image received, size: {len(base64_image)} bytes")

            # Create a message for the image prompt
            messages.append({
                "role": "user",
                "content": "What's in this image?",
                "type": "image_url",  # If this is supported in Groq API
                "image_url": {"url": f"data:image/png;base64,{base64_image}"}
            })

            logger.info("Sending image to Groq API for processing...")
            response = client.chat.completions.create(
                model="llama-3.2-11b-vision-preview",
                messages=messages,
            )
            logger.info("Image processed successfully.")
        else:
            # Process text input
            logger.info("Processing text input...")
            messages.append({
                "role": "user",
                "content": user_input
            })
            response = client.chat.completions.create(
                model="llama-3.2-11b-vision-preview",
                messages=messages,
            )
            logger.info("Text processed successfully.")

        # Extract the reply
        LLM_reply = response.choices[0].message.content
        logger.debug(f"LLM reply: {LLM_reply}")

        # Append the bot's response to the chat history
        chat_history.append(("bot", LLM_reply))

        # Generate audio for response
        audio_file = f"response_{uuid.uuid4().hex}.mp3"
        tts = gTTS(LLM_reply, lang='en')
        tts.save(audio_file)
        logger.info(f"Audio response saved as {audio_file}")

        # Return chat history and audio file
        return chat_history, audio_file

    except Exception as e:
        # Handle errors gracefully
        logger.error(f"Error in customLLMBot function: {e}")
        return [(("user", user_input or "Image uploaded"), ("bot", f"An error occurred: {e}"))], None


# Gradio Interface
def chatbot_ui():
    with gr.Blocks() as demo:
        gr.Markdown("# Healthcare Chatbot Doctor")

        # State for user chat history
        chat_history = gr.State([])

        # Layout for chatbot and input box alignment
        with gr.Row():
            with gr.Column(scale=3):  # Main column for chatbot
                chatbot = gr.Chatbot(label="Responses", elem_id="chatbot")
                user_input = gr.Textbox(
                    label="Ask a health-related question",
                    placeholder="Describe your symptoms...",
                    elem_id="user-input",
                    lines=1,
                )
            with gr.Column(scale=1):  # Side column for image and buttons
                uploaded_image = gr.Image(label="Upload an Image", type="pil")
                submit_btn = gr.Button("Submit")
                clear_btn = gr.Button("Clear")
                audio_output = gr.Audio(label="Audio Response")

        # Define actions
        def handle_submit(user_query, image, history):
            logger.info("User submitted a query.")
            response, audio = customLLMBot(user_query, image, history)
            return response, audio, None,'', history  # Clear the image after submission

        # Submit on pressing Enter key
        user_input.submit(
            handle_submit,
            inputs=[user_input, uploaded_image, chat_history],
            outputs=[chatbot, audio_output, uploaded_image,user_input, chat_history],
        )

        # Submit on button click
        submit_btn.click(
            handle_submit,
            inputs=[user_input, uploaded_image, chat_history],
            outputs=[chatbot, audio_output, uploaded_image,user_input, chat_history],
        )

        # Action for clearing all fields
        clear_btn.click(
            lambda: ([], "", None, []),
            inputs=[],
            outputs=[chatbot, user_input, uploaded_image, chat_history],
        )

    return demo


# Launch the interface
chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)

#chatbot_ui().launch(server_name="localhost", server_port=7860)