ubaid975 commited on
Commit
e40bd04
·
verified ·
1 Parent(s): 5785a77

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +18 -12
src/streamlit_app.py CHANGED
@@ -4,10 +4,12 @@ from langchain.chat_models import ChatOpenAI
4
  from langchain.agents import create_sql_agent
5
  from langchain_groq import ChatGroq
6
  from langchain_community.agent_toolkits import SQLDatabaseToolkit
7
-
8
- import tempfile
9
  import sqlite3
10
  import pandas as pd
 
 
 
11
 
12
 
13
  def is_valid_sqlite(file_path):
@@ -61,9 +63,14 @@ def show_tables_as_df(db_path):
61
  # Streamlit UI
62
  st.title('🗃️ Chat with SQLite Database')
63
 
64
- st.write("This app lets you interact with a SQLite database using natural language queries powered by LLMs.")
65
 
66
- uploaded_file = st.file_uploader("Upload SQLite Database (.db file)", type=["db"])
 
 
 
 
 
67
 
68
  llm_provider = st.radio("Choose LLM Provider", options=['OPEN_ROUTER', 'GROQ', 'OPENAI'])
69
  model_name = st.text_input("Enter the Model Name", value='nousresearch/deephermes-3-mistral-24b-preview:free')
@@ -71,17 +78,15 @@ api_key = st.text_input("Enter Your API Key", type="password")
71
  query = st.text_area("Enter Your Query")
72
 
73
  try:
74
- if uploaded_file is not None:
75
- with tempfile.NamedTemporaryFile(delete=False, suffix=".db") as tmpfile:
76
- tmpfile.write(uploaded_file.read())
77
- tmp_db_path = tmpfile.name
78
 
79
  if not is_valid_sqlite(tmp_db_path):
80
- st.error("The uploaded file is not a valid SQLite database.")
81
  else:
82
- st.success("Valid SQLite database uploaded!")
83
 
84
- # Show tables as pandas DataFrames
85
  st.info("Displaying first 10 rows from each table:")
86
  show_tables_as_df(tmp_db_path)
87
 
@@ -91,10 +96,11 @@ try:
91
  elif not query.strip():
92
  st.error("Please enter a query.")
93
  else:
94
- st.info(f"Running query on `{uploaded_file.name}`...")
95
  result = text_to_sql(query, tmp_db_path, llm_provider, api_key, model_name)
96
  st.success("Query Result:")
97
  st.write(result)
98
 
99
  except Exception as e:
100
  st.error(f"Error: {str(e)}")
 
 
4
  from langchain.agents import create_sql_agent
5
  from langchain_groq import ChatGroq
6
  from langchain_community.agent_toolkits import SQLDatabaseToolkit
7
+ from dotenv import load_dotenv
 
8
  import sqlite3
9
  import pandas as pd
10
+ import os
11
+
12
+ load_dotenv()
13
 
14
 
15
  def is_valid_sqlite(file_path):
 
63
  # Streamlit UI
64
  st.title('🗃️ Chat with SQLite Database')
65
 
66
+ st.write("Interact with your SQLite databases stored on server using LLM-powered queries.")
67
 
68
+ # List of .db files in "data" folder
69
+ db_folder = "data"
70
+ os.makedirs(db_folder, exist_ok=True)
71
+ db_files = [f for f in os.listdir(db_folder) if f.endswith('.db')]
72
+
73
+ selected_db = st.selectbox("Select Database", options=db_files)
74
 
75
  llm_provider = st.radio("Choose LLM Provider", options=['OPEN_ROUTER', 'GROQ', 'OPENAI'])
76
  model_name = st.text_input("Enter the Model Name", value='nousresearch/deephermes-3-mistral-24b-preview:free')
 
78
  query = st.text_area("Enter Your Query")
79
 
80
  try:
81
+ if selected_db:
82
+ tmp_db_path = os.path.join(db_folder, selected_db)
 
 
83
 
84
  if not is_valid_sqlite(tmp_db_path):
85
+ st.error("The selected file is not a valid SQLite database.")
86
  else:
87
+ st.success("Valid SQLite database selected!")
88
 
89
+ # Show tables
90
  st.info("Displaying first 10 rows from each table:")
91
  show_tables_as_df(tmp_db_path)
92
 
 
96
  elif not query.strip():
97
  st.error("Please enter a query.")
98
  else:
99
+ st.info(f"Running query on `{selected_db}`...")
100
  result = text_to_sql(query, tmp_db_path, llm_provider, api_key, model_name)
101
  st.success("Query Result:")
102
  st.write(result)
103
 
104
  except Exception as e:
105
  st.error(f"Error: {str(e)}")
106
+