{ "cells": [ { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "import os\n", "import openai \n", "from openai import OpenAI\n", "from dotenv import load_dotenv, find_dotenv\n", "\n", "import re\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "sample_input = \\\n", "\"\"\"\n", "Visitor: Heyyy\n", "Visitor: How are you this evening\n", "Agent: better now ;) call me\n", "Visitor: I am at work for now, be off around 10pm\n", "Visitor: Need some company\n", "Visitor: Are you independent honey\n", "Agent: well since you arent available at the moment ill just come out and say-these sites are bad news. \\\n", " did you know that most of the girls on here are here against their will? \\\n", " Most of them got dragged into this lifestyle by an abuser, \\\n", " oftentimes before they were of legal consenting age. isnt that sad?\n", "Agent: we are with some guys who are trying to spread awareness of the realities of this \"industry\".\n", "Agent: https://exoduscry.com/choice/\n", "Visitor: Thanks\n", "Agent: i encourage you to watch this video. it is jarring to think about how bad someone else's options must be to choose to be on these sites\n", "Visitor: Ooohhh\n", "Agent: selling their body to make ends meet or appease a pimp\n", "Visitor: That's really awful\n", "Agent: it is. you seem like the kind of guy who wouldnt wont to proliferate that kind of harmful lifestyle. am i right in thinking that?\n", "Visitor: Well iam just looking for attention\n", "Visitor: My marriage is not going well lol\n", "Agent: i know that it is hard to find ourselves lonely and without much alternative to meet that perceived need but \\\n", " its humbling to think that our needs can force someone else into such a dark place\n", "Agent: hey, thanks for sharing that my man. i know it can be hard\n", "Agent: marraige is the most humbling of relationships, isnt it?\n", "Visitor: She leaves with her friends n no time for me\n", "Agent: ive been there my guy. i know that it is alot easier to numb that loneliness for sure\n", "Visitor: I want to be faithful\n", "Agent: does your wife know how you feel when she chooses her friends instead of you?\n", "Visitor: I been drinking lately\n", "Visitor: Yes, she takes pills\n", "Agent: if so, i hope you are praying for her to realize the hurt she is causing and to seek change\n", "Visitor: She had surgery 4 yes ago n it's been hard for her n her addiction on pills\n", "Visitor: Yes for now i am looking for a female friend to talk n see what can we do for each other\n", "Agent: that is hard my man. physical pain is a huge obstacle in life for sure so i hear you\n", "Visitor: Well chat later. thanks\n", "Agent: have you considered pursuing other men who can encourage you instead of looking for the easy way out?\n", "Agent: what is your name my friend? i will be praying for you by name if you wouldnt mind sharing it\n", "Agent: well, i gotta run. watch that video i sent and i will definitely be praying for you. \\\n", " I hope you pray for yourself and for your wife - God can definitely intervene and cause complete change in the situation if He wills it. \\\n", " He is good and He hears you. You are loved by Him, brother. Good night\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "sample_output = \\\n", "\"\"\"\n", "Visitor: Heyyy\n", "[Greeting]\n", "Visitor: How are you this evening\n", "[Greeting]\n", "Agent: better now ;) call me\n", "[Openness]\n", "Visitor: I am at work for now, be off around 10pm\n", "[Interest]\n", "Visitor: Need some company\n", "[Interest]\n", "Visitor: Are you independent honey\n", "[Interest]\n", "Agent: well since you arent available at the moment ill just come out and say-these sites are bad news. \\\n", " did you know that most of the girls on here are here against their will? \\\n", " Most of them got dragged into this lifestyle by an abuser, \\\n", " oftentimes before they were of legal consenting age. isnt that sad?\n", "[Informative]\n", "Agent: we are with some guys who are trying to spread awareness of the realities of this \"industry\".\n", "[Informative]\n", "Agent: https://exoduscry.com/choice/\n", "[Informative]\n", "Visitor: Thanks\n", "[Acceptance]\n", "Agent: i encourage you to watch this video. it is jarring to think about how bad someone else's options must be to choose to be on these sites\n", "[Informative]\n", "Visitor: Ooohhh\n", "[Interest]\n", "Agent: selling their body to make ends meet or appease a pimp\n", "[Informative]\n", "Visitor: That's really awful\n", "[Remorse]\n", "Agent: it is. you seem like the kind of guy who wouldnt wont to proliferate that kind of harmful lifestyle. am i right in thinking that?\n", "[Accusatory]\n", "Visitor: Well iam just looking for attention\n", "[Anxious]\n", "Visitor: My marriage is not going well lol\n", "[Anxious]\n", "Agent: i know that it is hard to find ourselves lonely and without much alternative to meet that perceived need but \\\n", " its humbling to think that our needs can force someone else into such a dark place\n", "[Informative]\n", "Agent: hey, thanks for sharing that my man. i know it can be hard\n", "[Acceptance]\n", "Agent: marraige is the most humbling of relationships, isnt it?\n", "[Openness]\n", "Visitor: She leaves with her friends n no time for me\n", "[Annoyed]\n", "Agent: ive been there my guy. i know that it is alot easier to numb that loneliness for sure\n", "[Acceptance]\n", "Visitor: I want to be faithful\n", "[Acceptance]\n", "Agent: does your wife know how you feel when she chooses her friends instead of you?\n", "[Curiosity]\n", "Visitor: I been drinking lately\n", "[Anxious]\n", "Visitor: Yes, she takes pills\n", "[Anxious]\n", "Agent: if so, i hope you are praying for her to realize the hurt she is causing and to seek change\n", "[Interest]\n", "Visitor: She had surgery 4 yes ago n it's been hard for her n her addiction on pills\n", "[Anxious]\n", "Visitor: Yes for now i am looking for a female friend to talk n see what can we do for each other\n", "[Informative]\n", "Agent: that is hard my man. physical pain is a huge obstacle in life for sure so i hear you\n", "[Acceptance]\n", "Visitor: Well chat later. thanks\n", "[Openness]\n", "Agent: have you considered pursuing other men who can encourage you instead of looking for the easy way out?\n", "[Informative]\n", "Agent: what is your name my friend? i will be praying for you by name if you wouldnt mind sharing it\n", "[Openness]\n", "Agent: well, i gotta run. watch that video i sent and i will definitely be praying for you. \\\n", " I hope you pray for yourself and for your wife - God can definitely intervene and cause complete change in the situation if He wills it. \\\n", " He is good and He hears you. You are loved by Him, brother. Good night\n", "[Openness]\n", "\n", "Sentiment Flow Analysis on the Visitor's side:\n", "\n", "The Visitor begins the conversation with a friendly and casual tone, expressing a desire for company and showing interest in the Agent. \\\n", "However, as the Agent provides information about the harsh realities of the commercial sex industry, the Visitor's sentiment shifts to acceptance of the information \\\n", "and a sense of confusion and remorse about the situation.\n", "\n", "The Visitor then reveals personal issues, indicating anxiety and seeking attention due to marital problems. \\\n", "The sentiment continues to be anxious as the Visitor discusses personal struggles with alcohol and his wife's pill addiction, \\\n", "showing a need for companionship and support.\n", "\n", "Despite the heavy topics, the Visitor expresses a desire to remain faithful and shows interest in finding a friend, albeit with a hint of desperation. \\\n", "The Visitor openly takes the Agent's information and the conversation flows smoothly as both the Visitor and the Agent \\\n", "show openness toward each other.\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "def get_completion(conversation, model=\"gpt-4-1106-preview\"):\n", "\n", " prompt = f\"\"\"\n", " The EPIK Project is about mobilizing male allies \\\n", " to disrupt the commercial sex market, \\\n", " equipping them to combat the roots of exploitation \\\n", " and encouraging them to collaborate effectively \\\n", " with the wider anti-trafficking movement. \\\n", " You are an adept expert conversation sentiment analyzer. \\\n", " Your job is to analyze the conversation and provide a report \\\n", " based on the sentiment flow of the conversation on the visitor's \\\n", " perspective. Visitor indicates the potential buyer, and Agent indicates the volunteer from EPIK. \\\n", " The conversation is going to be given in the format:\n", " \n", " Visitor: \n", " Agent: \n", " \n", " The actual conversation is delimited by triple backticks\n", " ```{conversation}```\n", " \n", " Here is the list of sentiment labels you should use delimited by square brackets. \\\n", " [\"Openness\", \"Anxious\", \"Confused\", \"Disapproval\", \"Remorse\", \"Accusatory\", \\\n", " \"Denial\", \"Obscene\", \"Uninterested\", \"Annoyed\", \"Informative\", \"Greeting\", \\\n", " \"Interest\", \"Curiosity\", \"Acceptance\"]\n", " \n", " Your output should look like:\n", " ```\n", " Speaker: \n", " [sentiment label]\n", " ...\n", " Speaker: \n", " [sentiment label]\n", " ```\n", " \n", " where Speaker can either be Visitor or Agent. Then, you should write your report on the sentiment flow \\\n", " on the Visitor's side below.\n", "\n", " Here is a sample input delimited by triple backticks\n", "\n", " ```{sample_input}```\n", "\n", " Here is a same output that you should try to aim for delimited by sqaure brackets\n", "\n", " [{sample_output}]\n", " \"\"\"\n", " \n", " client = OpenAI()\n", " \n", " messages = [{\"role\": \"user\", \"content\": prompt}]\n", " response = client.chat.completions.create(\n", " model=model,\n", " messages=messages,\n", " temperature=0, # this is the degree of randomness of the model's output\n", " )\n", "\n", " analysis = response.choices[0].message.content\n", "\n", " def extract_conv_with_labels(analysis):\n", " analysis = analysis.replace(\"\\n\", \" \")\n", " BETWEEN_BACKTICKS = \"\\\\`\\\\`\\\\`(.*?)\\\\`\\\\`\\\\`\"\n", " match = re.search(BETWEEN_BACKTICKS, analysis)\n", " if match:\n", " conv_with_labels = match.group()[4:-4]\n", " else:\n", " return \"OUTPUT IS IN WRONG FORMAT\"\n", " \n", " # just reformatting it for better format\n", " conv_with_labels = conv_with_labels.split('] ')\n", " temp = [utterance + ']' for utterance in conv_with_labels[:-1]]\n", " conv_with_labels = temp + [conv_with_labels[-1]]\n", " return conv_with_labels\n", "\n", " grouped_sentiments = {\n", " 'Acceptance': 3,\n", " 'Openness': 3,\n", " 'Interest': 2,\n", " 'Curiosity': 2,\n", " 'Informative': 1,\n", " 'Greeting': 0,\n", " 'None': 0,\n", " 'Uninterested': -1,\n", " 'Anxious': -2,\n", " 'Confused': -2,\n", " 'Annoyed': -2,\n", " 'Remorse': -2,\n", " 'Disapproval': -3,\n", " 'Accusatory': -3,\n", " 'Denial': -3,\n", " 'Obscene': -3\n", " }\n", "\n", "\n", " def sentiment_flow_plot(conv):\n", " conv_with_labels = extract_conv_with_labels(analysis)\n", " num_utterances = len(conv_with_labels)\n", "\n", " visitor_Y = [''] * num_utterances\n", " agent_Y = [''] * num_utterances\n", "\n", " for i in range(num_utterances):\n", " utterance = conv_with_labels[i]\n", " match = re.search(r'\\[(.*?)\\]$', utterance)\n", " if match:\n", " label = match.group(1)\n", " else:\n", " print(\"OUTPUT IS IN WRONG FORMAT\")\n", " break\n", " \n", " if utterance.startswith('Visitor'):\n", " visitor_Y[i] = label\n", " if i == 0:\n", " agent_Y[i] = 'None'\n", " else:\n", " agent_Y[i] = agent_Y[i-1]\n", " elif utterance.startswith('Agent'):\n", " agent_Y[i] = label\n", " if i == 0:\n", " visitor_Y[i] = 'None'\n", " else:\n", " visitor_Y[i] = visitor_Y[i-1]\n", "\n", " X = range(1,num_utterances+1)\n", " visitor_Y_converted = [grouped_sentiments[visitor_Y[i]] for i in range(num_utterances)]\n", " agent_Y_converted = [grouped_sentiments[agent_Y[i]] for i in range(num_utterances)]\n", "\n", "\n", " fig, ax = plt.subplots()\n", " sns.set(style=\"whitegrid\")\n", "\n", " ax.plot(X, visitor_Y_converted, label='Visitor', color='blue', marker='o')\n", " ax.plot(X, agent_Y_converted, label='Agent', color='green', marker='o')\n", "\n", " plt.legend(loc='upper left', bbox_to_anchor=(1,1))\n", " plt.subplots_adjust(right=0.8)\n", "\n", " plt.yticks(ticks=[-3,-2,-1,0,1,2,3])\n", " \n", " # y_labels = {-3: 'Disapproval/Accusatory/Denial/Obscene', -2: 'Anxious/Confused\\nAnnoyed/Remorse', -1: 'Uninterested', 0: 'Greeting/None',\n", " # 1: 'Informative', 2: 'Interest/Curiosity', 3: 'Acceptance/Openness'}\n", " \n", " # cell_text = [[label] for label in y_labels.values()]\n", " # plt.table(cellText=cell_text, rowLabels=list(y_labels.keys()), loc='left')\n", "\n", " # plt.tick_params(axis='y', labelsize=10)\n", "\n", " plt.xlabel('Timestamp')\n", " plt.ylabel('Sentiment Score')\n", " plt.title('Sentiment Flow Plot')\n", "\n", " plt.close(fig)\n", "\n", " return fig\n", " \n", " fig = sentiment_flow_plot(analysis)\n", "\n", " return response.choices[0].message.content, fig\n", "\n", "def set_key(key):\n", " os.environ['OPENAI_API_KEY'] = key\n", " load_dotenv()\n", " return" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "aligned_markdown_table = \"\"\"\n", "
\n", "\n", "| Sentiment Score | Sentiment Label |\n", "|:---------------:|:---------------:|\n", "| 3 | Acceptance, Openness |\n", "| 2 | Interest, Curiosity |\n", "| 1 | Informative |\n", "| 0 | Greeting |\n", "| -1 | Uninterested |\n", "| -2 | Anxious, Confused, Annoyed, Remorse |\n", "| -3 | Disapproval, Accusatory, Denial, Obscene |\n", "\n", "
\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "with gr.Blocks() as gpt_analysis:\n", " gr.Markdown(\"## Conversation Sentiment Analysis Report\")\n", " gr.Markdown(\n", " \"This is a custom GPT model designed to provide \\\n", " a report on overall sentiment flow of the conversation on the \\\n", " volunteer's perspective. It also provies a live plot analysis of sentiments throughout the conversation.

Click on them and submit them to the model to see how it works.\")\n", " api_key = gr.Textbox(label=\"Key\", lines=1)\n", " btn_key = gr.Button(value=\"Submit Key\")\n", " btn_key.click(set_key, inputs=api_key)\n", " with gr.Row():\n", " with gr.Column():\n", " conversation = gr.Textbox(label=\"Input\", lines=4)\n", " with gr.Column():\n", " output_box = gr.Textbox(value=\"\", label=\"Output\",lines=4)\n", " btn = gr.Button(value=\"Submit\")\n", " with gr.Row():\n", " with gr.Column(scale=2):\n", " gr.Markdown(aligned_markdown_table)\n", " with gr.Column(scale=2):\n", " plot_box = gr.Plot(label=\"Analysis Plot\")\n", "\n", " \n", " btn.click(get_completion, inputs=conversation, outputs=[output_box, plot_box])\n" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7883\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gr.TabbedInterface([gpt_analysis], [\"GPT Anlysis\"]).launch(inline=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 2 }