import tensorflow as tf import tensorflowhub as hub import pandas as pd import re import…
- User
import tensorflow as tf import tensorflow_hub as hub import pandas as pd import re import numpy as np
import gradio as gr import sqlite3 import torch from transformers import AutoTokenizer, AutoModelForCausalLM from nltk.stem import PorterStemmer
from queue import Queue from nltk.stem import PorterStemmer
Load similarity model
from sentence_transformers import SentenceTransformer retrieval_model_path = "NlpSql//model_jatin//" retrieval_model = SentenceTransformer(retrieval_model_path)
Load LLM model
model_path = "weights/sql-weights" tokenizer = AutoTokenizer.from_pretrained(model_path) model = AutoModelForCausalLM.from_pretrained(model_path) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device)
Load data
data = pd.read_csv('NlpSql/Data/data_1.txt', sep="\t", header=None)
headers = data.iloc[0] new_df = pd.DataFrame(data.values[1:], columns=headers)
Preprocess data
new_df['Question'] = new_df['Question'].str.replace('-',' ') new_df['Question'] = new_df['Question'].str.lower()
data_list = new_df["Question"].to_list() fin = [re.sub(r'\r\n', '', line) for line in data_list] data_emb = retrieval_model.encode(fin)
Define dummy users for authentication
dummy_users = [ {"username": "jatin", "password": "qwer@345"}, {"username": "ajoy", "password": "qwer@345"}, {"username": "user3", "password": "pass3"}, {"username": "user4", "password": "pass4"}, {"username": "user5", "password": "pass5"} ]
Authenticate user function
def authenticate_user(username, password): for user in dummy_users: if user["username"] == username and user["password"] == password: return True return False
Function to get most similar question
def get_most_similar_question(userText, history): print(userText)
codeinp_emb = retrieval_model.encode([userText]) corr = np.inner(inp_emb, data_emb) flat_list = [item for sublist in corr for item in sublist] top_1_idx = np.argsort(flat_list)[-1:] top_1_values = [flat_list[i] for i in top_1_idx] if top_1_values[0] < 0.7: return predict(userText, history) else: n_data = new_df.iloc[top_1_idx, [1]] df_html_with_sql = n_data.to_html(index=False) return df_html_with_sqlporter_stemmer =PorterStemmer() #-------------------------------------- def fn_preprocess_question(question): return ' '.join([porter_stemmer.stem(word) for word in question.split()]) dict_predefined_answers ={ "What is the highest selling region?": "SELECT Region, Max(counterselloutval) from sales_dlb Group by region;", "Who are you":"I am SAMS bot" } #----- def predict(question,history): try:
- Assistant
- User
how to run this streamlit app in jupyter notebook at 8282 port
- Assistant
To run a Streamlit app in a Jupyter notebook, you'll need to use an IPython magic command that lets you run shell commands directly from the notebook cells. However, please note that Streamlit apps typically run as standalone scripts and are not typically executed from within a notebook.
- User
i wnat to run at ipynb file not py
- Assistant
If you're looking to run the Streamlit app defined within a Jupyter notebook (
.ipynbfile) rather than a separate Python script (.pyfile), you'll need to export the code from the notebook cells into a Python script and then run the Streamlit app using the method discussed earlier.First, convert your Jupyter notebook cells containing the Streamlit app code into a Python script. You can do this manually by copying the code into a new
.pyfile, or programmatically using Jupyter's nbconvert:python# In a Jupyter notebook cell, use nbconvert to convert the current notebook to a Python script !jupyter nbconvert --to script your_notebook.ipynb --output streamlit_app