chatbot2 / app.py
Reshmarb's picture
file modified
1b0a98b
raw
history blame
8.88 kB
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
# Create a message specifically for image prompts
messages_image=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "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_image,
)
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))
messages.append({"role":"assistant","content":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)