alejandro commited on
Commit
ec96023
·
1 Parent(s): 7df1f40

feat: add database connection

Browse files
Files changed (2) hide show
  1. requirements.txt +6 -0
  2. src/app.py +17 -0
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit==1.31.1
2
+ langchain==0.1.8
3
+ langchain-community==0.0.21
4
+ langchain-core==0.1.24
5
+ langchain-openai==0.0.6
6
+ mysql-connector-python==8.3.0
src/app.py CHANGED
@@ -1,4 +1,10 @@
1
  import streamlit as st
 
 
 
 
 
 
2
 
3
  st.set_page_config(initial_sidebar_state="expanded", page_title="Chat with a MySQL Database", page_icon=":speech_balloon:")
4
 
@@ -12,6 +18,17 @@ with st.sidebar:
12
  st.text_input("Password", key="password")
13
  st.text_input("Database", key="database")
14
 
 
 
 
 
 
 
 
 
 
 
 
15
  user_query = st.chat_input("Type a message...")
16
 
17
  if user_query != "":
 
1
  import streamlit as st
2
+ from langchain_community.utilities import SQLDatabase
3
+
4
+ def initialize_database(host, port, username, password, database):
5
+ db_uri = f"mysql+mysqlconnector://{username}:{password}@{host}:{port}/{database}"
6
+ return SQLDatabase.from_uri(db_uri)
7
+
8
 
9
  st.set_page_config(initial_sidebar_state="expanded", page_title="Chat with a MySQL Database", page_icon=":speech_balloon:")
10
 
 
18
  st.text_input("Password", key="password")
19
  st.text_input("Database", key="database")
20
 
21
+ if st.button("Connect"):
22
+ with st.spinner("Connecting to the database..."):
23
+ db = initialize_database(
24
+ username=st.session_state.username,
25
+ password=st.session_state.password,
26
+ host=st.session_state.name,
27
+ port=st.session_state.port,
28
+ database=st.session_state.database
29
+ )
30
+ st.success("Connected to the database!")
31
+
32
  user_query = st.chat_input("Type a message...")
33
 
34
  if user_query != "":