jh000107 commited on
Commit
e26ce64
·
1 Parent(s): 4446131
Files changed (2) hide show
  1. app.ipynb +94 -28
  2. app.py +46 -18
app.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": null,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
@@ -11,14 +11,14 @@
11
  "from openai import OpenAI\n",
12
  "from dotenv import load_dotenv, find_dotenv\n",
13
  "\n",
14
- "%matplotlib inline\n",
15
  "import re\n",
16
- "import matplotlib.pyplot as plt"
 
17
  ]
18
  },
19
  {
20
  "cell_type": "code",
21
- "execution_count": null,
22
  "metadata": {},
23
  "outputs": [],
24
  "source": [
@@ -69,7 +69,7 @@
69
  },
70
  {
71
  "cell_type": "code",
72
- "execution_count": null,
73
  "metadata": {},
74
  "outputs": [],
75
  "source": [
@@ -168,7 +168,7 @@
168
  },
169
  {
170
  "cell_type": "code",
171
- "execution_count": null,
172
  "metadata": {},
173
  "outputs": [],
174
  "source": [
@@ -297,23 +297,28 @@
297
  " visitor_Y_converted = [grouped_sentiments[visitor_Y[i]] for i in range(num_utterances)]\n",
298
  " agent_Y_converted = [grouped_sentiments[agent_Y[i]] for i in range(num_utterances)]\n",
299
  "\n",
300
- " plt.style.use('seaborn')\n",
301
  "\n",
302
  " fig, ax = plt.subplots()\n",
303
- "\n",
304
  "\n",
305
  " ax.plot(X, visitor_Y_converted, label='Visitor', color='blue', marker='o')\n",
306
  " ax.plot(X, agent_Y_converted, label='Agent', color='green', marker='o')\n",
307
  "\n",
308
- " plt.yticks(ticks=[-3,-2,-1,0,1,2,3], \n",
309
- " labels=['Disapproval/Accusatory/Denial/Obscene', 'Anxious/Confused/Annoyed/Remorse',\n",
310
- " 'Uninterested', 'Greeting/None', 'Informative', 'Interest/Curiosity', 'Acceptance/Openness'])\n",
 
311
  " \n",
312
- " for label in ax.get_yticklabels():\n",
313
- " label.set_rotation(45)\n",
 
 
 
 
 
314
  "\n",
315
- " plt.xlabel('Number of utterances')\n",
316
- " plt.ylabel('Sentiments')\n",
317
  " plt.title('Sentiment Flow Plot')\n",
318
  "\n",
319
  " plt.close(fig)\n",
@@ -325,16 +330,37 @@
325
  " return response.choices[0].message.content, fig\n",
326
  "\n",
327
  "def set_key(key):\n",
328
- " with open(\"_.env\", \"w\") as file:\n",
329
- " file.write(f\"OPENAI_API_KEY={key}\")\n",
330
- " \n",
331
- " load_dotenv(find_dotenv(\"_.env\"), override=True)\n",
332
  " return"
333
  ]
334
  },
335
  {
336
  "cell_type": "code",
337
- "execution_count": null,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
  "metadata": {},
339
  "outputs": [],
340
  "source": [
@@ -343,36 +369,68 @@
343
  },
344
  {
345
  "cell_type": "code",
346
- "execution_count": null,
347
  "metadata": {},
348
  "outputs": [],
349
  "source": [
350
  "with gr.Blocks() as gpt_analysis:\n",
351
- " gr.Markdown(\"## Conversation Analysis\")\n",
352
  " gr.Markdown(\n",
353
  " \"This is a custom GPT model designed to provide \\\n",
354
  " a report on overall sentiment flow of the conversation on the \\\n",
355
- " volunteer's perspective.<br /> Click on them and submit them to the model to see how it works.\")\n",
356
  " api_key = gr.Textbox(label=\"Key\", lines=1)\n",
357
  " btn_key = gr.Button(value=\"Submit Key\")\n",
358
  " btn_key.click(set_key, inputs=api_key)\n",
359
- " conversation = gr.Textbox(label=\"Input\", lines=2)\n",
 
 
 
 
360
  " btn = gr.Button(value=\"Submit\")\n",
361
  " with gr.Row():\n",
362
- " output_box = gr.Textbox(value=\"\", label=\"Output\",lines=4)\n",
363
- " plot_box = gr.Plot(label=\"Analysis Plot\")\n",
 
 
 
364
  " \n",
365
  " btn.click(get_completion, inputs=conversation, outputs=[output_box, plot_box])\n"
366
  ]
367
  },
368
  {
369
  "cell_type": "code",
370
- "execution_count": null,
371
  "metadata": {},
372
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  "source": [
374
  "gr.TabbedInterface([gpt_analysis], [\"GPT Anlysis\"]).launch(inline=False)"
375
  ]
 
 
 
 
 
 
 
376
  }
377
  ],
378
  "metadata": {
@@ -382,7 +440,15 @@
382
  "name": "python3"
383
  },
384
  "language_info": {
 
 
 
 
 
 
385
  "name": "python",
 
 
386
  "version": "3.9.13"
387
  }
388
  },
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 28,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
 
11
  "from openai import OpenAI\n",
12
  "from dotenv import load_dotenv, find_dotenv\n",
13
  "\n",
 
14
  "import re\n",
15
+ "import matplotlib.pyplot as plt\n",
16
+ "import seaborn as sns"
17
  ]
