Spaces:
Sleeping
Sleeping
File size: 19,217 Bytes
4d17192 e26ce64 4d17192 254b6e4 e26ce64 4d17192 e26ce64 4d17192 254b6e4 4d17192 e26ce64 4d17192 254b6e4 4d17192 e26ce64 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 e26ce64 254b6e4 e26ce64 254b6e4 e26ce64 4d17192 e26ce64 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 4d17192 254b6e4 e26ce64 254b6e4 4d17192 e26ce64 4d17192 254b6e4 4d17192 e26ce64 4d17192 254b6e4 e26ce64 254b6e4 e26ce64 254b6e4 e26ce64 254b6e4 e26ce64 254b6e4 4d17192 e26ce64 4d17192 e26ce64 4d17192 254b6e4 4d17192 e26ce64 4d17192 0ee0f23 4d17192 e26ce64 0ee0f23 e26ce64 0ee0f23 4d17192 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
{
"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: <Visitor's message here>\n",
" Agent: <Agent's message here>\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: <Speaker's message here>\n",
" [sentiment label]\n",
" ...\n",
" Speaker: <Speaker's message here>\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",
"<div style='text-align: right; font-size: small;'>\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",
"</div>\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.<br /><br />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
}
|