Spaces:
Configuration error
Configuration error
File size: 65,480 Bytes
28e2c84 |
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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "V100",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/vanderbilt-data-science/lo-achievement/blob/124-implement-baseline-functionality-for-oral-exam-module/UI_design_oral_exam_baseline_functionality.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Project IO Achievement - UI Design (Oral Exam)"
],
"metadata": {
"id": "PIbogPXyM0wr"
}
},
{
"cell_type": "markdown",
"source": [
"## Problem Definition\n",
"\n",
"The v1 functionality for the Oral Exam module requires the following:\n",
"\n",
"1. Upload or generation of questions: either the user should upload a set of questions or we should allow the model to generate the questions. The user should pick or it should be inherent if there is no upload of questions. Note that we must also allow for context to be uploaded (vector store, vector store link, specific documents)\n",
"2. The model should prompt the user with a question and pause.\n",
"The user should respond by audio.\n",
"3. This should continue on until some final point where the exam is over.\n",
"\n",
"Then:\n",
"\n",
"1. We should use Whisper to do the transcription, and\n",
"2. Send the transcription, questions, and context for GPT4 for evaluation\n",
"Return the evaluation.\n",
"3. This will primarily be work on a user interface."
],
"metadata": {
"id": "x_Vp8SiKM4p1"
}
},
{
"cell_type": "markdown",
"source": [
"## Libraries\n",
"\n",
"This section will install and import some important libraries such as Langchain, openai, Gradio, and so on"
],
"metadata": {
"id": "o_60X8H3NEne"
}
},
{
"cell_type": "code",
"source": [
"# install libraries here\n",
"# -q flag for \"quiet\" install\n",
"%%capture\n",
"!pip install -q langchain\n",
"!pip install -q openai\n",
"!pip install -q gradio\n",
"!pip install -q transformers\n",
"!pip install -q datasets\n",
"!pip install -q huggingsound\n",
"!pip install -q torchaudio\n",
"!pip install -q git+https://github.com/openai/whisper.git\n",
"!pip install -q docx\n",
"!pip install -q PyPDF2\n",
"!pip install python-docx"
],
"metadata": {
"id": "pxcqXgg2aAN7"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "pEjM1tLsMZBq"
},
"outputs": [],
"source": [
"# import libraries here\n",
"from langchain.llms import OpenAI\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain.document_loaders import TextLoader\n",
"from langchain.indexes import VectorstoreIndexCreator\n",
"from langchain import ConversationChain, LLMChain, PromptTemplate\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.memory import ConversationBufferWindowMemory\n",
"from langchain.prompts import ChatPromptTemplate\n",
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain.embeddings import OpenAIEmbeddings\n",
"from langchain.schema import SystemMessage, HumanMessage, AIMessage\n",
"import openai\n",
"import os\n",
"from getpass import getpass\n",
"from IPython.display import display, Javascript, HTML\n",
"from google.colab.output import eval_js\n",
"from base64 import b64decode\n",
"import ipywidgets as widgets\n",
"from IPython.display import clear_output\n",
"import time\n",
"import requests\n",
"from transformers import WhisperProcessor, WhisperForConditionalGeneration\n",
"from datasets import load_dataset\n",
"# from torchaudio.transforms import Resample\n",
"import whisper\n",
"from huggingsound import SpeechRecognitionModel\n",
"import numpy as np\n",
"import torch\n",
"import librosa\n",
"from datasets import load_dataset\n",
"from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor\n",
"from jiwer import wer\n",
"import pandas as pd\n",
"from IPython.display import display, HTML\n",
"import gradio as gr\n",
"from transformers import pipeline\n",
"from docx import Document\n",
"import PyPDF2\n",
"from pydub import AudioSegment\n",
"import tempfile\n",
"import os\n"
]
},
{
"cell_type": "markdown",
"source": [
"## API Keys\n",
"\n",
"Use these cells to load the API keys required for this notebook. The below code cell uses the `getpass` library."
],
"metadata": {
"id": "03KLZGI_a5W5"
}
},
{
"cell_type": "code",
"source": [
"openai_api_key = getpass()\n",
"os.environ[\"OPENAI_API_KEY\"] = openai_api_key\n",
"openai.api_key = openai_api_key"
],
"metadata": {
"id": "5smcWj4DbFgy",
"outputId": "9a73707b-1a6a-4253-b7d8-181a82b1040f",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Prompt Design\n",
"\n",
"To be added"
],
"metadata": {
"id": "pMo9x8u4AEV1"
}
},
{
"cell_type": "code",
"source": [
"chat = ChatOpenAI(temperature=0.0, model_name='gpt-3.5-turbo-16k')\n",
"chat"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UgnCZRMhADvo",
"outputId": "1bd6b84d-3ea8-49ba-8156-701f4155d69c"
},
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"ChatOpenAI(cache=None, verbose=False, callbacks=None, callback_manager=None, tags=None, metadata=None, client=<class 'openai.api_resources.chat_completion.ChatCompletion'>, model_name='gpt-3.5-turbo-16k', temperature=0.0, model_kwargs={}, openai_api_key='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', openai_api_base='', openai_organization='', openai_proxy='', request_timeout=None, max_retries=6, streaming=False, n=1, max_tokens=None, tiktoken_model_name=None)"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"source": [
"# This is what I used to test the function 'generate_questions'\n",
"template_string2 = \"\"\"\n",
"You are teacher, and you will be given a {context} that is related to the presentation topic.\n",
"\n",
"Please generate a questions based on the context above and transcript that student created . \\\n",
"\n",
"The audio file generated by student is shown below: {transcribed_text}. \\\n",
"\"\"\""
],
"metadata": {
"id": "WmysQZAhKBli"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"prompt_template1 = ChatPromptTemplate.from_template(template_string2)"
],
"metadata": {
"id": "oij6W5rwAaGb"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# prompt_template.messages[0].prompt\n",
"prompt_template1.messages[0].prompt.input_variables"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "C1YRmL46AaJA",
"outputId": "e850524a-3831-4113-c796-5a0ec8584569"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['context', 'transcribed_text']"
]
},
"metadata": {},
"execution_count": 25
}
]
},
{
"cell_type": "code",
"source": [
"# This template is used for testing the function 'ai_evaluate'\n",
"# Detailed evaluation metrics are to be added\n",
"template_string3 = \"\"\"\n",
"You are teacher, and you will be given a context that is related to the presentation topic. \\\n",
"Now, given {context}, evaluate the answer based on the accuracy\n",
"\n",
"The main answer generated by student is shown below: {transcribed_text}. \\\n",
"The questions are shown below: {questions}. \\\n",
"The questions answered by student is shown below: {transcribed_qa}. \\\n",
"\"\"\"\n",
"prompt_template2 = ChatPromptTemplate.from_template(template_string3)\n",
"prompt_template2.messages[0].prompt.input_variables\n"
],
"metadata": {
"id": "141Cxa2MT-l7",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0374d45d-a7f6-41e7-aed0-44c61681de21"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['context', 'questions', 'transcribed_qa', 'transcribed_text']"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "markdown",
"source": [
"## Integrate Prompts from LO project"
],
"metadata": {
"id": "MJCHl1T2TPWC"
}
},
{
"cell_type": "markdown",
"source": [
"### Creating a Chain for Short Answer Generation"
],
"metadata": {
"id": "IPTyUOl-WdiL"
}
},
{
"cell_type": "markdown",
"source": [
"In this example, the context would include the poem \"The Road Not Taken\" by Robert Frost"
],
"metadata": {
"id": "203qBjZvmFK1"
}
},
{
"cell_type": "code",
"source": [
"# This is what I used to test the function 'generate_questions_v2'\n",
"template_string = \"\"\"\n",
"You are a world-class tutor helping students to perform better on oral and written exams though interactive experiences.\"\n",
"\n",
"The following text should be used as the basis for the instructions which follow: {context} \\\n",
"\n",
"The following is the guideline for generating the questiion: {pre_prompt}\n",
"\"\"\""
],
"metadata": {
"id": "w1AjHwIoVnvw"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"prompt_template = ChatPromptTemplate.from_template(template_string)\n",
"prompt_template.messages[0].prompt.input_variables"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "39-lm5I-Wlep",
"outputId": "44b39930-5258-484b-8c7d-c36ff4b5dc1a"
},
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['context', 'pre_prompt']"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"source": [
"### Creating a Chain for AI Evaluation"
],
"metadata": {
"id": "-Pfxkcdxh9nZ"
}
},
{
"cell_type": "code",
"source": [
"template_evaluation = \"\"\"\n",
"Given the follwing {context} and the {transcript}, evaluate whether or not the student answered correctly on the {question}.\n",
"\"\"\""
],
"metadata": {
"id": "u6pH1x-gWnFF"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# @title\n",
"prompt_template2 = ChatPromptTemplate.from_template(template_evaluation)\n",
"prompt_template2.messages[0].prompt.input_variables"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YPO_IE5ThC6W",
"outputId": "5361929c-cf8c-483d-901a-ed14a0db89fa"
},
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['context', 'question', 'transcript']"
]
},
"metadata": {},
"execution_count": 8
}
]
},
{
"cell_type": "markdown",
"source": [
"## UI Design\n",
"\n",
"https://colab.research.google.com/github/petewarden/openai-whisper-webapp/blob/main/OpenAI_Whisper_ASR_Demo.ipynb"
],
"metadata": {
"id": "M6IzVTjz5cex"
}
},
{
"cell_type": "markdown",
"source": [
"### Functions"
],
"metadata": {
"id": "l4o8R5eUE1n8"
}
},
{
"cell_type": "code",
"source": [
"def embed_key(openai_api_key):\n",
" os.environ[\"OPENAI_API_KEY\"] = openai_api_key\n",
"\n",
"def transcribe(audio_file_path):\n",
" with open(audio_file_path, \"rb\") as audio_file:\n",
" # Call OpenAI's Whisper model for transcription\n",
" transcript = openai.Audio.transcribe(\"whisper-1\", audio_file)\n",
" transcribed_text = transcript[\"text\"]\n",
" return transcribed_text\n",
"\n",
"def translate(text):\n",
" # Create a prompt template (This will be changed later to fit the actual task)\n",
" # Here translation will be a filler task of GPT\n",
" test_input1 = prompt_template.format_messages(\n",
" expertise='Language Translation',\n",
" language='Japanese',\n",
" style='romantic',\n",
" transcribed_text=text)\n",
"\n",
" response = chat.predict_messages(test_input1)\n",
" return response.content\n",
"\n",
"def process_file(files):\n",
" for file in files:\n",
" try:\n",
" extension = file.name.split('.')[-1].lower()\n",
" if extension == 'docx':\n",
" doc = Document(file.name)\n",
" full_text = []\n",
" for paragraph in doc.paragraphs:\n",
" full_text.append(paragraph.text)\n",
" return '\\n'.join(full_text)\n",
"\n",
" elif extension == 'pdf':\n",
" pdf_file = open(file.name, 'rb')\n",
" reader = PyPDF2.PdfReader(pdf_file)\n",
" num_pages = len(reader.pages)\n",
" full_text = []\n",
" for page in range(num_pages):\n",
" page_obj = reader.pages[page]\n",
" full_text.append(page_obj.extract_text())\n",
" pdf_file.close()\n",
" return '\\n'.join(full_text)\n",
"\n",
" elif extension == 'txt':\n",
" with open(file.name, 'r') as txt_file:\n",
" full_text = txt_file.read()\n",
" return full_text\n",
"\n",
" else:\n",
" return \"Unsupported file type\"\n",
" except FileNotFoundError:\n",
" return \"File not found\"\n",
" except PermissionError:\n",
" return \"Permission denied\"\n",
"\n",
"def generate_questions(context, transcript):\n",
" text = process_file(context)\n",
" test_input1 = prompt_template1.format_messages(\n",
" context = text,\n",
" transcribed_text = transcript)\n",
"\n",
" response = chat(test_input1)\n",
" return response.content\n",
"\n",
"def generate_questions_v2(text, prompt):\n",
" #text = process_file(file)\n",
" test_input1 = prompt_template.format_messages(\n",
" context = text,\n",
" pre_prompt = prompt)\n",
"\n",
" response = chat(test_input1)\n",
" return response\n",
"\n",
"# def ai_evaluate(context, audio_main, audio_qa, questions):\n",
"# test_input1 = prompt_template2.format_messages(\n",
"# context = context,\n",
"# transcribed_text = audio_main,\n",
"# transcribed_qa = audio_qa,\n",
"# questions = questions)\n",
"\n",
"# response = chat(test_input1)\n",
"# return response.content\n",
"\n",
"def ai_evaluate_v2(text, audio_main, questions):\n",
" #audio = transcribe(audio_main)\n",
" test_input1 = prompt_template2.format_messages(\n",
" context = text,\n",
" transcript = audio_main,\n",
" question = questions\n",
" )\n",
"\n",
" response = chat(test_input1)\n",
" return response.content\n",
"\n",
"def upload_file(files):\n",
" file_paths = [file.name for file in files]\n",
" return file_paths"
],
"metadata": {
"id": "ABN0X9xQHeii"
},
"execution_count": 12,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Test process_file"
],
"metadata": {
"id": "a3WUL_hFyMkr"
}
},
{
"cell_type": "code",
"source": [
"from google.colab import files\n",
"def upload_syllabi():\n",
" uploaded = files.upload()\n",
" for name, data in uploaded.items():\n",
" with open(name, 'wb') as f:\n",
" f.write(data)\n",
" print('saved file', name)\n",
"upload_syllabi()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 90
},
"id": "nih4FXX0Pl9U",
"outputId": "ce48c70a-d52c-4267-f3fc-8b22404448d7"
},
"execution_count": 13,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <input type=\"file\" id=\"files-c72e8e8a-ac6f-48ab-9fb4-a84b1483268a\" name=\"files[]\" multiple disabled\n",
" style=\"border:none\" />\n",
" <output id=\"result-c72e8e8a-ac6f-48ab-9fb4-a84b1483268a\">\n",
" Upload widget is only available when the cell has been executed in the\n",
" current browser session. Please rerun this cell to enable.\n",
" </output>\n",
" <script>// Copyright 2017 Google LLC\n",
"//\n",
"// Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"// you may not use this file except in compliance with the License.\n",
"// You may obtain a copy of the License at\n",
"//\n",
"// http://www.apache.org/licenses/LICENSE-2.0\n",
"//\n",
"// Unless required by applicable law or agreed to in writing, software\n",
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"// See the License for the specific language governing permissions and\n",
"// limitations under the License.\n",
"\n",
"/**\n",
" * @fileoverview Helpers for google.colab Python module.\n",
" */\n",
"(function(scope) {\n",
"function span(text, styleAttributes = {}) {\n",
" const element = document.createElement('span');\n",
" element.textContent = text;\n",
" for (const key of Object.keys(styleAttributes)) {\n",
" element.style[key] = styleAttributes[key];\n",
" }\n",
" return element;\n",
"}\n",
"\n",
"// Max number of bytes which will be uploaded at a time.\n",
"const MAX_PAYLOAD_SIZE = 100 * 1024;\n",
"\n",
"function _uploadFiles(inputId, outputId) {\n",
" const steps = uploadFilesStep(inputId, outputId);\n",
" const outputElement = document.getElementById(outputId);\n",
" // Cache steps on the outputElement to make it available for the next call\n",
" // to uploadFilesContinue from Python.\n",
" outputElement.steps = steps;\n",
"\n",
" return _uploadFilesContinue(outputId);\n",
"}\n",
"\n",
"// This is roughly an async generator (not supported in the browser yet),\n",
"// where there are multiple asynchronous steps and the Python side is going\n",
"// to poll for completion of each step.\n",
"// This uses a Promise to block the python side on completion of each step,\n",
"// then passes the result of the previous step as the input to the next step.\n",
"function _uploadFilesContinue(outputId) {\n",
" const outputElement = document.getElementById(outputId);\n",
" const steps = outputElement.steps;\n",
"\n",
" const next = steps.next(outputElement.lastPromiseValue);\n",
" return Promise.resolve(next.value.promise).then((value) => {\n",
" // Cache the last promise value to make it available to the next\n",
" // step of the generator.\n",
" outputElement.lastPromiseValue = value;\n",
" return next.value.response;\n",
" });\n",
"}\n",
"\n",
"/**\n",
" * Generator function which is called between each async step of the upload\n",
" * process.\n",
" * @param {string} inputId Element ID of the input file picker element.\n",
" * @param {string} outputId Element ID of the output display.\n",
" * @return {!Iterable<!Object>} Iterable of next steps.\n",
" */\n",
"function* uploadFilesStep(inputId, outputId) {\n",
" const inputElement = document.getElementById(inputId);\n",
" inputElement.disabled = false;\n",
"\n",
" const outputElement = document.getElementById(outputId);\n",
" outputElement.innerHTML = '';\n",
"\n",
" const pickedPromise = new Promise((resolve) => {\n",
" inputElement.addEventListener('change', (e) => {\n",
" resolve(e.target.files);\n",
" });\n",
" });\n",
"\n",
" const cancel = document.createElement('button');\n",
" inputElement.parentElement.appendChild(cancel);\n",
" cancel.textContent = 'Cancel upload';\n",
" const cancelPromise = new Promise((resolve) => {\n",
" cancel.onclick = () => {\n",
" resolve(null);\n",
" };\n",
" });\n",
"\n",
" // Wait for the user to pick the files.\n",
" const files = yield {\n",
" promise: Promise.race([pickedPromise, cancelPromise]),\n",
" response: {\n",
" action: 'starting',\n",
" }\n",
" };\n",
"\n",
" cancel.remove();\n",
"\n",
" // Disable the input element since further picks are not allowed.\n",
" inputElement.disabled = true;\n",
"\n",
" if (!files) {\n",
" return {\n",
" response: {\n",
" action: 'complete',\n",
" }\n",
" };\n",
" }\n",
"\n",
" for (const file of files) {\n",
" const li = document.createElement('li');\n",
" li.append(span(file.name, {fontWeight: 'bold'}));\n",
" li.append(span(\n",
" `(${file.type || 'n/a'}) - ${file.size} bytes, ` +\n",
" `last modified: ${\n",
" file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() :\n",
" 'n/a'} - `));\n",
" const percent = span('0% done');\n",
" li.appendChild(percent);\n",
"\n",
" outputElement.appendChild(li);\n",
"\n",
" const fileDataPromise = new Promise((resolve) => {\n",
" const reader = new FileReader();\n",
" reader.onload = (e) => {\n",
" resolve(e.target.result);\n",
" };\n",
" reader.readAsArrayBuffer(file);\n",
" });\n",
" // Wait for the data to be ready.\n",
" let fileData = yield {\n",
" promise: fileDataPromise,\n",
" response: {\n",
" action: 'continue',\n",
" }\n",
" };\n",
"\n",
" // Use a chunked sending to avoid message size limits. See b/62115660.\n",
" let position = 0;\n",
" do {\n",
" const length = Math.min(fileData.byteLength - position, MAX_PAYLOAD_SIZE);\n",
" const chunk = new Uint8Array(fileData, position, length);\n",
" position += length;\n",
"\n",
" const base64 = btoa(String.fromCharCode.apply(null, chunk));\n",
" yield {\n",
" response: {\n",
" action: 'append',\n",
" file: file.name,\n",
" data: base64,\n",
" },\n",
" };\n",
"\n",
" let percentDone = fileData.byteLength === 0 ?\n",
" 100 :\n",
" Math.round((position / fileData.byteLength) * 100);\n",
" percent.textContent = `${percentDone}% done`;\n",
"\n",
" } while (position < fileData.byteLength);\n",
" }\n",
"\n",
" // All done.\n",
" yield {\n",
" response: {\n",
" action: 'complete',\n",
" }\n",
" };\n",
"}\n",
"\n",
"scope.google = scope.google || {};\n",
"scope.google.colab = scope.google.colab || {};\n",
"scope.google.colab._files = {\n",
" _uploadFiles,\n",
" _uploadFilesContinue,\n",
"};\n",
"})(self);\n",
"</script> "
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Saving instructor_note_2.docx to instructor_note_2.docx\n",
"saved file instructor_note_2.docx\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Might need some way to make pdf file to load more readable\n",
"# process_file('/content/instrutor_note.docx')\n",
"# process_file('/content/Big Data & Economics.pdf')\n",
"# process_file('/content/Big Data & Economics.pdf')"
],
"metadata": {
"id": "LJX1AKTMyVm8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Gradio Interface V1"
],
"metadata": {
"id": "c4s5o8baE6wN"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"# with gr.Blocks() as demo:\n",
"# gr.Markdown(\"# Oral Exam App\")\n",
"# with gr.Box():\n",
"# gr.HTML(\"\"\"Embed your OpenAI API key below; if you haven't created one already, visit\n",
"# platform.openai.com/account/api-keys\n",
"# to sign up for an account and get your personal API key\"\"\",\n",
"# elem_classes=\"textbox_label\")\n",
"# input = gr.Textbox(show_label=False, type=\"password\", container=False,\n",
"# placeholder=\"βββββββββββββββββ\")\n",
"# input.change(fn=embed_key, inputs=input, outputs=None)\n",
"\n",
"# with gr.Blocks():\n",
"# gr.Markdown(\"## Upload your audio file or start recording\")\n",
"\n",
"# with gr.Row():\n",
"\n",
"\n",
"# with gr.Column():\n",
"# file_input = gr.Files(label=\"Load a mp3 file\",\n",
"# file_types=['.mp3'], type=\"file\",\n",
"# elem_classes=\"short-height\")\n",
"# record_inputs = gr.Audio(source=\"microphone\", type=\"filepath\")\n",
"\n",
"# with gr.Column():\n",
"# outputs_transcribe=gr.Textbox(label=\"Transcription\")\n",
"\n",
"# with gr.Row():\n",
"# btn1 = gr.Button(value=\"Transcribe recorded audio\")\n",
"# btn1.click(transcribe, inputs=record_inputs, outputs=outputs_transcribe)\n",
"# btn2 = gr.Button(value=\"Transcribe uploaded audio\")\n",
"# btn2.click(transcribe, inputs=file_input, outputs=outputs_transcribe)\n",
"\n",
"# outputs_translate=gr.Textbox(label=\"Translation\")\n",
"# btn3 = gr.Button(value=\"Translate\")\n",
"# btn3.click(translate, inputs=outputs_transcribe, outputs=outputs_translate)\n",
"\n",
"# demo.launch()\n"
],
"metadata": {
"id": "ZkfJXCGDFhdw",
"cellView": "form"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### baseline functionality V1"
],
"metadata": {
"id": "AnkuosJ7Vw4z"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"# Oral Exam App\")\n",
" gr.Markdown(\"## OpenAI API key\")\n",
" with gr.Box():\n",
" gr.HTML(\"\"\"Embed your OpenAI API key below; if you haven't created one already, visit\n",
" platform.openai.com/account/api-keys\n",
" to sign up for an account and get your personal API key\"\"\",\n",
" elem_classes=\"textbox_label\")\n",
" input = gr.Textbox(show_label=False, type=\"password\", container=False,\n",
" placeholder=\"βββββββββββββββββ\")\n",
" input.change(fn=embed_key, inputs=input, outputs=None)\n",
"\n",
" with gr.Blocks():\n",
" #########################\n",
" #########Context#########\n",
" #########################\n",
" with gr.Accordion(\"Context section\"):\n",
" ### Should also allow vector stores\n",
" gr.Markdown(\"## Please upload the context document(s) for Oral exam\")\n",
" context_input = gr.File(label=\"Click to upload context file\",\n",
" file_count=\"multiple\",\n",
" file_types=[\".txt\", \".docx\", \".pdf\"])\n",
" outputs_context=gr.Textbox(label=\"Context\")\n",
" context_input.change(fn=process_file, inputs=context_input, outputs=outputs_context)\n",
" # upload_button = gr.Button(value=\"Show context\")\n",
" # upload_button.click(process_file, context_input, outputs_context)\n",
"\n",
" #########################\n",
" #######Main Audio########\n",
" #########################\n",
" with gr.Accordion(\"Main audio section\"):\n",
" gr.Markdown(\"## Upload your audio file or start recording\")\n",
" with gr.Column():\n",
" ## uploading files seem not working (don't know why)\n",
" with gr.Row():\n",
" file_input = gr.Audio(label=\"Upload Audio\", source=\"upload\", type=\"filepath\")\n",
" record_inputs = gr.Audio(label=\"Record Audio\", source=\"microphone\", type=\"filepath\")\n",
"\n",
" gr.Markdown(\"## Transcribe the audio uploaded or recorded\")\n",
" outputs_transcribe=gr.Textbox(label=\"Transcription\")\n",
"\n",
" file_input.change(fn=transcribe, inputs=file_input, outputs=outputs_transcribe)\n",
" record_inputs.change(fn=transcribe, inputs=record_inputs, outputs=outputs_transcribe)\n",
"\n",
" #########################\n",
" ###Question Generation###\n",
" #########################\n",
" with gr.Accordion(\"Question section\"):\n",
" gr.Markdown(\"## Questions\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" outputs_qa=gr.Textbox(label=\"Generate questions\")\n",
" btn3 = gr.Button(value=\"Generate questions\")\n",
" btn3.click(generate_questions, inputs=[context_input, outputs_transcribe], outputs=outputs_qa)\n",
"\n",
" ######################### Need additional work to include these questions when click button #########################\n",
" with gr.Column():\n",
" submit_question=gr.Textbox(label=\"Use existing questions\")\n",
" btn4 = gr.Button(value=\"Use these questions\")\n",
" # btn4.click(use_this_question, inputs=outputs_transcribe, outputs=None)\n",
"\n",
" #########################\n",
" #########Audio QA########\n",
" #########################\n",
" with gr.Accordion(\"Audio QA section\"):\n",
" gr.Markdown(\"## Question answering\")\n",
" ##### This may be iterative\n",
" with gr.Row():\n",
" file_input2 = gr.Audio(label=\"Upload Audio\", source=\"upload\", type=\"filepath\")\n",
" record_inputs2 = gr.Audio(label=\"Record Audio\", source=\"microphone\", type=\"filepath\")\n",
"\n",
" gr.Markdown(\"## Transcribe the audio uploaded or recorded\")\n",
" outputs_transcribe2=gr.Textbox(label=\"Transcription\")\n",
" file_input2.change(fn=transcribe, inputs=file_input2, outputs=outputs_transcribe2)\n",
" record_inputs2.change(fn=transcribe, inputs=record_inputs2, outputs=outputs_transcribe2)\n",
"\n",
" #########################\n",
" #######Evaluation########\n",
" #########################\n",
" with gr.Accordion(\"Evaluation section\"):\n",
" gr.Markdown(\"## Evaluation\")\n",
" with gr.Tab(\"General evalution\"):\n",
" evalution=gr.Textbox(label=\"AI Evaluation\")\n",
" btn5 = gr.Button(value=\"Evaluate\")\n",
" btn5.click(ai_evaluate, inputs=[context_input, record_inputs,record_inputs2, outputs_qa], outputs=evalution)\n",
" with gr.Tab(\"Quantitative evalution\"):\n",
" table_output = gr.Dataframe(label = \"Some kind of evaluation metrics?\")\n",
" btn6 = gr.Button(value=\"Evaluate\")\n",
" btn6.click(ai_evaluate, inputs=[context_input, record_inputs,record_inputs2, outputs_qa], outputs=table_output)\n",
"\n",
" demo.launch()\n",
" # demo.launch(share=True)\n",
" # demo.launch(debug=True)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 616
},
"id": "EAPljDMYVy3u",
"outputId": "1f347376-14e8-48ea-e531-295a4fefd6cd",
"cellView": "form"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
"Note: opening Chrome Inspector may crash demo inside Colab notebooks.\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"(async (port, path, width, height, cache, element) => {\n",
" if (!google.colab.kernel.accessAllowed && !cache) {\n",
" return;\n",
" }\n",
" element.appendChild(document.createTextNode(''));\n",
" const url = await google.colab.kernel.proxyPort(port, {cache});\n",
"\n",
" const external_link = document.createElement('div');\n",
" external_link.innerHTML = `\n",
" <div style=\"font-family: monospace; margin-bottom: 0.5rem\">\n",
" Running on <a href=${new URL(path, url).toString()} target=\"_blank\">\n",
" https://localhost:${port}${path}\n",
" </a>\n",
" </div>\n",
" `;\n",
" element.appendChild(external_link);\n",
"\n",
" const iframe = document.createElement('iframe');\n",
" iframe.src = new URL(path, url).toString();\n",
" iframe.height = height;\n",
" iframe.allow = \"autoplay; camera; microphone; clipboard-read; clipboard-write;\"\n",
" iframe.width = width;\n",
" iframe.style.border = 0;\n",
" element.appendChild(iframe);\n",
" })(7862, \"/\", \"100%\", 500, false, window.element)"
]
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"source": [
"### Baseline Functionality V2"
],
"metadata": {
"id": "YAKl-5P4dHEF"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"# Oral Exam App\")\n",
" gr.Markdown(\"## OpenAI API key\")\n",
" with gr.Box():\n",
" gr.HTML(\"\"\"Embed your OpenAI API key below; if you haven't created one already, visit\n",
" platform.openai.com/account/api-keys\n",
" to sign up for an account and get your personal API key\"\"\",\n",
" elem_classes=\"textbox_label\")\n",
" input = gr.Textbox(show_label=False, type=\"password\", container=False,\n",
" placeholder=\"βββββββββββββββββ\")\n",
" input.change(fn=embed_key, inputs=input, outputs=None)\n",
"\n",
" with gr.Blocks():\n",
" #########################\n",
" #########Context#########\n",
" #########################\n",
" with gr.Accordion(\"Context section\"):\n",
" ### Should also allow vector stores\n",
" gr.Markdown(\"## Please upload the context document(s) for Oral exam\")\n",
" context_input = gr.File(label=\"Click to upload context file\",\n",
" file_count=\"multiple\",\n",
" file_types=[\".txt\", \".docx\", \".pdf\"])\n",
" outputs_context=gr.Textbox(label=\"Context\")\n",
" context_input.change(fn=process_file, inputs=context_input, outputs=outputs_context)\n",
" # upload_button = gr.Button(value=\"Show context\")\n",
" # upload_button.click(process_file, context_input, outputs_context)\n",
"\n",
" #########################\n",
" ###Question Generation###\n",
" #########################\n",
" with gr.Accordion(\"Question section\"):\n",
" gr.Markdown(\"## Questions\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" outputs_qa=gr.Textbox(label=\"Generate questions\")\n",
" btn1 = gr.Button(value=\"Generate questions\")\n",
" btn1.click(generate_questions_v2, inputs=outputs_context, outputs=outputs_qa)\n",
"\n",
" ######################### Need additional work to include these questions when click button #########################\n",
" with gr.Column():\n",
" submit_question=gr.Textbox(label=\"Use existing questions\")\n",
" btn4 = gr.Button(value=\"Use these questions\")\n",
" # btn4.click(use_this_question, inputs=outputs_transcribe, outputs=None)\n",
"\n",
" #########################\n",
" #######Main Audio########\n",
" #########################\n",
" with gr.Accordion(\"Main audio section\"):\n",
" gr.Markdown(\"## Upload your audio file or start recording\")\n",
" with gr.Column():\n",
" ## uploading files seem not working (don't know why)\n",
" with gr.Row():\n",
" file_input = gr.Audio(label=\"Upload Audio\", source=\"upload\", type=\"filepath\")\n",
" record_inputs = gr.Audio(label=\"Record Audio\", source=\"microphone\", type=\"filepath\")\n",
"\n",
" gr.Markdown(\"## Transcribe the audio uploaded or recorded\")\n",
" outputs_transcribe=gr.Textbox(label=\"Transcription\")\n",
"\n",
" file_input.change(fn=transcribe, inputs=file_input, outputs=outputs_transcribe)\n",
" record_inputs.change(fn=transcribe, inputs=record_inputs, outputs=outputs_transcribe)\n",
"\n",
" #########################\n",
" #######Evaluation########\n",
" #########################\n",
" with gr.Accordion(\"Evaluation section\"):\n",
" gr.Markdown(\"## Evaluation\")\n",
" with gr.Tab(\"General evalution\"):\n",
" evalution=gr.Textbox(label=\"AI Evaluation\")\n",
" btn5 = gr.Button(value=\"Evaluate\")\n",
" btn5.click(ai_evaluate_v2, inputs=[outputs_context, outputs_transcribe, outputs_qa], outputs=evalution)\n",
" with gr.Tab(\"Quantitative evalution\"):\n",
" table_output = gr.Dataframe(label = \"Some kind of evaluation metrics?\")\n",
" btn6 = gr.Button(value=\"Evaluate\")\n",
" btn6.click(ai_evaluate_v2, inputs=[outputs_context, outputs_transcribe, outputs_qa], outputs=table_output)\n",
"\n",
" demo.launch()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 706
},
"id": "04KxUQgUcTrm",
"outputId": "66a9f8c8-36fe-4792-b7d6-3befa6f09269",
"cellView": "form",
"collapsed": true
},
"execution_count": 23,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.10/dist-packages/gradio/utils.py:833: UserWarning: Expected 2 arguments for function <function generate_questions_v2 at 0x7aa8748f9bd0>, received 1.\n",
" warnings.warn(\n",
"/usr/local/lib/python3.10/dist-packages/gradio/utils.py:837: UserWarning: Expected at least 2 arguments for function <function generate_questions_v2 at 0x7aa8748f9bd0>, received 1.\n",
" warnings.warn(\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
"Note: opening Chrome Inspector may crash demo inside Colab notebooks.\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"(async (port, path, width, height, cache, element) => {\n",
" if (!google.colab.kernel.accessAllowed && !cache) {\n",
" return;\n",
" }\n",
" element.appendChild(document.createTextNode(''));\n",
" const url = await google.colab.kernel.proxyPort(port, {cache});\n",
"\n",
" const external_link = document.createElement('div');\n",
" external_link.innerHTML = `\n",
" <div style=\"font-family: monospace; margin-bottom: 0.5rem\">\n",
" Running on <a href=${new URL(path, url).toString()} target=\"_blank\">\n",
" https://localhost:${port}${path}\n",
" </a>\n",
" </div>\n",
" `;\n",
" element.appendChild(external_link);\n",
"\n",
" const iframe = document.createElement('iframe');\n",
" iframe.src = new URL(path, url).toString();\n",
" iframe.height = height;\n",
" iframe.allow = \"autoplay; camera; microphone; clipboard-read; clipboard-write;\"\n",
" iframe.width = width;\n",
" iframe.style.border = 0;\n",
" element.appendChild(iframe);\n",
" })(7863, \"/\", \"100%\", 500, false, window.element)"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [
"def prompt_select(selection, number, length):\n",
" if selection == \"Random\":\n",
" prompt = f\"Please design a {number} question quiz based on the context provided and the inputted learning objectives (if applicable). The types of questions should be randomized (including multiple choice, short answer, true/false, short answer, etc.). Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide 1 question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional chances to respond until I get the correct choice. Explain why the correct choice is right.\"\n",
" elif selection == \"Fill in the Blank\":\n",
" prompt = f\"Create a {number} question fill in the blank quiz refrencing the context provided. The quiz should reflect the learning objectives (if inputted). The 'blank' part of the question should appear as '________'. The answers should reflect what word(s) should go in the blank an accurate statement. An example is the follow: 'The author of the article is ______.' The question should be a statement. Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide ONE question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect,and then give me additional chances to respond until I get the correct choice. Explain why the correct choice is right.\"\n",
" elif selection == \"Short Answer\":\n",
" prompt = f\"Please design a {number} question quiz about which reflects the learning objectives (if inputted). The questions should be short answer. Expect the correct answers to be {length} sentences long. Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide ONE question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional chances to respond until I get the correct choice. Explain why the correct answer is right.\"\n",
" else:\n",
" prompt = f\"Please design a {number} question {selection.lower()} quiz based on the context provided and the inputted learning objectives (if applicable). Provide one question at a time, and wait for my response before providing me with feedback. Again, while the quiz may ask for multiple questions, you should only provide 1 question in you initial response. Do not include the answer in your response. If I get an answer wrong, provide me with an explanation of why it was incorrect, and then give me additional chances to respond until I get the correct choice. Explain why the correct choice is right.\"\n",
" return prompt\n",
"\n",
"\n",
"# Function to save prompts (premade or custom) and return in the user input box in the chatbot`\n",
"saved_text = \"\"\n",
"def save_text(text):\n",
" global saved_text\n",
" saved_text = text\n",
"\n",
"def return_text():\n",
" # Return the saved text\n",
" return saved_text"
],
"metadata": {
"id": "wF80F1wU80rU"
},
"execution_count": 14,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Baseline Functionality V3"
],
"metadata": {
"id": "F5-Ja2evCE4X"
}
},
{
"cell_type": "markdown",
"source": [
"Updated Question Selection and Chatbot Feature"
],
"metadata": {
"id": "rr8YlzcJCKv4"
}
},
{
"cell_type": "code",
"source": [
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"# Oral Exam App\")\n",
" gr.Markdown(\"## OpenAI API key\")\n",
" with gr.Box():\n",
" gr.HTML(\"\"\"Embed your OpenAI API key below; if you haven't created one already, visit\n",
" platform.openai.com/account/api-keys\n",
" to sign up for an account and get your personal API key\"\"\",\n",
" elem_classes=\"textbox_label\")\n",
" input = gr.Textbox(show_label=False, type=\"password\", container=False,\n",
" placeholder=\"βββββββββββββββββ\")\n",
" input.change(fn=embed_key, inputs=input, outputs=None)\n",
"\n",
" with gr.Blocks():\n",
" #########################\n",
" #########Context#########\n",
" #########################\n",
" with gr.Accordion(\"Context section\"):\n",
" ### Should also allow vector stores\n",
" gr.Markdown(\"## Please upload the context document(s) for Oral exam\")\n",
" context_input = gr.File(label=\"Click to upload context file\",\n",
" file_count=\"multiple\",\n",
" file_types=[\".txt\", \".docx\", \".pdf\"])\n",
" outputs_context=gr.Textbox(label=\"Context\")\n",
" context_input.change(fn=process_file, inputs=context_input, outputs=outputs_context)\n",
" # upload_button = gr.Button(value=\"Show context\")\n",
" # upload_button.click(process_file, context_input, outputs_context)\n",
"\n",
" with gr.Blocks():\n",
" gr.Markdown(\"\"\"\n",
" ## Generate a Premade Prompt\n",
" Select your type and number of desired questions. Click \"Generate Prompt\" to get your premade prompt,\n",
" and then \"Insert Prompt into Chat\" to copy the text into the chat interface below. \\\n",
" You can also copy the prompt using the icon in the upper right corner and paste directly into the input box when interacting with the model.\n",
" \"\"\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" question_type = gr.Dropdown([\"Multiple Choice\", \"True or False\", \"Short Answer\", \"Fill in the Blank\", \"Random\"], label=\"Question Type\")\n",
" number_of_questions = gr.Textbox(label=\"Enter desired number of questions\")\n",
" sa_desired_length = gr.Dropdown([\"1-2\", \"3-4\", \"5-6\", \"6 or more\"], label = \"For short answer questions only, choose the desired sentence length for answers. The default value is 1-2 sentences.\")\n",
" with gr.Column():\n",
" prompt_button = gr.Button(\"Generate Prompt\")\n",
" premade_prompt_output = gr.Textbox(label=\"Generated prompt (save or copy)\", show_copy_button=True)\n",
" prompt_button.click(prompt_select,\n",
" inputs=[question_type, number_of_questions, sa_desired_length],\n",
" outputs=premade_prompt_output)\n",
" ########################\n",
" ##Question Generation###\n",
" ########################\n",
" with gr.Accordion(\"Question section\"):\n",
" gr.Markdown(\"## Questions\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" outputs_qa=gr.Textbox(label=\"Generate questions\")\n",
" btn1 = gr.Button(value=\"Generate questions\")\n",
" btn1.click(generate_questions_v2, inputs=[outputs_context, premade_prompt_output], outputs=outputs_qa)\n",
"\n",
" ######################### Need additional work to include these questions when click button #########################\n",
" with gr.Column():\n",
" submit_question=gr.Textbox(label=\"Use existing questions\")\n",
" btn4 = gr.Button(value=\"Use these questions\")\n",
" # btn4.click(use_this_question, inputs=outputs_transcribe, outputs=None)\n",
"\n",
" #########################\n",
" #######Main Audio########\n",
" #########################\n",
" with gr.Accordion(\"Main audio section\"):\n",
" gr.Markdown(\"## Upload your audio file or start recording\")\n",
" with gr.Column():\n",
" ## uploading files seem not working (don't know why)\n",
" with gr.Row():\n",
" file_input = gr.Audio(label=\"Upload Audio\", source=\"upload\", type=\"filepath\")\n",
" record_inputs = gr.Audio(label=\"Record Audio\", source=\"microphone\", type=\"filepath\")\n",
"\n",
" gr.Markdown(\"## Transcribe the audio uploaded or recorded\")\n",
" outputs_transcribe=gr.Textbox(label=\"Transcription\")\n",
"\n",
" file_input.change(fn=transcribe, inputs=file_input, outputs=outputs_transcribe)\n",
" record_inputs.change(fn=transcribe, inputs=record_inputs, outputs=outputs_transcribe)\n",
"\n",
" #########################\n",
" #######Evaluation########\n",
" #########################\n",
" with gr.Accordion(\"Evaluation section\"):\n",
" gr.Markdown(\"## Evaluation\")\n",
" with gr.Tab(\"General evalution\"):\n",
" evalution=gr.Textbox(label=\"AI Evaluation\")\n",
" btn5 = gr.Button(value=\"Evaluate\")\n",
" btn5.click(ai_evaluate_v2, inputs=[outputs_context, outputs_transcribe, outputs_qa], outputs=evalution)\n",
" with gr.Tab(\"Quantitative evalution\"):\n",
" table_output = gr.Dataframe(label = \"Some kind of evaluation metrics?\")\n",
" btn6 = gr.Button(value=\"Evaluate\")\n",
" btn6.click(ai_evaluate_v2, inputs=[outputs_context, outputs_transcribe, outputs_qa], outputs=table_output)\n",
"\n",
"\n",
" # Chatbot (https://gradio.app/creating-a-chatbot/)\n",
" '''\n",
" with gr.Blocks():\n",
" gr.Markdown(\"\"\"\n",
" ## Chat with the Model\n",
" Click \"Display Prompt\" to display the premade or custom prompt that you created earlier. Then, continue chatting with the model.\n",
" \"\"\")\n",
" with gr.Row():\n",
" show_prompt_block = gr.Button(\"Display Prompt\")\n",
" '''\n",
" gr.Markdown(\"## Chat with the Model\")\n",
" with gr.Row(equal_height=True):\n",
" with gr.Column(scale=2):\n",
" chatbot = gr.Chatbot()\n",
" with gr.Row():\n",
" user_chat_input = gr.Textbox(label=\"User input\", scale=9)\n",
" user_chat_input.submit(return_text, inputs=None, outputs=user_chat_input)\n",
" user_chat_submit = gr.Button(\"Ask/answer model\", scale=1)\n",
" #show_prompt_block.click(return_text, inputs=None, outputs=user_chat_input)\n",
"\n",
" # TODO Move the sources so it's displayed to the right of the chat bot,\n",
" # with the sources taking up about 1/3rd of the horizontal space\n",
" # with gr.Box(elem_id=\"sources-container\", scale=1):\n",
" # # TODO: Display document sources in a nicer format?\n",
" # gr.HTML(value=\"<h3 id='sources'>Sources</h3>\")\n",
" # sources_output = []\n",
" # for i in range(num_sources):\n",
" # source_elem = gr.HTML(visible=False)\n",
" # sources_output.append(source_elem)\n",
"\n",
"demo.launch()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 616
},
"id": "Y7-3JFuZ8H5k",
"outputId": "ea99ce65-7b79-4d39-dd88-44785b0d6615"
},
"execution_count": 24,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
"Note: opening Chrome Inspector may crash demo inside Colab notebooks.\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"(async (port, path, width, height, cache, element) => {\n",
" if (!google.colab.kernel.accessAllowed && !cache) {\n",
" return;\n",
" }\n",
" element.appendChild(document.createTextNode(''));\n",
" const url = await google.colab.kernel.proxyPort(port, {cache});\n",
"\n",
" const external_link = document.createElement('div');\n",
" external_link.innerHTML = `\n",
" <div style=\"font-family: monospace; margin-bottom: 0.5rem\">\n",
" Running on <a href=${new URL(path, url).toString()} target=\"_blank\">\n",
" https://localhost:${port}${path}\n",
" </a>\n",
" </div>\n",
" `;\n",
" element.appendChild(external_link);\n",
"\n",
" const iframe = document.createElement('iframe');\n",
" iframe.src = new URL(path, url).toString();\n",
" iframe.height = height;\n",
" iframe.allow = \"autoplay; camera; microphone; clipboard-read; clipboard-write;\"\n",
" iframe.width = width;\n",
" iframe.style.border = 0;\n",
" element.appendChild(iframe);\n",
" })(7864, \"/\", \"100%\", 500, false, window.element)"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": []
},
"metadata": {},
"execution_count": 24
}
]
},
{
"cell_type": "markdown",
"source": [
"### What's left\n",
"- vector store (link) upload\n",
"- submit question section need to be linked with ai_evaluate function"
],
"metadata": {
"id": "g2EVIogW69Fd"
}
}
]
} |