18
  },
19
  {
20
  "cell_type": "code",
21
+ "execution_count": 29,
22
  "metadata": {},
23
  "outputs": [],
24
  "source": [
 
69
  },
70
  {
71
  "cell_type": "code",
72
+ "execution_count": 30,
73
  "metadata": {},
74
  "outputs": [],
75
  "source": [
 
168
  },
169
  {
170
  "cell_type": "code",
171
+ "execution_count": 65,
172
  "metadata": {},
173
  "outputs": [],
174
  "source": [
 
297
  " visitor_Y_converted = [grouped_sentiments[visitor_Y[i]] for i in range(num_utterances)]\n",
298
  " agent_Y_converted = [grouped_sentiments[agent_Y[i]] for i in range(num_utterances)]\n",
299
  "\n",
 
300
  "\n",
301
  " fig, ax = plt.subplots()\n",
302
+ " sns.set(style=\"whitegrid\")\n",
303
  "\n",
304
  " ax.plot(X, visitor_Y_converted, label='Visitor', color='blue', marker='o')\n",
305
  " ax.plot(X, agent_Y_converted, label='Agent', color='green', marker='o')\n",
306
  "\n",
307
+ " plt.legend(loc='upper left', bbox_to_anchor=(1,1))\n",
308
+ " plt.subplots_adjust(right=0.8)\n",
309
+ "\n",
310
+ " plt.yticks(ticks=[-3,-2,-1,0,1,2,3])\n",
311
  " \n",
312
+ " # y_labels = {-3: 'Disapproval/Accusatory/Denial/Obscene', -2: 'Anxious/Confused\\nAnnoyed/Remorse', -1: 'Uninterested', 0: 'Greeting/None',\n",
313
+ " # 1: 'Informative', 2: 'Interest/Curiosity', 3: 'Acceptance/Openness'}\n",
314
+ " \n",
315
+ " # cell_text = [[label] for label in y_labels.values()]\n",
316
+ " # plt.table(cellText=cell_text, rowLabels=list(y_labels.keys()), loc='left')\n",
317
+ "\n",
318
+ " # plt.tick_params(axis='y', labelsize=10)\n",
319
  "\n",
320
+ " plt.xlabel('Timestamp')\n",
321
+ " plt.ylabel('Sentiment Score')\n",
322
  " plt.title('Sentiment Flow Plot')\n",
323
  "\n",
324
  " plt.close(fig)\n",
 
330
  " return response.choices[0].message.content, fig\n",
331
  "\n",
332
  "def set_key(key):\n",
333
+ " os.environ['OPENAI_API_KEY'] = key\n",
334
+ " load_dotenv()\n",
 
 
335
  " return"
336
  ]
337
  },
338
  {
339
  "cell_type": "code",
340
+ "execution_count": 51,
341
+ "metadata": {},
342
+ "outputs": [],
343
+ "source": [
344
+ "aligned_markdown_table = \"\"\"\n",
345
+ "<div style='text-align: right; font-size: small;'>\n",
346
+ "\n",
347
+ "| Sentiment Score | Sentiment Label |\n",
348
+ "|:---------------:|:---------------:|\n",
349
+ "| 3 | Acceptance, Openness |\n",
350
+ "| 2 | Interest, Curiosity |\n",
351
+ "| 1 | Informative |\n",
352
+ "| 0 | Greeting |\n",
353
+ "| -1 | Uninterested |\n",
354
+ "| -2 | Anxious, Confused, Annoyed, Remorse |\n",
355
+ "| -3 | Disapproval, Accusatory, Denial, Obscene |\n",
356
+ "\n",
357
+ "</div>\n",
358
+ "\"\"\""
359
+ ]
360
+ },
361
+ {
362
+ "cell_type": "code",
363
+ "execution_count": 32,
364
  "metadata": {},
365
  "outputs": [],
366
  "source": [
 
369
  },
370
  {
371
  "cell_type": "code",
372
+ "execution_count": 66,
373
  "metadata": {},
374
  "outputs": [],
375
  "source": [
376
  "with gr.Blocks() as gpt_analysis:\n",
377
+ " gr.Markdown(\"## Conversation Sentiment Analysis Report\")\n",
378
  " gr.Markdown(\n",
379
  " \"This is a custom GPT model designed to provide \\\n",
380
  " a report on overall sentiment flow of the conversation on the \\\n",
381
+ " 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",
382
  " api_key = gr.Textbox(label=\"Key\", lines=1)\n",
383
  " btn_key = gr.Button(value=\"Submit Key\")\n",
384
  " btn_key.click(set_key, inputs=api_key)\n",
385
+ " with gr.Row():\n",
386
+ " with gr.Column():\n",
387
+ " conversation = gr.Textbox(label=\"Input\", lines=4)\n",
388
+ " with gr.Column():\n",
389
+ " output_box = gr.Textbox(value=\"\", label=\"Output\",lines=4)\n",
390
  " btn = gr.Button(value=\"Submit\")\n",
391
  " with gr.Row():\n",
392
+ " with gr.Column(scale=2):\n",
393
+ " gr.Markdown(aligned_markdown_table)\n",
394
+ " with gr.Column(scale=2):\n",
395
+ " plot_box = gr.Plot(label=\"Analysis Plot\")\n",
396
+ "\n",
397
  " \n",
398
  " btn.click(get_completion, inputs=conversation, outputs=[output_box, plot_box])\n"
399
  ]
400
  },
401
  {
402
  "cell_type": "code",
403
+ "execution_count": 67,
404
  "metadata": {},
405
+ "outputs": [
406
+ {
407
+ "name": "stdout",
408
+ "output_type": "stream",
409
+ "text": [
410
+ "Running on local URL: http://127.0.0.1:7883\n",
411
+ "\n",
412
+ "To create a public link, set `share=True` in `launch()`.\n"
413
+ ]
414
+ },
415
+ {
416
+ "data": {
417
+ "text/plain": []
418
+ },
419
+ "execution_count": 67,
420
+ "metadata": {},
421
+ "output_type": "execute_result"
422
+ }
423
+ ],
424
  "source": [
425
  "gr.TabbedInterface([gpt_analysis], [\"GPT Anlysis\"]).launch(inline=False)"
426
  ]
427
+ },
428
+ {
429
+ "cell_type": "code",
430
+ "execution_count": null,
431
+ "metadata": {},
432
+ "outputs": [],
433
+ "source": []
434
  }
435
  ],
436
  "metadata": {
 
440
  "name": "python3"
441
  },
442
  "language_info": {
443
+ "codemirror_mode": {
444
+ "name": "ipython",
445
+ "version": 3
446
+ },
447
+ "file_extension": ".py",
448
+ "mimetype": "text/x-python",
449
  "name": "python",
450
+ "nbconvert_exporter": "python",
451
+ "pygments_lexer": "ipython3",
452
  "version": "3.9.13"
453
  }
454
  },
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import os
2
- import openai
 
3
  from openai import OpenAI
4
- from dotenv import load_dotenv, find_dotenv
5
 
6
  import re
7
  import matplotlib.pyplot as plt
@@ -233,6 +234,7 @@ def get_completion(conversation, model="gpt-4-1106-preview"):
233
  'Obscene': -3
234
  }
