#!/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"
{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"{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 = """Project Seminar
12-PS-192-m01
Faculty of Business Management and Economics
Holder of the Chair of Business Management and Business
15
numerical grade
1 semester
graduate
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.
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
450 hours
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( """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("