import streamlit as st from My_SQL_Connection import database_details, tables_in_this_DB, printing_tables, create_table_command from streamlit_option_menu import option_menu from model_functions import LOAD_GEMMA import torch user_name = 'arya' if 'localhost' not in st.session_state: st.session_state.localhost = '' st.session_state.user = '' st.session_state.password = '' st.session_state.table_commands = """ """ with st.sidebar: selected = option_menu("Querio Lingua", ["Log In", 'main functionalities','Chat with AI'], icons=['house', 'gear', 'robot'], menu_icon="cast", default_index=1) if selected == 'Log In': st.title(f'welcome to our web application :green[{user_name}]') st.subheader('welcome to our MY SQL Database Explorer ~ ') st.write('First Check how many databases you have in your server ~') st.session_state.localhost = st.text_input("what is your host, (localhost if in local) or give the url", 'localhost',help='host') st.session_state.user = st.text_input("what is your user name (usually root)", 'root') st.session_state.password = st.text_input('Password', type='password') elif selected == 'main functionalities': st.title(f'welcome to our web application :green[{user_name}]') st.subheader('welcome to our MY SQL Database Explorer ~ ') if st.button('All your databases ~ '): db, l = database_details(st.session_state.localhost, st.session_state.user, st.session_state.password) st.table(db) st.subheader('Now we will see details of any database~ ') st.session_state.db_name = st.text_input('Which Database you want') if st.button('All tables present in that particular database'): if not st.session_state.db_name: st.warning('Input database name first') else: tables, l = tables_in_this_DB(st.session_state.localhost, st.session_state.user, st.session_state.password, st.session_state.db_name) st.write(f'There is only {l} tables present in this database') st.write(tables) st.subheader('check out tables~ ') if st.button('Print the tables~'): tables_data = printing_tables(st.session_state.localhost, st.session_state.user, st.session_state.password, st.session_state.db_name) for table_name, table_data in tables_data.items(): st.write(f"Table: {table_name}") st.table(table_data) st.subheader('Retrieve the CREATE TABLE Statements') if st.button('Generate statements'): statements = create_table_command(st.session_state.localhost, st.session_state.user, st.session_state.password, st.session_state.db_name) for table_name, table_statements in statements.items(): st.write(f'{table_name}') st.session_state.table_commands = table_statements st.code(table_statements) elif selected == 'Chat with AI': #st.set_page_config(page_title='🧠MemoryBot🤖', layout='wide') # Initialize session states if "generated" not in st.session_state: st.session_state["generated"] = [] if "past" not in st.session_state: st.session_state["past"] = [] if "input" not in st.session_state: st.session_state["input"] = "" if "stored_session" not in st.session_state: st.session_state["stored_session"] = [] def get_text(): """ Get the user input text. Returns: (str): The text entered by the user """ input_text = st.text_input("You: ", st.session_state["input"], key="input", placeholder="Your AI assistant here! Ask me anything ...", label_visibility='hidden') return input_text def new_chat(): """ Clears session state and starts a new chat. """ save = [] for i in range(len(st.session_state['generated'])-1, -1, -1): save.append("User:" + st.session_state["past"][i]) save.append("Bot:" + st.session_state["generated"][i]) st.session_state["stored_session"].append(save) st.session_state["generated"] = [] st.session_state["past"] = [] st.session_state["input"] = "" with st.sidebar.expander("🛠️ ", expanded=False): MODEL = st.selectbox(label='Model', options=['GEMMA-2B FINE TUNED']) st.title("🤖 Chat Bot with 🧠") st.subheader(" Powered by 🚀 GEMMA") st.sidebar.button("New Chat", on_click = new_chat, type='primary') user_input = get_text() if user_input: tokenizer,model = LOAD_GEMMA() device = torch.device("cpu") alpeca_prompt = f"""Below are sql tables schemas paired with instruction that describes a task. Using valid SQLite, write a response that appropriately completes the request for the provided tables. ### Instruction: {user_input}. ### Input: {st.session_state.table_commands} ### Response: """ with st.status('Generating Result',expanded=False) as status: inputs = tokenizer([alpeca_prompt], return_tensors="pt").to(device) outputs = model.generate(**inputs, max_new_tokens=30) output = tokenizer.decode(outputs[0], skip_special_tokens=False) st.session_state.past.append(user_input) st.session_state.generated.append(output) print(output) status.update(label="Result Generated!", state="complete", expanded=False) download_str = [] # Display the conversation history using an expander, and allow the user to download it with st.expander("Conversation", expanded=True): for i in range(len(st.session_state['generated'])-1, -1, -1): st.info(st.session_state["past"][i],icon="🧐") st.success(st.session_state["generated"][i], icon="🤖") download_str.append(st.session_state["past"][i]) download_str.append(st.session_state["generated"][i]) # Can throw error - requires fix download_str = '\n'.join(download_str) if download_str: st.download_button('Download',download_str) # Display stored conversation sessions in the sidebar for i, sublist in enumerate(st.session_state.stored_session): with st.sidebar.expander(label= f"Conversation-Session:{i}"): st.write(sublist) # Allow the user to clear all stored conversation sessions if st.session_state.stored_session: if st.sidebar.button("Clear-all"): del st.session_state.stored_session