235
 
 
236
  def sentiment_flow_plot(conv):
237
  conv_with_labels = extract_conv_with_labels(analysis)
238
  num_utterances = len(conv_with_labels)
@@ -266,23 +268,28 @@ def get_completion(conversation, model="gpt-4-1106-preview"):
266
  visitor_Y_converted = [grouped_sentiments[visitor_Y[i]] for i in range(num_utterances)]
267
  agent_Y_converted = [grouped_sentiments[agent_Y[i]] for i in range(num_utterances)]
268
 
269
- # plt.style.use('seaborn')
270
 
271
  fig, ax = plt.subplots()
272
-
273
 
274
  ax.plot(X, visitor_Y_converted, label='Visitor', color='blue', marker='o')
275
  ax.plot(X, agent_Y_converted, label='Agent', color='green', marker='o')
276
 
277
- plt.yticks(ticks=[-3,-2,-1,0,1,2,3],
278
- labels=['Disapproval/Accusatory/Denial/Obscene', 'Anxious/Confused/Annoyed/Remorse',
279
- 'Uninterested', 'Greeting/None', 'Informative', 'Interest/Curiosity', 'Acceptance/Openness'])
 
 
 
 
280
 
281
- for label in ax.get_yticklabels():
282
- label.set_rotation(45)
283
 
