Heisenberg08/Text2SQL
1
1import streamlit as st2import torch3import transformers4from transformers import AutoTokenizer, AutoModelWithLMHead5 6device=torch.device("cuda" if torch.cuda.is_available() else "cpu")7# device=torch.device("cpu")8 9tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M")10# model=torch.load("Gpt_neo_Epoch_10_Loss_031_data_5000.pth",map_location=torch.device('cpu'))11torch.manual_seed(0)12model=torch.load("Gpt_neo_Epoch_10_Loss_031_data_5000.pth",map_location=device)13 14def predict_query(input_sentence,max_len=40,temp=0.7):15 pred=[]16 seq=tokenizer(input_sentence,return_tensors='pt')['input_ids'].to(device)17 outputs=model.generate(seq,18 max_length=max_len,19 do_sample=True,20 top_p=0.95,21 #num_beams=5,22 temperature=temp,23 no_repeat_ngram_size=3,24 num_return_sequences=525 ).to(device)26 for i,out in enumerate(outputs):27 out=tokenizer.decode(out, skip_special_tokens=True)28 idx=out.find("<|sep|>")+729 out=out[idx:]30 # print(f"Sugestion{i} :{out}")31 print("Sugestion: ",out)32 33 pred.append(out)34 return pred35# option = st.selectbox( 36# 'Please Select option',37# ('Predictive writing',"None"),index=1)38 39 40st.title("Text2SQL")41st.write('# Generate SQL Query with Natural Language sentence')42st.markdown("Creator: [Pranav Kushare] (https://github.com/Pranav082001)")43 44st.sidebar.markdown(45 ''' 46 ## Select Hyperparameters47''')48max_len = st.sidebar.slider(label='Output Size', min_value=1, max_value=150, value=40, step=1)49# samples = st.sidebar.slider(label='Number of Samples', min_value=1, max_value=50, value=10, step=1)50temp = st.sidebar.slider(label='Temperature (Creativity)', min_value=0.0, max_value=2.0, value=0.7, step=0.1)51# temp = st.sidebar.slider(label='Temperature', min_value=0.1, max_value=1.0, value=5.0, step=0.05)52# do_sample=st.sidebar.checkbox("do_sample")53 54 55 56# max_len=st.slider("max_len",1,100,None,1,key="max_len")57# top_k=st.slider("top_k",1,50,None,1)58# do_sample=st.checkbox("do_sample")59# print(max_len)60sentence = st.text_area('Input your sentence here:') 61st.markdown('Example: "Find Average Salary of Employees"')62Enter=st.button("Generate")63clear=st.button("Clear")64 65if clear:66 print(clear)67 st.markdown(' ')68 69if Enter:70 st.header("Output-")71 print("Generating predictions......\n\n")72 # out=generate(sentence,max_len,top_k,do_sample)73 torch.manual_seed(0)74 out=predict_query(sentence,max_len,temp)75 for i,out in enumerate(out):76 st.markdown(f"Query {i} :{out}")77 