File size: 12,751 Bytes
9e04abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c377810
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e04abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "import pandas as pd\n",
    "from transformers import TapasTokenizer, TapasForQuestionAnswering\n",
    "from transformers import AutoTokenizer, AutoModelForTableQuestionAnswering\n",
    "from transformers import pipeline\n",
    "import PIL\n",
    "\n",
    "# read the module_guide_tableQA\\0915NC_Studienplaetze.jpg as pil image\n",
    "pil_image = PIL.Image.open(\"0915NC_Studienplaetze.jpg\")\n",
    "# make that image a bit less high\n",
    "pil_image = pil_image.resize((int(pil_image.width * 0.5), int(pil_image.height * 0.5)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_answer(\n",
    "    dropdown,\n",
    "    question,\n",
    "    view_as_table=False,\n",
    "    model=\"google/tapas-finetuned-wtq\",\n",
    "    #progress=gr.Progress(),\n",
    "):\n",
    "    #progress(0, desc=\"Looking for answer in module guide...\")\n",
    "    df = pd.DataFrame()\n",
    "    if dropdown == \"Master Information Systems\":\n",
    "        df = pd.read_excel(\n",
    "            r\"03_extracted_final_modules\\MS_IS_all_modules_orginal_15_rows_cleaned.xlsx\"\n",
    "        )\n",
    "    elif dropdown == \"Bachelor Information Systems\":\n",
    "        df = pd.read_excel(r\"03_extracted_final_modules\\BA_IS_all_modules_15.xlsx\")\n",
    "    elif dropdown == \"Bachelor Management\":\n",
    "        df = pd.read_excel(r\"03_extracted_final_modules\\BA_MM_all_modules_15.xlsx\")\n",
    "    df = df.astype(str)\n",
    "    print(question)\n",
    "    question = str(question)\n",
    "    print(df.shape)\n",
    "    question = [question]\n",
    "\n",
    "    if model == \"google/tapas-finetuned-wtq\":\n",
    "        tqa = pipeline(\n",
    "            task=\"table-question-answering\", model=\"google/tapas-base-finetuned-wtq\"\n",
    "        )\n",
    "    elif model == \"google/tapas-large-finetuned-wtq\":\n",
    "        tqa = pipeline(\n",
    "            task=\"table-question-answering\", model=\"google/tapas-large-finetuned-wtq\"\n",
    "        )\n",
    "\n",
    "    results = tqa(table=df, query=question)\n",
    "    print(results)\n",
    "    cells_input = results[\"cells\"]\n",
    "    cells_input = str(cells_input)\n",
    "    cells_input = cells_input.replace(\"[\", \"\")\n",
    "    cells_input = cells_input.replace(\"]\", \"\")\n",
    "    cells_input = cells_input.replace(\"'\", \"\")\n",
    "\n",
    "    print(cells_input)\n",
    "    print(results)\n",
    "    html_string_short = f\"<h1>Short Answer:</h1><p>{cells_input}</p>\"\n",
    "    row_numbers = [coord[0] for coord in results[\"coordinates\"]]\n",
    "    df_short = df.iloc[row_numbers]\n",
    "    df_short = df_short.dropna(axis=1, how=\"all\")\n",
    "    df_short = df_short.loc[:, (df_short != \"--\").any(axis=0)]\n",
    "    html_table = (\n",
    "        f\"<hr><h2>Complete Module(s):</h2><p>{df_short.to_html(index=False)}</p>\"\n",
    "    )\n",
    "\n",
    "    # check if there are more than 1 rows in df_short\n",
    "    html_string = \"\"\n",
    "    if df_short.shape[0] > 1 or view_as_table == True:\n",
    "        html_string = html_table\n",
    "    elif df_short.shape[0] == 1:\n",
    "        html_string = \"\"\"<html>\n",
    "        <head>\n",
    "            <style>\n",
    "                .module-info {\n",
    "                    margin-bottom: 20px;\n",
    "                }\n",
    "                .module-info h3, .module-info p {\n",
    "                    margin: 0;\n",
    "                }\n",
    "            </style>\n",
    "        </head>\n",
    "        <body>\n",
    "            <hr>\n",
    "            <h1>Detailed Module Information</h1>\n",
    "\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Module title:</h3>\n",
    "                <p>Project Seminar</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Abbreviation:</h3>\n",
    "                <p>12-PS-192-m01</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Module coordinator:</h3>\n",
    "                <p>Faculty of Business Management and Economics</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Module offered by:</h3>\n",
    "                <p>Holder of the Chair of Business Management and Business</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>ETCS:</h3>\n",
    "                <p>15</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Method of grading:</h3>\n",
    "                <p>numerical grade</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Duration:</h3>\n",
    "                <p>1 semester</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Module level:</h3>\n",
    "                <p>graduate</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Contents:</h3>\n",
    "                <p>In small project teams of 4 to 10 members, students will spend several months actively working on a specific and realistic problem with practical relevance. They will progress through several project stages including as-is analysis, to-be conception and implementation of an IS solution. The project teams will be required to work independently and will only receive advice and minor support from research assistants.</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Intended learning outcomes:</h3>\n",
    "                <ul>\n",
    "                    <li>Analyze business tasks and requirements and generate fitting IS solutions</li>\n",
    "                    <li>Apply project management methods</li>\n",
    "                    <li>Internalize stress, time and conflict management by means of practical teamwork</li>\n",
    "                </ul>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Courses:</h3>\n",
    "                <p>Project: preparing a conceptual design (approx. 150 hours), designing and implementing an approach to solution (approx. 300 hours) as well as presentation (approx. 20 minutes), weighted 1:2:1</p>\n",
    "                <p>Language of assessment: German, English</p>\n",
    "                <p>Creditable for bonus</p>\n",
    "            </div>\n",
    "            <div class=\"module-info\">\n",
    "                <h3>Workload:</h3>\n",
    "                <p>450 hours</p>\n",
    "            </div>\n",
    "        </body>\n",
    "        </html>\"\"\"\n",
    "    else:\n",
    "        html_string = \"\"\n",
    "\n",
    "    return html_string_short, html_string\n",
    "\n",
    "\n",
    "def change_html_link(dropdown_item):\n",
    "    html_link = \"\"\n",
    "    if dropdown_item == \"Master Information Systems\":\n",
    "        html_link = f'<p>View complete pdf here:  <a href=\"https://www2.uni-wuerzburg.de/mhb/MHB1-en-88-i45-H-2018.pdf\" target=\"_blank\">{dropdown_item}</a></p> <p>Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.</p>'\n",
    "    elif dropdown_item == \"Bachelor Information Systems\":\n",
    "        html_link = f'View complete pdf here:  <a href=\"https://www2.uni-wuerzburg.de/mhb/MHB1-en-82-277-H-2021.pdf\" target=\"_blank\">{dropdown_item}</a></p> <p>Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.</p>'\n",
    "    elif dropdown_item == \"Bachelor Management\":\n",
    "        html_link = f'View complete pdf here: <a href=\"https://www2.uni-wuerzburg.de/mhb/MHB1-en-82-184-H-2008.pdf\" target=\"_blank\">{dropdown_item}</a></p> <p>Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.</p>'\n",
    "\n",
    "    return html_link"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7867\n",
      "Running on public URL: https://a7a23badcaa31f041e.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://a7a23badcaa31f041e.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Keyboard interruption in main thread... closing server.\n",
      "Killing tunnel 127.0.0.1:7867 <> https://a7a23badcaa31f041e.gradio.live\n"
     ]
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with gr.Blocks() as demo:    \n",
    "    gr.HTML(\n",
    "    \"\"\"\n",
    "    <div style=\"text-align: center;\">\n",
    "        <img src=\"file/0915NC_Studienplaetze.jpg\" alt=\"Module Guide Header Image\" width=\"500\">\n",
    "    </div>\n",
    "    \"\"\"\n",
    "    )\n",
    "\n",
    "    gr.HTML(\n",
    "        \"<h1>Your Module Guide Assistant</h1>\"\n",
    "    )\n",
    "    table = gr.Dropdown(\n",
    "        [\n",
    "            \"Master Information Systems\",\n",
    "            \"Bachelor Information Systems\",\n",
    "            \"Bachelor Management\",\n",
    "        ],\n",
    "        label=\"Select Module Guide\",\n",
    "        value=\"Master Information Systems\",\n",
    "    )\n",
    "    html_link = gr.HTML(\n",
    "        \"\"\"\n",
    "        <p>View complete PDF here: <a href=\"https://www2.uni-wuerzburg.de/mhb/MHB1-en-88-j10-H-2019.pdf\" target=\"_blank\">Master Information Systems</a></p>\n",
    "        <p>Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.</p>\n",
    "    \"\"\"\n",
    "    )\n",
    "\n",
    "    table.change(change_html_link, table, html_link)\n",
    "    question = gr.Textbox(\n",
    "        label=\"Question\", value=\"How many ECTS credits does the project seminar have?\"\n",
    "    )\n",
    "    with gr.Accordion(\"Advanced Options\", open=False):\n",
    "        with gr.Group():\n",
    "            model_selction = gr.Dropdown(\n",
    "                [\n",
    "                    \"google/tapas-finetuned-wtq\",\n",
    "                    \"google/tapas-large-finetuned-wtq\",\n",
    "                ],\n",
    "                label=\"Select Model\",\n",
    "                value=\"google/tapas-finetuned-wtq\",\n",
    "            )\n",
    "            view_as_table_or_text = gr.Checkbox(\n",
    "                label=\"View detailed information as table\", value=False\n",
    "            )\n",
    "\n",
    "    ask_btn = gr.Button(\"Ask The Assistant\")\n",
    "    gr.HTML(\"<hr>\")\n",
    "    inputs = [table, question, view_as_table_or_text, model_selction]\n",
    "    output_question = gr.HTML(label=\"Answer\")\n",
    "    outout_full_module = gr.HTML(label=\"Detailed Description\")\n",
    "    outputs = [output_question, outout_full_module]\n",
    "    ask_btn.click(fn=get_answer, inputs=inputs, outputs=outputs, api_name=\"greet\")\n",
    "\n",
    "demo.launch(debug=True, share=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "enterpriseai2",
   "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.8.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}