284
- plt.xlabel('Number of utterances')
285
- plt.ylabel('Sentiments')
 
 
286
  plt.title('Sentiment Flow Plot')
287
 
288
  plt.close(fig)
@@ -294,28 +301,49 @@ def get_completion(conversation, model="gpt-4-1106-preview"):
294
  return response.choices[0].message.content, fig
295
 
296
  def set_key(key):
297
-
298
  os.environ['OPENAI_API_KEY'] = key
299
  load_dotenv()
300
-
301
  return
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  import gradio as gr
304
 
305
  with gr.Blocks() as gpt_analysis:
306
- gr.Markdown("## Conversation Analysis")
307
  gr.Markdown(
308
  "This is a custom GPT model designed to provide \
309
  a report on overall sentiment flow of the conversation on the \
310
- volunteer's perspective.<br /> Click on them and submit them to the model to see how it works.")
311
  api_key = gr.Textbox(label="Key", lines=1)
312
  btn_key = gr.Button(value="Submit Key")
313
  btn_key.click(set_key, inputs=api_key)
314
- conversation = gr.Textbox(label="Input", lines=2)
 
 
 
 
315
  btn = gr.Button(value="Submit")
316
  with gr.Row():
317
- output_box = gr.Textbox(value="", label="Output",lines=4)
318
- plot_box = gr.Plot(label="Analysis Plot")
 
 
 
319
 
320
  btn.click(get_completion, inputs=conversation, outputs=[output_box, plot_box])
321
 
 
1
  import os
2
+ import openai
3
+ import seaborn as sns
4
  from openai import OpenAI
5
+ from dotenv import load_dotenv
6
 
7
  import re
8
  import matplotlib.pyplot as plt
 
234
  'Obscene': -3
235
  }
