Skip to content

hareesh540/SmartBuddy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SmartBuddy: AI-Powered Chatbot Web App

Overview

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.


Features

  • 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 .env file using python-dotenv.
  • Concise Responses: Gemini is prompted to reply in 2-3 plain text sentences, with no markdown or lists, for clarity.

Project Structure

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

Setup & Installation

  1. Clone the repository
  2. Install dependencies
    pip install -r requirements.txt
  3. Create a .env file in the project root:
    FLASK_SECRET_KEY=your_flask_secret_key
    GEMINI_API_KEY=your_gemini_api_key
  4. Run the app
    python app.py
  5. Open in browser Visit http://127.0.0.1:5000

Configuration

  • Gemini API Key: Set your Gemini API key in the .env file.
  • Secret Key: Set a secure Flask secret key in the .env file for session management.

Core Files

app.py

  • 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 .env using python-dotenv.

chat_db.py

  • SQLite database logic:
    • init_db(): Initializes the chats table.
    • 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.

templates/index.html

  • Main HTML structure.
  • Includes chat area, input, control buttons, and modal for previous chats.

static/script.js

  • 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.

static/style.css

  • Modern, responsive design with gradients, rounded corners, chat bubbles, and dark mode support.

Usage

  • 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.

Extending the App

  • Add more APIs: Extend the logic in get_response to 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.

License

This project is for educational/demo purposes. Replace the Gemini API key with your own for production use.


Credits

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors