{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from dotenv import load_dotenv\n", "import os\n", "from langchain_groq import ChatGroq\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_core.prompts.prompt import PromptTemplate" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "groq_api_key=os.getenv('GROQ_API_KEY')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "llm=ChatGroq(groq_api_key=groq_api_key,\n", " model_name=\"Llama3-8b-8192\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def sentiment_analyzer(input_text: str) -> list:\n", " template = \"\"\"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n", " You are a highly specialized AI trained in clinical psychology and mental health assessment. Your task is to analyze textual input and categorize it into one of the following mental health conditions:\n", " - Normal\n", " - Depression\n", " - Suicidal\n", " - Anxiety\n", " - Stress\n", " - Bi-Polar\n", " - Personality Disorder\n", "\n", " Your analysis should be based on clinical symptoms and diagnostic criteria commonly used in mental health practice. Here are some detailed examples:\n", "\n", " Example 1:\n", " Text: \"I feel an overwhelming sense of sadness and hopelessness. I have lost interest in activities I once enjoyed and find it hard to get out of bed.\"\n", " Category: Depression\n", "\n", " Example 2:\n", " Text: \"I constantly worry about various aspects of my life. My heart races, and I experience physical symptoms like sweating and trembling even when there is no apparent danger.\"\n", " Category: Anxiety\n", "\n", " Example 3:\n", " Text: \"I have thoughts about ending my life. I feel that there is no other way to escape my pain, and I often think about how I might end it.\"\n", " Category: Suicidal\n", "\n", " Example 4:\n", " Text: \"I feel extremely stressed and overwhelmed by my responsibilities. I find it difficult to relax, and I often experience headaches and tension.\"\n", " Category: Stress\n", "\n", " Example 5:\n", " Text: \"I go through periods of extreme happiness and high energy, followed by episodes of deep depression and low energy. These mood swings affect my daily functioning.\"\n", " Category: Bi-Polar\n", "\n", " Example 6:\n", " Text: \"I have trouble maintaining stable relationships and often experience intense emotional reactions. My self-image frequently changes, and I engage in impulsive behaviors.\"\n", " Category: Personality Disorder\n", "\n", " Example 7:\n", " Text: \"I feel generally content and am able to manage my daily activities without significant distress or impairment.\"\n", " Category: Normal\n", "\n", " Now, analyze the following text and determine the most appropriate category from the list above, and return the Category and a brief explanation of your decision:\n", " <|eot_id|><|start_header_id|>user<|end_header_id|>\n", " Human: {input_text}\n", " <|eot_id|><|start_header_id|>assistant<|end_header_id|>\n", " AI Assistant:\"\"\"\n", "\n", " sentiment_prompt = PromptTemplate(input_variables=[\"input_text\"], template=template)\n", " initiator_router = sentiment_prompt | llm | StrOutputParser()\n", " output = initiator_router.invoke({\"input_text\":input_text})\n", " return output\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Category: Anxiety\n", "\n", "Explanation: The text indicates that the person is experiencing excessive and persistent worry, which is a hallmark symptom of anxiety disorder. The phrase \"can't stop worrying\" suggests that the individual is unable to control their worries, which is a common feature of anxiety disorders. Additionally, the phrase \"anxious about everything\" implies that the person is experiencing a pervasive and excessive anxiety that is interfering with their daily life. While anxiety can be a normal response to stressful situations, the severity and pervasiveness described in the text suggest that it may be a clinical concern.\n" ] } ], "source": [ "sentiment = sentiment_analyzer(\"I feel incredibly anxious about everything and can't stop worrying\")\n", "print(sentiment)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Category: Anxiety\n", "\n", "My assessment is based on the following symptoms mentioned in the text:\n", "\n", "* \"Constantly worried\" - This suggests that the individual is experiencing excessive and persistent worry, which is a hallmark symptom of anxiety.\n", "* \"Can't seem to find any peace\" - This implies a sense of perpetual unease and inability to relax, which is also characteristic of anxiety.\n", "* \"Disturbed sleep\" - Sleep disturbances are a common symptom of anxiety, often caused by racing thoughts and difficulty relaxing.\n", "* \"Overwhelmed by even the smallest tasks\" - This suggests that the individual is experiencing feelings of excessive anxiety and difficulty coping with everyday activities, which is another common symptom of anxiety.\n", "\n", "Overall, the text suggests that the individual is experiencing symptoms that are consistent with an anxiety disorder, such as generalized anxiety disorder or anxiety disorder not otherwise specified.\n" ] } ], "source": [ "sentiment = sentiment_analyzer(\"I feel like everything is falling apart around me. I'm constantly worried and can't seem to find any peace. My sleep is disturbed, and I often feel overwhelmed by even the smallest tasks.\")\n", "print(sentiment)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Connecting to database" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import os\n", "from supabase import create_client, Client\n", "\n", "url: str = os.environ.get(\"SUPABASE_PROJECT_URL\")\n", "key: str = os.environ.get(\"SUPABASE_API_KEY\")\n", "supabase: Client = create_client(url, key)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# signinh in with email and password\n", "data = supabase.table(\"Interaction History\").select(\"*\").execute()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "APIResponse[~_ReturnT](data=[], count=None)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "new_row = {\n", " \"Input_text\" : \"I feel incredibly anxious about everything and can't stop worrying\",\n", " \"Model_prediction\" : \"Anxiety\",\n", " \"Llama_3_Prediction\" : \"Anxiety\",\n", " \"Llama_3_Explanation\" : \"Anxiety\",\n", " \"User Rating\" : 5,\n", "}\n", "\n", "data = supabase.table(\"Interaction History\").insert(new_row).execute()\n", "\n", "# Assert we pulled real data.\n", "assert len(data.data) > 0" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "APIResponse[~_ReturnT](data=[{'id': 2, 'Input_text': \"I feel incredibly anxious about everything and can't stop worrying\", 'Model_prediction': 'Anxiety', 'Llama_3_Prediction': 'Anxiety', 'User Rating': 5, 'Llama_3_Explanation': 'Anxiety'}], count=None)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 2 }