{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "!pip install SpeechRecognition pyttsx3 wikipedia pyjokes"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Rli4dV6z_Dhq",
        "outputId": "2b5f8d2c-861d-4b0c-9557-74693a9a44ba"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting SpeechRecognition\n",
            "  Downloading speechrecognition-3.14.2-py3-none-any.whl.metadata (30 kB)\n",
            "Collecting pyttsx3\n",
            "  Downloading pyttsx3-2.98-py3-none-any.whl.metadata (3.8 kB)\n",
            "Collecting wikipedia\n",
            "  Downloading wikipedia-1.4.0.tar.gz (27 kB)\n",
            "  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "Collecting pyjokes\n",
            "  Downloading pyjokes-0.8.3-py3-none-any.whl.metadata (3.4 kB)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.11/dist-packages (from SpeechRecognition) (4.13.2)\n",
            "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.11/dist-packages (from wikipedia) (4.13.4)\n",
            "Requirement already satisfied: requests<3.0.0,>=2.0.0 in /usr/local/lib/python3.11/dist-packages (from wikipedia) (2.32.3)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3.0.0,>=2.0.0->wikipedia) (3.4.1)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests<3.0.0,>=2.0.0->wikipedia) (3.10)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3.0.0,>=2.0.0->wikipedia) (2.4.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests<3.0.0,>=2.0.0->wikipedia) (2025.4.26)\n",
            "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.11/dist-packages (from beautifulsoup4->wikipedia) (2.7)\n",
            "Downloading speechrecognition-3.14.2-py3-none-any.whl (32.9 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m32.9/32.9 MB\u001b[0m \u001b[31m52.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading pyttsx3-2.98-py3-none-any.whl (34 kB)\n",
            "Downloading pyjokes-0.8.3-py3-none-any.whl (47 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m47.6/47.6 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hBuilding wheels for collected packages: wikipedia\n",
            "  Building wheel for wikipedia (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for wikipedia: filename=wikipedia-1.4.0-py3-none-any.whl size=11678 sha256=7a52508b98ef024059179d32b20236425211023a78088811f6c5b938310c6aa4\n",
            "  Stored in directory: /root/.cache/pip/wheels/8f/ab/cb/45ccc40522d3a1c41e1d2ad53b8f33a62f394011ec38cd71c6\n",
            "Successfully built wikipedia\n",
            "Installing collected packages: pyttsx3, SpeechRecognition, pyjokes, wikipedia\n",
            "Successfully installed SpeechRecognition-3.14.2 pyjokes-0.8.3 pyttsx3-2.98 wikipedia-1.4.0\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import speech_recognition as sr\n",
        "import pyttsx3\n",
        "import datetime\n",
        "import wikipedia\n",
        "import webbrowser\n",
        "import os\n",
        "import pyjokes"
      ],
      "metadata": {
        "id": "itXflGv6_Dk1"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def speak(text):\n",
        "    print(f\"Assistant: {text}\")\n",
        "    try:\n",
        "        engine = pyttsx3.init()\n",
        "        engine.say(text)\n",
        "        engine.runAndWait()\n",
        "    except:\n",
        "        print(\"Speech output not supported in Colab.\")"
      ],
      "metadata": {
        "id": "EBwqU2PT_DoY"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def wish_user():\n",
        "    hour = int(datetime.datetime.now().hour)\n",
        "    if hour < 12:\n",
        "        speak(\"Good Morning!\")\n",
        "    elif hour < 18:\n",
        "        speak(\"Good Afternoon!\")\n",
        "    else:\n",
        "        speak(\"Good Evening!\")\n",
        "    speak(\"I am your voice assistant. How can I help you today?\")"
      ],
      "metadata": {
        "id": "bjOYoGgU_DsA"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def take_command():\n",
        "    return input(\"You (type your command): \").lower()"
      ],
      "metadata": {
        "id": "w5-kfirkQ77M"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def run_assistant():\n",
        "    wish_user()\n",
        "    while True:\n",
        "        query = take_command()\n",
        "\n",
        "        if 'wikipedia' in query:\n",
        "            speak(\"Searching Wikipedia...\")\n",
        "            query = query.replace(\"wikipedia\", \"\")\n",
        "            try:\n",
        "                result = wikipedia.summary(query, sentences=2)\n",
        "                speak(\"According to Wikipedia:\")\n",
        "                speak(result)\n",
        "            except:\n",
        "                speak(\"Sorry, I couldn't find anything.\")\n",
        "\n",
        "        elif 'open youtube' in query:\n",
        "            speak(\"Opening YouTube...\")\n",
        "            webbrowser.open(\"https://www.youtube.com\")\n",
        "\n",
        "        elif 'open google' in query:\n",
        "            speak(\"Opening Google...\")\n",
        "            webbrowser.open(\"https://www.google.com\")\n",
        "\n",
        "        elif 'time' in query:\n",
        "            strTime = datetime.datetime.now().strftime(\"%H:%M:%S\")\n",
        "            speak(f\"The current time is {strTime}\")\n",
        "\n",
        "        elif 'joke' in query:\n",
        "            joke = pyjokes.get_joke()\n",
        "            speak(joke)\n",
        "\n",
        "        elif 'exit' in query or 'bye' in query:\n",
        "            speak(\"Goodbye! Have a nice day!\")\n",
        "            break\n",
        "\n",
        "        else:\n",
        "            speak(\"Sorry, I didn't understand that. Try again.\")"
      ],
      "metadata": {
        "id": "wD51lEPe_eRr"
      },
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "run_assistant()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "evdpAQAf_kRJ",
        "outputId": "b5ee2aa8-7fb4-473c-85ff-c15570716d61"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Assistant: Good Morning!\n",
            "Speech output not supported in Colab.\n",
            "Assistant: I am your voice assistant. How can I help you today?\n",
            "Speech output not supported in Colab.\n",
            "You (type your command): what is python wikipedia\n",
            "Assistant: Searching Wikipedia...\n",
            "Speech output not supported in Colab.\n",
            "Assistant: According to Wikipedia:\n",
            "Speech output not supported in Colab.\n",
            "Assistant: Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.\n",
            "Speech output not supported in Colab.\n",
            "You (type your command): open you tube\n",
            "Assistant: Sorry, I didn't understand that. Try again.\n",
            "Speech output not supported in Colab.\n",
            "You (type your command): open youtube\n",
            "Assistant: Opening YouTube...\n",
            "Speech output not supported in Colab.\n",
            "You (type your command): what's the time\n",
            "Assistant: The current time is 09:07:15\n",
            "Speech output not supported in Colab.\n",
            "You (type your command): exit\n",
            "Assistant: Goodbye! Have a nice day!\n",
            "Speech output not supported in Colab.\n"
          ]
        }
      ]
    }
  ]
}