HassanSaado commited on
Commit
cc900e1
·
1 Parent(s): cb85166

first commit

Browse files
Files changed (4) hide show
  1. .gitignore +6 -0
  2. README.md +2 -2
  3. app.py +76 -0
  4. requirements.txt +82 -0
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ Chinook_Sqlite.sql.txt
2
+ test_notebook.ipynb
3
+ app_class.py
4
+ .env
5
+ SQL_chat/
6
+ __pycache__/
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: SQL Chatbot
3
  emoji: 💻
4
- colorFrom: yellow
5
- colorTo: pink
6
  sdk: streamlit
7
  sdk_version: 1.33.0
8
  app_file: app.py
 
1
  ---
2
  title: SQL Chatbot
3
  emoji: 💻
4
+ colorFrom: white
5
+ colorTo: black
6
  sdk: streamlit
7
  sdk_version: 1.33.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ from langchain_community.utilities import SQLDatabase
4
+ from langchain_google_cloud_sql_mysql import MySQLEngine
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain_community.agent_toolkits import create_sql_agent
7
+ from dotenv import load_dotenv
8
+ load_dotenv()
9
+ import streamlit as st
10
+ import random
11
+ import time
12
+
13
+ # Create a SQL chatbot instance
14
+ username = "root"
15
+ host = "35.204.50.120"
16
+ port = "3306"
17
+ mydatabase = "Chinook"
18
+ pg_uri = f"mysql+pymysql://{username}:{password}@{host}:{port}/{mydatabase}"
19
+ db = SQLDatabase.from_uri(pg_uri)
20
+ llm = ChatOpenAI(model="gpt-4-turbo", temperature=0)
21
+ agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=False)
22
+
23
+ #sidebar
24
+ st.sidebar.title("SQL Chatbot")
25
+ st.sidebar.write("This is a simple chatbot that can answer your SQL queries.")
26
+ #list of the tables in database
27
+ tables = db.get_table_names()
28
+ st.sidebar.write("Tables in the database:")
29
+ st.sidebar.write(tables)
30
+
31
+ yes_list = ["yes","Yes", "yes", "YES", "Y", "y","yues", "yeah", "sure", "yea", "ok", "okay", "fine", "cool", "alright", "yup", "yep", "ya", "ye"]
32
+ def add_spaces_before_newline(input_string):
33
+ modified_chars = []
34
+ for char in input_string:
35
+ if char == '\n':
36
+ modified_chars.append(' \n')
37
+ else:
38
+ modified_chars.append(char)
39
+ return ''.join(modified_chars)
40
+
41
+ def run_query(query):
42
+ if query:
43
+ if query in yes_list:
44
+ query = "Give a list of all the tables and their column in bullet points"
45
+ answer = agent_executor.invoke(query)
46
+ else:
47
+ answer = agent_executor.invoke(query)
48
+ return answer["output"]
49
+ else:
50
+ return """Welcome, I am a SQL chatbot. Please type a query in the chatbox to get started.
51
+ To get started do you want me to provide you will the tables and their respective columns? Simply answer yes."""
52
+
53
+ st.title("SQL Chatbot")
54
+
55
+ if "messages" not in st.session_state:
56
+ st.session_state.messages = []
57
+
58
+ for message in st.session_state.messages:
59
+ with st.chat_message(message["role"]):
60
+ st.markdown(message["content"])
61
+
62
+ if prompt := st.chat_input("Type a query..."):
63
+ with st.chat_message("user"):
64
+ st.markdown(prompt)
65
+ st.session_state.messages.append({"role": "user", "content": prompt})
66
+
67
+ def response_generator():
68
+ response = run_query(prompt)
69
+ print(response)
70
+ for word in response.splitlines(keepends=True):
71
+ yield word + " "
72
+ time.sleep(0.05)
73
+
74
+ with st.chat_message("assistant"):
75
+ response = st.write_stream(response_generator())
76
+ st.session_state.messages.append({"role": "assistant", "content": response})
requirements.txt ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiohttp==3.9.5
2
+ aiosignal==1.3.1
3
+ annotated-types==0.6.0
4
+ anyio==4.3.0
5
+ asttokens==2.4.1
6
+ attrs==23.2.0
7
+ cachetools==5.3.3
8
+ certifi==2024.2.2
9
+ cffi==1.16.0
10
+ charset-normalizer==3.3.2
11
+ cloud-sql-python-connector==1.9.1
12
+ colorama==0.4.6
13
+ comm==0.2.2
14
+ cryptography==42.0.5
15
+ dataclasses-json==0.6.4
16
+ debugpy==1.8.1
17
+ decorator==5.1.1
18
+ distro==1.9.0
19
+ executing==2.0.1
20
+ frozenlist==1.4.1
21
+ google-auth==2.29.0
22
+ greenlet==3.0.3
23
+ h11==0.14.0
24
+ httpcore==1.0.5
25
+ httpx==0.27.0
26
+ idna==3.7
27
+ ipykernel==6.29.4
28
+ ipython==8.23.0
29
+ jedi==0.19.1
30
+ jsonpatch==1.33
31
+ jsonpointer==2.4
32
+ jupyter_client==8.6.1
33
+ jupyter_core==5.7.2
34
+ langchain==0.1.16
35
+ langchain-community==0.0.34
36
+ langchain-core==0.1.45
37
+ langchain-google-cloud-sql-mysql==0.2.1
38
+ langchain-openai==0.1.3
39
+ langchain-text-splitters==0.0.1
40
+ langsmith==0.1.49
41
+ marshmallow==3.21.1
42
+ matplotlib-inline==0.1.7
43
+ multidict==6.0.5
44
+ mypy-extensions==1.0.0
45
+ nest-asyncio==1.6.0
46
+ numpy==1.26.4
47
+ openai==1.23.2
48
+ orjson==3.10.1
49
+ packaging==23.2
50
+ parso==0.8.4
51
+ platformdirs==4.2.0
52
+ prompt-toolkit==3.0.43
53
+ psutil==5.9.8
54
+ pure-eval==0.2.2
55
+ pyasn1==0.6.0
56
+ pyasn1_modules==0.4.0
57
+ pycparser==2.22
58
+ pydantic==2.7.0
59
+ pydantic_core==2.18.1
60
+ Pygments==2.17.2
61
+ PyMySQL==1.1.0
62
+ python-dateutil==2.9.0.post0
63
+ pywin32==306
64
+ PyYAML==6.0.1
65
+ pyzmq==26.0.2
66
+ regex==2024.4.16
67
+ requests==2.31.0
68
+ rsa==4.9
69
+ six==1.16.0
70
+ sniffio==1.3.1
71
+ SQLAlchemy==2.0.29
72
+ stack-data==0.6.3
73
+ tenacity==8.2.3
74
+ tiktoken==0.6.0
75
+ tornado==6.4
76
+ tqdm==4.66.2
77
+ traitlets==5.14.3
78
+ typing-inspect==0.9.0
79
+ typing_extensions==4.11.0
80
+ urllib3==2.2.1
81
+ wcwidth==0.2.13
82
+ yarl==1.9.4