Solution approach for BloomBot
1. Problem Definition and Goals
Objective:
- Develop BloomBot, an AI-powered chatbot tailored for expecting mothers to provide:
- Pregnancy tips
- Nutrition advice by week
- Emotional support resources
- A conversational interface for queries
Key Requirements:
- AI-Powered Chat: Leverage Gemini for generative responses.
- User Interface: Interactive and user-friendly chatbot interface.
- Customization: Adapt responses based on pregnancy stages.
- Scalability: Handle concurrent user interactions efficiently.
2. Architecture Overview
Key Components:
Frontend:
- Tool:
Tkinter
for desktop GUI. - Features: Buttons, dropdowns, text areas for interaction.
- Tool:
Backend:
- Role: Acts as a bridge between the frontend and Gemini API.
- Tech Stack: Python with
google.generativeai
for Gemini API integration.
Gemini API:
- Purpose: Generate responses for user inputs.
- Capabilities Used: Content generation, chat handling.
Environment Configuration:
- Secure API key storage using
.env
file anddotenv
.
- Secure API key storage using
3. Solution Workflow
Frontend Interaction:
- Users interact with BloomBot via a Tkinter-based GUI:
- Buttons for specific tasks (e.g., pregnancy tips, nutrition advice).
- A dropdown for selecting pregnancy weeks.
- A text area for displaying bot responses.
Backend Processing:
- Task-Specific Prompts:
- Predefined prompts for tasks like fetching pregnancy tips or emotional support.
- Dynamic prompts (e.g., week-specific nutrition advice).
- Free-Form Queries:
- Use the chat feature of Gemini to handle user inputs dynamically.
- Response Handling:
- Parse and return Gemini's response to the frontend.
Gemini API Integration:
- Models Used:
gemini-1.5-flash
. - API methods like
generate_content
for static prompts andstart_chat
for conversational queries.
4. Implementation Details
Backend Implementation
Key Features:
- Pregnancy Tip Generator:
- Prompt:
"Give me a helpful tip for expecting mothers."
- Method:
generate_content
.
- Prompt:
- Week-Specific Nutrition Advice:
- Dynamic prompt:
"Provide nutrition advice for week {week} of pregnancy."
- Method:
generate_content
.
- Dynamic prompt:
- Emotional Support Resources:
- Prompt:
"What resources are available for emotional support for expecting mothers?"
- Method:
generate_content
.
- Prompt:
- Chat Handler:
- Start a conversation:
start_chat
. - Handle free-form queries.
- Start a conversation:
Code Snippet:
class ExpectingMotherBotBackend:
def __init__(self, api_key):
self.api_key = api_key
genai.configure(api_key=self.api_key)
self.model = genai.GenerativeModel("models/gemini-1.5-flash")
def get_pregnancy_tip(self):
prompt = "Give me a helpful tip for expecting mothers."
result = self.model.generate_content(prompt)
return result.text if result.text else "Sorry, I couldn't fetch a tip right now."
def get_nutrition_advice(self, week):
prompt = f"Provide nutrition advice for week {week} of pregnancy."
result = self.model.generate_content(prompt)
return result.text if result.text else "I couldn't fetch nutrition advice at the moment."
def get_emotional_support(self):
prompt = "What resources are available for emotional support for expecting mothers?"
result = self.model.generate_content(prompt)
return result.text if result.text else "I'm having trouble fetching emotional support resources."
def chat_with_bot(self, user_input):
chat = self.model.start_chat()
response = chat.send_message(user_input)
return response.text if response.text else "I'm here to help, but I didn't understand your query."
Frontend Implementation
Key Features:
- Buttons and Inputs:
- Fetch pregnancy tips, nutrition advice, or emotional support.
- Text Area:
- Display bot responses with a scrollable interface.
- Dropdown:
- Select pregnancy week for tailored nutrition advice.
Code Snippet:
class ExpectingMotherBotFrontend:
def __init__(self, backend):
self.backend = backend
self.window = tk.Tk()
self.window.title("BloomBot: Pregnancy Companion")
self.window.geometry("500x650")
self.create_widgets()
def create_widgets(self):
title_label = tk.Label(self.window, text="BloomBot: Your Pregnancy Companion")
title_label.pack()
# Buttons for functionalities
tip_button = tk.Button(self.window, text="Get Daily Pregnancy Tip", command=self.show_pregnancy_tip)
tip_button.pack()
self.week_dropdown = ttk.Combobox(self.window, values=[str(i) for i in range(1, 51)], state="readonly")
self.week_dropdown.pack()
nutrition_button = tk.Button(self.window, text="Get Nutrition Advice", command=self.show_nutrition_advice)
nutrition_button.pack()
support_button = tk.Button(self.window, text="Emotional Support", command=self.show_emotional_support)
support_button.pack()
self.response_text = tk.Text(self.window)
self.response_text.pack()
def show_pregnancy_tip(self):
tip = self.backend.get_pregnancy_tip()
self.display_response(tip)
def show_nutrition_advice(self):
week = self.week_dropdown.get()
advice = self.backend.get_nutrition_advice(int(week))
self.display_response(advice)
def show_emotional_support(self):
support = self.backend.get_emotional_support()
self.display_response(support)
def display_response(self, response):
self.response_text.delete(1.0, tk.END)
self.response_text.insert(tk.END, response)
5. Deployment
Steps:
- Environment Setup:
- Install required packages:
pip install tkinter requests google-generativeai python-dotenv
. - Set up
.env
with the Gemini API key.
- Install required packages:
- Testing:
- Ensure prompt-response functionality works as expected.
- Test UI interactions and Gemini API responses.
6. Monitoring and Maintenance
- Usage Analytics: Track interactions for feature improvements.
- Error Handling: Implement better fallback mechanisms for API failures.
- Feedback Loop: Regularly update prompts based on user feedback.