Team Ai
Apppublic

ramhemanth580/NL_2_SQL_Data_Analysis_Chatbot

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py56 linesDownload Raw Back to root
1import streamlit as st2import os3from dotenv import load_dotenv4import google.generativeai as genai5from langchain_google_genai import ChatGoogleGenerativeAI6from langchain_utils import  get_chain7from langchain.memory import ChatMessageHistory8from PIL import Image9 10st.title("Langchain NL2SQL Chatbot")11 12# Set Google GenAI API key from Streamlit secrets13#client = OpenAI(api_key="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")14 15load_dotenv()16genai.configure(api_key=os.environ["GOOGLE_API_KEY"])17llm = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0,convert_system_message_to_human=True)18 19# Set a default model20if "Gemini_model" not in st.session_state:21    st.session_state["Gemini_model"] = "gemini-pro"22 23history = ChatMessageHistory()24 25if "messages" not in st.session_state:26    # print("Creating session state")27    st.session_state.messages = []28 29def invoke_chain(question,messages):30    chain = get_chain()31    #history = create_history(messages)32    response = chain.invoke({"question": question,"top_k":3,"messages":history.messages})33    # history.add_user_message(question)34    # history.add_ai_message(response)35    return response36 37question = st.text_input("Ask a Question about the database")38 39 40# if question :41#     st.session_state.messages.append({"role": "user", "content": question})42#     history.add_user_message(question)43#     response = invoke_chain(question, st.session_state.messages)44#     history.add_ai_message(response)45#     st.session_state.messages.append({"role": "assistant", "content": response})46if st.button("submit") :47    if question  :48        response = invoke_chain(question, st.session_state.messages)49        st.markdown(response)50 51# Set up the sidebar with a button52st.sidebar.title("Database Info")53if st.sidebar.button('Show Database Schema'):54    # Display the database schema image when the button is clicked55    image = Image.open('database_schema.PNG')56    st.image(image, caption='Database Schema', use_column_width=True)