1Prarthana commited on
Commit
ec8d989
Β·
verified Β·
1 Parent(s): b373c25

Upload 7 files

Browse files
Files changed (7) hide show
  1. .env +1 -0
  2. requirements.txt +9 -0
  3. sql.py +124 -0
  4. sqlite copy.py +32 -0
  5. sqlite.py +35 -0
  6. student.db +0 -0
  7. test.db +0 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GOOGLE_API_KEY = "AIzaSyCuh8F92Mm3UCoFLg74tugLQdPOo_yA4i0"
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ pdf2image
9
+ sqlite3
sql.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv() ## Load all environment variables
6
+
7
+ import streamlit as st
8
+ import os
9
+ import sqlite3
10
+ import google.generativeai as genai
11
+
12
+ ## Configure Google Gemini API Key
13
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
14
+
15
+
16
+ ## Function To Load Google Gemini Model and provide queries as response
17
+ def get_gemini_response(question, prompt):
18
+ model = genai.GenerativeModel('gemini-2.0-flash')
19
+ response = model.generate_content([prompt[0], question])
20
+ return response.text
21
+
22
+
23
+ ## Function To Retrieve Query from the Database
24
+ def read_sql_query(sql, db):
25
+ try:
26
+ conn = sqlite3.connect(db)
27
+ cur = conn.cursor()
28
+ cur.execute(sql)
29
+ rows = cur.fetchall()
30
+ conn.commit()
31
+ conn.close()
32
+ return rows
33
+ except sqlite3.Error as e:
34
+ return [("Error:", str(e))]
35
+
36
+
37
+ ## Ensure Table Exists Before Querying
38
+ def ensure_table_exists():
39
+ conn = sqlite3.connect("student.db")
40
+ cursor = conn.cursor()
41
+ cursor.execute("""
42
+ CREATE TABLE IF NOT EXISTS STUDENT(
43
+ NAME VARCHAR(25),
44
+ CLASS VARCHAR(25),
45
+ SECTION VARCHAR(25),
46
+ MARKS INT);
47
+ """)
48
+ conn.commit()
49
+ conn.close()
50
+
51
+
52
+ ## Define Your Prompt
53
+ prompt = [
54
+ """
55
+ You are an expert in converting English questions to SQL queries!
56
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
57
+ SECTION, MARKS.\n\nFor example:
58
+ - How many entries of records are present?
59
+ SQL: SELECT COUNT(*) FROM STUDENT;
60
+ - Tell me all the students studying in Data Science class?
61
+ SQL: SELECT * FROM STUDENT WHERE CLASS="Data Science";
62
+
63
+ **Do NOT include "```sql" or "```" in the response.**
64
+ """
65
+ ]
66
+
67
+ ## 🎨 Streamlit App UI & UX Enhancements
68
+ st.set_page_config(page_title="Advanced SQL Query Generator", layout="wide")
69
+
70
+ # 🎯 Sidebar for Instructions & About
71
+ with st.sidebar:
72
+ st.image("https://cdn-icons-png.flaticon.com/512/2721/2721292.png", width=100)
73
+ st.title("πŸ“Œ How It Works")
74
+ st.markdown(
75
+ "1️⃣ **Enter a question in plain English** \n"
76
+ "2️⃣ **The AI converts it into an SQL query** \n"
77
+ "3️⃣ **The app fetches results from the database** \n"
78
+ "4️⃣ **You get an answer instantly! 🎯**"
79
+ )
80
+
81
+ st.warning("⚠️ **First, run all three files (sqlite.py, sqlite_copy.py, and sql.py) to ensure the database exists.** Then, ask your query.")
82
+ st.markdown("---")
83
+ st.write("πŸ‘©β€πŸ’» **Created By:** Prarthana πŸ’™")
84
+
85
+ # 🎬 Animated Header
86
+ st.markdown(
87
+ "<h1 style='text-align: center; color: #1f77b4;'>🎯 AI-Powered SQL Query Generator πŸš€</h1>",
88
+ unsafe_allow_html=True
89
+ )
90
+
91
+ # πŸ“ User Input
92
+ question = st.text_input("πŸ” **Enter Your Question:**", key="input")
93
+
94
+ # 🎯 Button to Generate SQL Query
95
+ if st.button("πŸ”Ž Get SQL Query & Fetch Data"):
96
+ if question:
97
+ ensure_table_exists() # Ensure table exists before querying
98
+
99
+ with st.spinner("πŸ€– Generating SQL query..."):
100
+ sql_query = get_gemini_response(question, prompt)
101
+
102
+ st.success(f"βœ… Generated Query: `{sql_query}`")
103
+
104
+ # Fetch Data
105
+ with st.spinner("πŸ“‘ Fetching Data from Database..."):
106
+ response = read_sql_query(sql_query, "student.db")
107
+
108
+ if response:
109
+ st.subheader("πŸ“Š **Query Results:**")
110
+ for row in response:
111
+ st.write(row)
112
+ else:
113
+ st.warning("⚠️ No data found in the database.")
114
+ else:
115
+ st.error("❌ Please enter a valid question.")
116
+
117
+ # ✨ Footer
118
+ st.markdown("<br><br>", unsafe_allow_html=True)
119
+ st.markdown("<h4 style='text-align: center;'>✨ Powered by Google Gemini Pro ✨</h4>", unsafe_allow_html=True)
120
+
121
+
122
+
123
+
124
+
sqlite copy.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import module
2
+ import sqlite3
3
+
4
+ # Connecting to sqlite
5
+ conn = sqlite3.connect('test.db')
6
+
7
+ # Creating a cursor object using the
8
+ # cursor() method
9
+ cursor = conn.cursor()
10
+
11
+ # Creating table
12
+ table = """CREATE TABLE STUDENT(NAME VARCHAR(255), CLASS VARCHAR(255),
13
+ SECTION VARCHAR(255));"""
14
+ cursor.execute(table)
15
+
16
+ # Queries to INSERT records.
17
+ cursor.execute('''INSERT INTO STUDENT VALUES ('Krish', 'Data Science', 'A')''')
18
+ cursor.execute('''INSERT INTO STUDENT VALUES ('Darius', 'Data Science', 'B')''')
19
+ cursor.execute('''INSERT INTO STUDENT VALUES ('Sudhanshu', 'Devops', 'C')''')
20
+ cursor.execute('''INSERT INTO STUDENT VALUES ('Vikash', 'Data Science', 'C')''')
21
+
22
+ # Display data inserted
23
+ print("Data Inserted in the table: ")
24
+ data = cursor.execute('''SELECT * FROM STUDENT''')
25
+ for row in data:
26
+ print(row)
27
+
28
+ # Commit your changes in the database
29
+ conn.commit()
30
+
31
+ # Closing the connection
32
+ conn.close()
sqlite.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ ## Connectt to SQlite
4
+ connection=sqlite3.connect("student.db")
5
+
6
+ # Create a cursor object to insert record,create table
7
+
8
+ cursor=connection.cursor()
9
+
10
+ ## create the table
11
+ table_info="""
12
+ Create table STUDENT(NAME VARCHAR(25),CLASS VARCHAR(25),
13
+ SECTION VARCHAR(25),MARKS INT);
14
+
15
+ """
16
+ cursor.execute(table_info)
17
+
18
+ ## Insert Some more records
19
+
20
+ cursor.execute('''Insert Into STUDENT values('Krish','Data Science','A',90)''')
21
+ cursor.execute('''Insert Into STUDENT values('Sudhanshu','Data Science','B',100)''')
22
+ cursor.execute('''Insert Into STUDENT values('Darius','Data Science','A',86)''')
23
+ cursor.execute('''Insert Into STUDENT values('Vikash','DEVOPS','A',50)''')
24
+ cursor.execute('''Insert Into STUDENT values('Dipesh','DEVOPS','A',35)''')
25
+
26
+ ## Disspaly ALl the records
27
+
28
+ print("The inserted records are")
29
+ data=cursor.execute('''Select * from STUDENT''')
30
+ for row in data:
31
+ print(row)
32
+
33
+ ## Commit your changes int he databse
34
+ connection.commit()
35
+ connection.close()
student.db ADDED
Binary file (8.19 kB). View file
 
test.db ADDED
Binary file (8.19 kB). View file