SmartBuddy is a modern, full-stack chatbot web application built with Python (Flask) and JavaScript. It leverages Google's Gemini API for AI-powered, context-aware responses and integrates several public APIs for fun and informative features. The app supports chat history, session management, and a beautiful, responsive UI with dark mode.
- Conversational AI: Uses Gemini API for intelligent, context-aware responses. Prompts are formatted as a role-based conversation (role: user, role: bot) for more natural dialogue.
- Fun & Utility APIs: Fetches jokes, advice, quotes, cat facts, number trivia, and yes/no answers from public APIs.
- Chat History: Stores all conversations in a SQLite database, allowing users to view and restore previous chats. Each chat session is named by the first user message and timestamped.
- Session Management: Each chat session is uniquely identified and can be restored from the UI.
- Modern UI: Responsive, gradient-based design with dark mode, chat bubbles, modal dialogs, and keyboard shortcuts.
- Keyboard Shortcuts: Press Enter to send messages.
- Security: All secrets (API keys, Flask secret) are loaded from a
.envfile usingpython-dotenv. - Concise Responses: Gemini is prompted to reply in 2-3 plain text sentences, with no markdown or lists, for clarity.
SmartBuddy/
├── app.py # Main Flask backend
├── chat_db.py # Database logic (SQLite)
├── chat_history.db # SQLite database file
├── requirements.txt # Python dependencies
├── .env # Environment variables (secrets)
├── static/
│ ├── script.js # Frontend JavaScript (UI logic)
│ └── style.css # CSS styles (light/dark mode, layout)
└── templates/
└── index.html # Main HTML template
- Clone the repository
- Install dependencies
pip install -r requirements.txt
- Create a
.envfile in the project root:FLASK_SECRET_KEY=your_flask_secret_key GEMINI_API_KEY=your_gemini_api_key
- Run the app
python app.py
- Open in browser Visit http://127.0.0.1:5000
- Gemini API Key: Set your Gemini API key in the
.envfile. - Secret Key: Set a secure Flask secret key in the
.envfile for session management.
- Flask app with routes for:
/(main page)/get_response(chatbot logic, integrates Gemini and public APIs)/get_history(fetch current session chat history)/clear_chat(start a new chat session)/get_sessions(list all previous chat sessions with name and timestamp)/load_session(restore a previous session)
- Handles session management and database integration.
- Gemini Prompting: Sends the last 5 user-bot exchanges as a role-based conversation (role: user, role: bot) for context-aware, concise responses.
- Secrets: Loads all secrets from
.envusingpython-dotenv.
- SQLite database logic:
init_db(): Initializes thechatstable.save_chat(): Stores each user-bot exchange.get_chats(): Retrieves all messages for a session.get_all_sessions(): Lists all sessions with their first message (as name) and last activity timestamp.
- Main HTML structure.
- Includes chat area, input, control buttons, and modal for previous chats.
- Handles sending/receiving messages, rendering chat history, session switching, and UI interactions (dark mode, modals, Enter-to-send).
- Pressing Enter in the input box sends the message.
- Modern, responsive design with gradients, rounded corners, chat bubbles, and dark mode support.
- Start a new chat: Click "New Chat".
- Clear chat: Click "Clear Chat".
- View previous chats: Click "Previous Chats" to open a modal with all sessions (named by the first message and timestamp).
- Switch sessions: Click a previous chat to restore its history.
- Send messages: Type in the input and press Enter or click Send.
- Toggle dark mode: Click the moon icon.
- Add more APIs: Extend the logic in
get_responseto include more public APIs or custom logic. - Authentication: Add user accounts for personalized chat history.
- Deployment: Use Gunicorn, Docker, or a cloud platform for production deployment.
This project is for educational/demo purposes. Replace the Gemini API key with your own for production use.