#!/usr/bin/env python # coding: utf-8 # In[ ]: import gradio as gr import pandas as pd from transformers import TapasTokenizer, TapasForQuestionAnswering from transformers import AutoTokenizer, AutoModelForTableQuestionAnswering from transformers import pipeline import PIL # read the module_guide_tableQA\0915NC_Studienplaetze.jpg as pil image pil_image = PIL.Image.open("0915NC_Studienplaetze.jpg") # make that image a bit less high pil_image = pil_image.resize((int(pil_image.width * 0.5), int(pil_image.height * 0.5))) # In[ ]: def get_answer( dropdown, question, view_as_table=False, model="google/tapas-finetuned-wtq", #progress=gr.Progress(), ): #progress(0, desc="Looking for answer in module guide...") df = pd.DataFrame() if dropdown == "Master Information Systems": df = pd.read_excel( r"03_extracted_final_modules\MS_IS_all_modules_orginal_15_rows_cleaned.xlsx" ) elif dropdown == "Bachelor Information Systems": df = pd.read_excel(r"03_extracted_final_modules\BA_IS_all_modules_15.xlsx") elif dropdown == "Bachelor Management": df = pd.read_excel(r"03_extracted_final_modules\BA_MM_all_modules_15.xlsx") df = df.astype(str) print(question) question = str(question) print(df.shape) question = [question] if model == "google/tapas-finetuned-wtq": tqa = pipeline( task="table-question-answering", model="google/tapas-base-finetuned-wtq" ) elif model == "google/tapas-large-finetuned-wtq": tqa = pipeline( task="table-question-answering", model="google/tapas-large-finetuned-wtq" ) results = tqa(table=df, query=question) print(results) cells_input = results["cells"] cells_input = str(cells_input) cells_input = cells_input.replace("[", "") cells_input = cells_input.replace("]", "") cells_input = cells_input.replace("'", "") print(cells_input) print(results) html_string_short = f"

Short Answer:

{cells_input}

" row_numbers = [coord[0] for coord in results["coordinates"]] df_short = df.iloc[row_numbers] df_short = df_short.dropna(axis=1, how="all") df_short = df_short.loc[:, (df_short != "--").any(axis=0)] html_table = ( f"

Complete Module(s):

{df_short.to_html(index=False)}

" ) # check if there are more than 1 rows in df_short html_string = "" if df_short.shape[0] > 1 or view_as_table == True: html_string = html_table elif df_short.shape[0] == 1: html_string = """

Detailed Module Information

Module title:

Project Seminar

Abbreviation:

12-PS-192-m01

Module coordinator:

Faculty of Business Management and Economics

Module offered by:

Holder of the Chair of Business Management and Business

ETCS:

15

Method of grading:

numerical grade

Duration:

1 semester

Module level:

graduate

Contents:

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.

Intended learning outcomes:

Courses:

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

Language of assessment: German, English

Creditable for bonus

Workload:

450 hours

""" else: html_string = "" return html_string_short, html_string def change_html_link(dropdown_item): html_link = "" if dropdown_item == "Master Information Systems": html_link = f'

View complete pdf here: {dropdown_item}

Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.

' elif dropdown_item == "Bachelor Information Systems": html_link = f'View complete pdf here: {dropdown_item}

Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.

' elif dropdown_item == "Bachelor Management": html_link = f'View complete pdf here: {dropdown_item}

Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.

' return html_link # In[20]: with gr.Blocks() as demo: gr.HTML( """
Module Guide Header Image
""" ) gr.HTML( "

Your Module Guide Assistant

" ) table = gr.Dropdown( [ "Master Information Systems", "Bachelor Information Systems", "Bachelor Management", ], label="Select Module Guide", value="Master Information Systems", ) html_link = gr.HTML( """

View complete PDF here: Master Information Systems

Ask whatever you want to know about the module guide here. You can ask formality-based and content-based questions.

""" ) table.change(change_html_link, table, html_link) question = gr.Textbox( label="Question", value="How many ECTS credits does the project seminar have?" ) with gr.Accordion("Advanced Options", open=False): with gr.Group(): model_selction = gr.Dropdown( [ "google/tapas-finetuned-wtq", "google/tapas-large-finetuned-wtq", ], label="Select Model", value="google/tapas-finetuned-wtq", ) view_as_table_or_text = gr.Checkbox( label="View detailed information as table", value=False ) ask_btn = gr.Button("Ask The Assistant") gr.HTML("
") inputs = [table, question, view_as_table_or_text, model_selction] output_question = gr.HTML(label="Answer") outout_full_module = gr.HTML(label="Detailed Description") outputs = [output_question, outout_full_module] ask_btn.click(fn=get_answer, inputs=inputs, outputs=outputs, api_name="greet") demo.launch(debug=True, share=True)