236
 
237
+
238
  def sentiment_flow_plot(conv):
239
  conv_with_labels = extract_conv_with_labels(analysis)
240
  num_utterances = len(conv_with_labels)
 
268
  visitor_Y_converted = [grouped_sentiments[visitor_Y[i]] for i in range(num_utterances)]
269
  agent_Y_converted = [grouped_sentiments[agent_Y[i]] for i in range(num_utterances)]
270
 
 
271
 
272
  fig, ax = plt.subplots()
273
+ sns.set(style="whitegrid")
274
 
275
  ax.plot(X, visitor_Y_converted, label='Visitor', color='blue', marker='o')
276
  ax.plot(X, agent_Y_converted, label='Agent', color='green', marker='o')
277
 
278
+ plt.legend(loc='upper left', bbox_to_anchor=(1,1))
279
+ plt.subplots_adjust(right=0.8)
280
+
281
+ plt.yticks(ticks=[-3,-2,-1,0,1,2,3])
282
+
283
+ # y_labels = {-3: 'Disapproval/Accusatory/Denial/Obscene', -2: 'Anxious/Confused\nAnnoyed/Remorse', -1: 'Uninterested', 0: 'Greeting/None',
284
+ # 1: 'Informative', 2: 'Interest/Curiosity', 3: 'Acceptance/Openness'}
285
 
286
+ # cell_text = [[label] for label in y_labels.values()]
287
+ # plt.table(cellText=cell_text, rowLabels=list(y_labels.keys()), loc='left')
288
 
289
+ # plt.tick_params(axis='y', labelsize=10)
290
+
291
+ plt.xlabel('Timestamp')
292
+ plt.ylabel('Sentiment Score')
293
  plt.title('Sentiment Flow Plot')
294
 
295
  plt.close(fig)
 
301
  return response.choices[0].message.content, fig
302
 
303
  def set_key(key):
 
304
  os.environ['OPENAI_API_KEY'] = key
305
  load_dotenv()
 
306
  return
307
 
308
+ aligned_markdown_table = """
309
+ <div style='text-align: right; font-size: small;'>
310
+
311
+ | Sentiment Score | Sentiment Label |
312
+ |:---------------:|:---------------:|
313
+ | 3 | Acceptance, Openness |
314
+ | 2 | Interest, Curiosity |
315
+ | 1 | Informative |
316
+ | 0 | Greeting |
317
+ | -1 | Uninterested |
318
+ | -2 | Anxious, Confused, Annoyed, Remorse |
319
+ | -3 | Disapproval, Accusatory, Denial, Obscene |
320
+
321
+ </div>
322
+ """
323
+
324
  import gradio as gr
325
 
326
  with gr.Blocks() as gpt_analysis:
327
+ gr.Markdown("## Conversation Sentiment Analysis Report")
328
  gr.Markdown(
329
  "This is a custom GPT model designed to provide \
330
  a report on overall sentiment flow of the conversation on the \
331
+ 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.")
332
  api_key = gr.Textbox(label="Key", lines=1)
333
  btn_key = gr.Button(value="Submit Key")
334
  btn_key.click(set_key, inputs=api_key)
335
+ with gr.Row():
336
+ with gr.Column():
337
+ conversation = gr.Textbox(label="Input", lines=4)
338
+ with gr.Column():
339
+ output_box = gr.Textbox(value="", label="Output",lines=4)
340
  btn = gr.Button(value="Submit")
341
  with gr.Row():
342
+ with gr.Column(scale=2):
343
+ gr.Markdown(aligned_markdown_table)
344
+ with gr.Column(scale=2):
345
+ plot_box = gr.Plot(label="Analysis Plot")
346
+
347
 
348
  btn.click(get_completion, inputs=conversation, outputs=[output_box, plot_box])
349