File size: 4,898 Bytes
6689e6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""
Created on Fri May 26 14:07:22 2023

@author: vibin
"""

import streamlit as st
from pandasql import sqldf
import pandas as pd
import re
from typing import List
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
import re


@st.cache_resource()
def tapas_model():
    return(pipeline(task="table-question-answering", model="google/tapas-base-finetuned-wtq"))

@st.cache_resource()
def prepare_input(question: str, table: List[str]):
    table_prefix = "table:"
    question_prefix = "question:"
    join_table = ",".join(table)
    inputs = f"{question_prefix} {question} {table_prefix} {join_table}"
    input_ids = tokenizer(inputs, max_length=512, return_tensors="pt").input_ids
    return input_ids

@st.cache_resource()
def inference(question: str, table: List[str]) -> str:
    input_data = prepare_input(question=question, table=table)
    input_data = input_data.to(model.device)
    outputs = model.generate(inputs=input_data, num_beams=10, top_k=10, max_length=700)
    result = tokenizer.decode(token_ids=outputs[0], skip_special_tokens=True)
    return result

@st.cache_resource()
def tokmod(tok_md):
    tkn = AutoTokenizer.from_pretrained(tok_md)
    mdl = AutoModelForSeq2SeqLM.from_pretrained(tok_md)
    return(tkn,mdl)


### Main

nav = st.sidebar.radio("Navigation",["TAPAS","Text2SQL"])
if nav == "TAPAS":
    
    col1 , col2, col3 = st.columns(3)
    col2.title("TAPAS")
    
    col3 , col4 = st.columns([3,12])
    col4.text("Tabular Data Text Extraction using text")
    
    table = pd.read_csv("data.csv")
    table = table.astype(str)
    st.text("DataSet - ")
    st.dataframe(table,width=3000,height= 400)
    
    st.title("")
    
    lst_q = ["Which country has low medicare","Who are the patients from india","Who are the patients from india","Patients who have Edema","CUI code for diabetes patients","Patients having oxygen less than 94 but 91"]
   
    v2 = st.selectbox("Choose your text",lst_q,index = 0)

    st.title("")
    
    sql_txt = st.text_area("TAPAS Input",v2)
    
    if st.button("Predict"):   
        tqa = tapas_model()
        txt_sql = tqa(table=table, query=sql_txt)["answer"]
        st.text("Output - ") 
        st.success(f"{txt_sql}")
        # st.write(all_students)
    
    
    
elif nav == "Text2SQL":
    
    ### Function
    col1 , col2, col3 = st.columns(3)
    col2.title("Text2SQL")
    
    col3 , col4 = st.columns([1,20])
    col4.text("Text will be converted to SQL Query and can extract the data from DataSet")
    
    # Import Data
    
    df_qna = pd.read_csv("qnacsv.csv", encoding= 'unicode_escape')
    
    st.title("")
    
    st.text("DataSet - ")
    st.dataframe(df_qna,width=3000,height= 500)
    
    st.title("")
    
    lst_q = ["what interface is measure indicator code = 72_HR_ABX and version is 1 and source is TD", "get class code with measure = 72_HR_ABX", "get sum of version for Class_Code is Antibiotic Stewardship", "what interface is measure indicator code = 72_HR_ABX"]
    v2 = st.selectbox("Choose your text",lst_q,index = 0)

    st.title("")
    
    
    sql_txt = st.text_area("Text for SQL Conversion",v2)
    
    
    if st.button("Predict"):
        
        tok_model = "juierror/flan-t5-text2sql-with-schema"
        tokenizer,model = tokmod(tok_model)
        
        # text = "what interface is measure indicator code = 72_HR_ABX and version is 1 and source is TD"
        table_name = "df_qna"
        table_col = ["Type","Class_Code", "Version","Measure_Indicator_Code","Measure_Indicator_Name","Description_Definition", "Source", "Interfaces"]
        
        txt_sql = inference(question=sql_txt, table=table_col)
        
        
        ### SQL Modification
        sql_avg = ["AVG","COUNT","DISTINCT","MAX","MIN","SUM"]
        txt_sql = txt_sql.replace("table",table_name)
        sql_quotes = []
        for match in re.finditer("=",txt_sql):
            new_txt = txt_sql[match.span()[1]+1:]
            try:
                match2 = re.search("AND",new_txt)
                sql_quotes.append((new_txt[:match2.span()[0]]).strip())
            except:
                sql_quotes.append(new_txt.strip())
        
        for i in sql_quotes:
            qts = "'" + i + "'"
            txt_sql = txt_sql.replace(i, qts)
            
        for r in sql_avg:
            if r in txt_sql:
                rr = re.search(rf"{r} (\w+)", txt_sql)
                init = " " + rr[1]
                qts = "(" + rr[1] + ")"
                txt_sql = txt_sql.replace(init,qts)
            else:
                pass
            
            
        st.success(f"{txt_sql}")
        all_students = sqldf(txt_sql)
        
        st.text("Output - ")
        st.write(all_students)