From 9a9c123a2208440a9fe7af477e444ada51cbdd13 Mon Sep 17 00:00:00 2001 From: LongMingWei <131566573+LongMingWei@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:51:33 +0800 Subject: [PATCH] Add files via upload --- GenerativeAI.ipynb | 191 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 GenerativeAI.ipynb diff --git a/GenerativeAI.ipynb b/GenerativeAI.ipynb new file mode 100644 index 0000000..6836b28 --- /dev/null +++ b/GenerativeAI.ipynb @@ -0,0 +1,191 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "\n", + "# Initialize generative AI: Gemini and pyttsx3 text-to-speech\n", + "\n", + "\n" + ], + "metadata": { + "id": "xajjvRn4ZQj_" + } + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "uDmI30KVpRmP" + }, + "outputs": [], + "source": [ + "import google.generativeai as genai\n", + "import pyttsx3\n", + "import cv2\n", + "import numpy as np\n", + "import time\n", + "from PIL import Image, ImageGrab\n", + "import customtkinter as tk\n", + "import pyautogui\n", + "import secret\n", + "\n", + "# Initialize Gemini Pro Vision\n", + "API_KEY = secret.API_KEY\n", + "genai.configure(api_key=API_KEY)\n", + "model = genai.GenerativeModel('gemini-pro-vision')\n", + "\n", + "# Initialize pyttsx3\n", + "speaker = pyttsx3.init()\n", + "speaker.setProperty('rate', 120)\n", + "voices = speaker.getProperty('voices')\n", + "speaker.setProperty('voice', voices[1].id)" + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Menu button function to start distraction hunting" + ], + "metadata": { + "id": "8r8RiXC0gn9M" + } + }, + { + "cell_type": "code", + "source": [ + "def activate():\n", + " if user_input.get() == \"\":\n", + " label3 = tk.CTkLabel(app, text = \"You can't plan to do nothing!\", font=my_font, fg_color='indigo') # Empty input not allowed\n", + " canvas.create_window(400,425, window=label3)\n", + " return\n", + " keyword = user_input.get()\n", + " canvas.delete(\"all\") # Replace with new screen to signal to user distraction hunting has started\n", + " button1 = tk.CTkButton(app, text=\"I'm done!\", command = app.destroy, font=my_font)\n", + " canvas.create_window(400,400, window=button1)\n", + " label5 = tk.CTkLabel(app, text = f\"Task: {keyword}\", font=my_font, fg_color='indigo') # Displays inputted task\n", + " canvas.create_window(400,300, window=label5)\n", + " focus(keyword, streak) # actual distraction hunting function" + ], + "metadata": { + "id": "psloRkHxgqeg" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Distraction hunting loop (AI backend)" + ], + "metadata": { + "id": "iHADdEm_hDOo" + } + }, + { + "cell_type": "code", + "source": [ + "# main distraction hunting manager\n", + "def focus(keyword, streak):\n", + " screenshot() # take screenshot of user's screen\n", + " picture = Image.open('s1.png')\n", + " unrelated = judge(keyword, picture) # judge from the image whether the user is distarcted from their desired task\n", + " if unrelated:\n", + " streak += 1 # streak counter increases by 1 if screenshot implies that the user is distracted from their task\n", + " if streak >= 2:\n", + " taunt(keyword, picture) # streak of 2 or more shows that user is confirmed to be distracted for some time, so an audio reminder will be played\n", + " else:\n", + " streak = 0 # streak resets to 0 when user is deemed focused on their task again\n", + " print(streak)\n", + " app.after(1000, focus, keyword, streak) # constantly loops every 1 second while in distraction hunting phase\n", + "\n", + "# record screen and take screenshots\n", + "def screenshot():\n", + " ss = pyautogui.screenshot()\n", + " ss.save(\"s1.png\")\n", + "\n", + "# send a text prompt based on the task keyword and make judgement based on the screenshot\n", + "def judge(keyword, image):\n", + " qn = f'Answer YES or NO in caps, nothing else. Is this screenshot directly or indirectly related to \"{keyword}\"?' # first prompt\n", + " print(qn)\n", + " response = model.generate_content(contents=[qn, image])\n", + " print(response.text)\n", + " if response.text == \"NO\" or \"NO\" in response.text:\n", + " return True\n", + " qn = f'Answer YES or NO in caps, nothing else. Is the user doing something else unrelated to the task \"{keyword}\"?' # second prompt\n", + " print(qn)\n", + " response = model.generate_content(contents=[qn, image])\n", + " print(response.text)\n", + " if response.text == \"YES\" or \"YES\" in response.text:\n", + " return True\n", + " return False # user is deemed not distracted when both prompts infer that the user is doing the inputted task\n", + "\n", + "# taunt the user with text-to-speech audio if it's clear they are distracted\n", + "def taunt(keyword, image):\n", + " qn = f'This user is distracted right now. Reply below in ONE SENTENCE ONLY, NOTHING ELSE in a cheeky manner\\\n", + " to remind the user based on what they are currently doing and they are distracted from the task \"{keyword}\"'\n", + " print(qn)\n", + " response = model.generate_content(contents=[qn, image])\n", + " print(response.text)\n", + " speaker.say(response.text)\n", + " speaker.runAndWait()" + ], + "metadata": { + "id": "6P3b_tgThHtk" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Main app + menu interface" + ], + "metadata": { + "id": "Wa2wT7RyhSlG" + } + }, + { + "cell_type": "code", + "source": [ + "app = tk.CTk()\n", + "app.title(\"Anti-Distraction Assistant\")\n", + "my_font = tk.CTkFont(family=\"Courier\", size=35, weight=\"bold\")\n", + "user_input = tk.StringVar()\n", + "times = tk.StringVar()\n", + "keyword = \"\"\n", + "streak = 0\n", + "canvas = tk.CTkCanvas(app, bg='indigo', width=800, height=600)\n", + "canvas.pack()\n", + "label1 = tk.CTkLabel(app, text = \"What do you plan to do today?\", font=my_font, fg_color='indigo')\n", + "canvas.create_window(400,150, window=label1)\n", + "entry1 = tk.CTkEntry(app, textvariable = user_input, font=my_font, width=600)\n", + "canvas.create_window(400,300, window=entry1)\n", + "button = tk.CTkButton(app, text=\"Lock In\", command = activate, font=my_font) # calls the activate() function\n", + "canvas.create_window(400,500, window=button)\n", + "app.mainloop()" + ], + "metadata": { + "id": "cOHC2IwPhWKa" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file