lisaf commited on
Commit
2ee6ec3
ยท
verified ยท
1 Parent(s): 3f969c3

Upload 3 files

Browse files
Files changed (3) hide show
  1. Sql.py +129 -0
  2. app.py +157 -0
  3. requirements.txt +3 -0
Sql.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ # Connect to SQLite
4
+ connection = sqlite3.connect("student.db")
5
+
6
+ # Create a cursor object to insert records and create table
7
+ cursor = connection.cursor()
8
+
9
+ # Create the table if not exists
10
+ table_info = """
11
+ CREATE TABLE IF NOT EXISTS STUDENT(
12
+ NAME VARCHAR(25),
13
+ CLASS VARCHAR(25),
14
+ SECTION VARCHAR(25),
15
+ MARKS INT
16
+ );
17
+ """
18
+ cursor.execute(table_info)
19
+
20
+ records = [
21
+ ('Saqib', 'Data Science', 'A', 90),
22
+ ('Subash', 'Data Science', 'B', 100),
23
+ ('Mandeep', 'Data Science', 'A', 86),
24
+ ('Vikash', 'DEVOPS', 'A', 50),
25
+ ('Dipesh', 'DEVOPS', 'A', 35),
26
+ ('Sakshi', 'Class1', 'Section1', 60),
27
+ ('Rajesh', 'Class2', 'Section2', 70),
28
+ ('Amit', 'Class3', 'Section0', 80),
29
+ ('Rahul', 'Class4', 'Section1', 90),
30
+ ('Vivek', 'Class0', 'Section2', 95),
31
+ ('Neha', 'Class1', 'Section0', 99),
32
+ ('Priya', 'Class2', 'Section1', 77),
33
+ ('Deepak', 'Class3', 'Section2', 66),
34
+ ('Pooja', 'Class4', 'Section0', 55),
35
+ ('Ankit', 'Class0', 'Section1', 44),
36
+ ('Ayesha', 'Class1', 'Section2', 33),
37
+ ('Aryan', 'Class2', 'Section0', 22),
38
+ ('Riya', 'Class3', 'Section1', 11),
39
+ ('Sumit', 'Class4', 'Section2', 100),
40
+ ('Meera', 'Class0', 'Section0', 85),
41
+ ('Sachin', 'Class1', 'Section1', 75),
42
+ ('Anjali', 'Class2', 'Section2', 65),
43
+ ('Rahul', 'Class3', 'Section0', 55),
44
+ ('Kritika', 'Class4', 'Section1', 45),
45
+ ('Prateek', 'Class0', 'Section2', 35),
46
+ ('Shreya', 'Class1', 'Section0', 25),
47
+ ('Amit', 'Class2', 'Section1', 15),
48
+ ('Roshni', 'Class3', 'Section2', 5),
49
+ ('Mohit', 'Class4', 'Section0', 95),
50
+ ('Riya', 'Class0', 'Section1', 85),
51
+ ('Ravi', 'Class1', 'Section2', 75),
52
+ ('Anu', 'Class2', 'Section0', 65),
53
+ ('Aman', 'Class3', 'Section1', 55),
54
+ ('Sana', 'Class4', 'Section2', 45),
55
+ ('Raj', 'Class0', 'Section0', 35),
56
+ ('Anushka', 'Class1', 'Section1', 25),
57
+ ('Ajay', 'Class2', 'Section2', 15),
58
+ ('Sanjay', 'Class3', 'Section0', 5),
59
+ ('Shivangi', 'Class4', 'Section1', 99),
60
+ ('Pranav', 'Class0', 'Section2', 77),
61
+ ('Monika', 'Class1', 'Section0', 66),
62
+ ('Sahil', 'Class2', 'Section1', 55),
63
+ ('Riya', 'Class3', 'Section2', 44),
64
+ ('Vikas', 'Class4', 'Section0', 33),
65
+ ('Muskan', 'Class0', 'Section1', 22),
66
+ ('Arjun', 'Class1', 'Section2', 11),
67
+ ('Neha', 'Class2', 'Section0', 100),
68
+ ('Rohit', 'Class3', 'Section1', 85),
69
+ ('Sonia', 'Class4', 'Section2', 75),
70
+ ('Manoj', 'Class0', 'Section0', 65),
71
+ ('Gaurav', 'Class1', 'Section1', 55),
72
+ ('Preeti', 'Class2', 'Section2', 45),
73
+ ('Karan', 'Class3', 'Section0', 35),
74
+ ('Manisha', 'Class4', 'Section1', 25),
75
+ ('Jatin', 'Class0', 'Section2', 15),
76
+ ('Aditi', 'Class1', 'Section0', 5),
77
+ ('Sandeep', 'Class2', 'Section1', 99),
78
+ ('Rajesh', 'Class3', 'Section2', 77),
79
+ ('Tanvi', 'Class4', 'Section0', 66),
80
+ ('Aakash', 'Class0', 'Section1', 55),
81
+ ('Garima', 'Class1', 'Section2', 44),
82
+ ('Harsh', 'Class2', 'Section0', 33),
83
+ ('Shivani', 'Class3', 'Section1', 22),
84
+ ('Rohit', 'Class4', 'Section2', 11),
85
+ ('Sapna', 'Class0', 'Section0', 100),
86
+ ('Rajat', 'Class1', 'Section1', 85),
87
+ ('Sneha', 'Class2', 'Section2', 75),
88
+ ('Vishal', 'Class3', 'Section0', 65),
89
+ ('Pooja', 'Class4', 'Section1', 55),
90
+ ('Akash', 'Class0', 'Section2', 45),
91
+ ('Nisha', 'Class1', 'Section0', 35),
92
+ ('Shubham', 'Class2', 'Section1', 25),
93
+ ('Sakshi', 'Class3', 'Section2', 15),
94
+ ('Gopal', 'Class4', 'Section0', 5),
95
+ ('Shweta', 'Class0', 'Section1', 99),
96
+ ('Mohini', 'Class1', 'Section2', 77),
97
+ ('Vivek', 'Class2', 'Section0', 66),
98
+ ('Aditya', 'Class3', 'Section1', 55),
99
+ ('Tanu', 'Class4', 'Section2', 44),
100
+ ('Aarti', 'Class0', 'Section0', 33),
101
+ ('Rohit', 'Class1', 'Section1', 22),
102
+ ('Pawan', 'Class2', 'Section2', 11),
103
+ ('Ankita', 'Class3', 'Section0', 99),
104
+ ('Komal', 'Class4', 'Section2', 77),
105
+ ('Nidhi', 'Class0', 'Section0', 66),
106
+ ('Abhishek', 'Class1', 'Section1', 55),
107
+ ('Sagar', 'Class2', 'Section2', 44),
108
+ ('Sneha', 'Class3', 'Section0', 33),
109
+ ('Rahul', 'Class4', 'Section1', 22),
110
+ ('Riya', 'Class0', 'Section2', 11)
111
+ ]
112
+
113
+
114
+ # Add more records to make it 100
115
+ for i in range(8, 101):
116
+ records.append(('Student' + str(i), 'Class' + str(i % 5), 'Section' + str(i % 3), i % 101))
117
+
118
+ # Insert all records
119
+ cursor.executemany('INSERT INTO STUDENT VALUES (?,?,?,?)', records)
120
+
121
+ # Display All the records
122
+ print("The inserted records are:")
123
+ data = cursor.execute('SELECT * FROM STUDENT')
124
+ for row in data:
125
+ print(row)
126
+
127
+ # Commit your changes in the database
128
+ connection.commit()
129
+ connection.close()
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() ## load all the environment variables
3
+
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+
8
+ import google.generativeai as genai
9
+ ## Configure Genai Key
10
+
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+ ## Function To Load Google Gemini Model and provide queries as response
14
+
15
+ def get_gemini_response(question,prompt):
16
+ model=genai.GenerativeModel('gemini-pro')
17
+ response=model.generate_content([prompt[0],question])
18
+ return response.text
19
+
20
+ ## Function To retrieve query from the database
21
+
22
+ def read_sql_query(sql,db):
23
+ conn=sqlite3.connect(db)
24
+ cur=conn.cursor()
25
+ cur.execute(sql)
26
+ rows=cur.fetchall()
27
+ conn.commit()
28
+ conn.close()
29
+ return rows
30
+
31
+ ## Define Your Prompt
32
+ ## Define Your Prompt
33
+ prompt=[
34
+ """
35
+ You are an expert in converting English questions to SQL query!
36
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
37
+ SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
38
+ the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
39
+ \nExample 2 - Tell me all the students studying in Data Science class?,
40
+ the SQL command will be something like this SELECT * FROM STUDENT
41
+ where CLASS="Data Science";
42
+ also the sql code should not have ``` in beginning or end and sql word in output
43
+
44
+ """
45
+ ]
46
+
47
+ ## Streamlit App
48
+
49
+ st.set_page_config(page_title="Gemini SQL Assistant", page_icon=":gemini:")
50
+ st.title("๐Ÿ”ฎ Gemini SQL Assistant ๐Ÿ”")
51
+
52
+
53
+ st.sidebar.title("๐Ÿš€ Ask a Question ๐Ÿ“")
54
+ question = st.sidebar.text_input("Enter your question here:")
55
+
56
+ submit = st.sidebar.button("๐Ÿ”Ž Get SQL Query")
57
+
58
+ # Main content area
59
+ st.markdown("---")
60
+
61
+ if submit:
62
+ response = get_gemini_response(question, prompt)
63
+ st.subheader("๐Ÿ” Generated SQL Query:")
64
+ st.code(response, language="sql")
65
+
66
+ # Execute SQL query
67
+ try:
68
+ rows = read_sql_query(response, "student.db")
69
+ if rows:
70
+ st.subheader("๐Ÿ“Š Query Result:")
71
+ st.dataframe(rows)
72
+ else:
73
+ st.warning("โ— No results found for this query.")
74
+ except Exception as e:
75
+ st.error(f"โŒ An error occurred: {e}")
76
+
77
+ # Main content area
78
+ st.write("""
79
+ ๐ŸŽ“ **About Gemini SQL Assistant**
80
+
81
+ Gemini SQL Assistant is an intelligent tool that helps you convert English questions into SQL queries.
82
+ Whether you're a beginner or an expert in SQL, Gemini can assist you in generating SQL queries to retrieve data from the STUDENT database.
83
+
84
+ Simply enter your question in the sidebar, click on the 'Get SQL Query' button, and Gemini will generate the corresponding SQL query for you.
85
+ You can then execute the query to retrieve the desired data from the database.
86
+
87
+ ๐Ÿ”ง **How to Use**
88
+
89
+ 1. Enter your question in the sidebar.
90
+ 2. Click on the 'Get SQL Query' button.
91
+ 3. View the generated SQL query in the main area.
92
+ 4. Execute the query to retrieve data from the database.
93
+ 5. Explore the query results and analyze the data as needed.
94
+
95
+ ๐Ÿ“Š **Example Questions**
96
+
97
+ - How many entries of records are present?
98
+ - Tell me all the students studying in Data Science class.
99
+ - Show me the students with marks above 80.
100
+
101
+ Feel free to ask any question related to the STUDENT database, and Gemini will assist you in generating the SQL query to retrieve the data.
102
+
103
+ ๐Ÿ‘ฉโ€๐Ÿ’ป **Powered By**
104
+
105
+ Gemini ๐Ÿ’ซ - A powerful generative AI model developed by Google.
106
+ Streamlit ๐Ÿš€ - An open-source app framework for Machine Learning and Data Science.
107
+
108
+ """)
109
+
110
+ # Footer
111
+ footer_with_image_light_blue = """
112
+ <style>
113
+ .footer {
114
+ background-color: #f0f0f0;
115
+ padding: 20px;
116
+ text-align: center;
117
+ border-top: 1px solid #ccc;
118
+ }
119
+
120
+ .footer img {
121
+ max-width: 100%;
122
+ margin-top: 10px;
123
+ }
124
+
125
+ .footer .connect-text {
126
+ color: #333;
127
+ font-weight: bold;
128
+ margin-bottom: 10px;
129
+ }
130
+
131
+ .footer a {
132
+ margin: 0 10px;
133
+ color: #333;
134
+ }
135
+
136
+ .footer .powered-by {
137
+ color: #333;
138
+ font-size: 14px;
139
+ margin-top: 10px;
140
+ }
141
+
142
+ .bright-text {
143
+ color: #004D40;
144
+ }
145
+ </style>
146
+ <div class="footer">
147
+ <div class="connect-text">Connect with me at</div>
148
+ <a href="https://github.com/FasilHameed" target="_blank"><img src="https://img.icons8.com/plasticine/30/000000/github.png" alt="GitHub"></a>
149
+ <a href="https://www.linkedin.com/in/faisal--hameed/" target="_blank"><img src="https://img.icons8.com/plasticine/30/000000/linkedin.png" alt="LinkedIn"></a>
150
+ <a href="tel:+917006862681"><img src="https://img.icons8.com/plasticine/30/000000/phone.png" alt="Phone"></a>
151
+ <a href="mailto:faisalhameed763@gmail.com"><img src="https://img.icons8.com/plasticine/30/000000/gmail.png" alt="Gmail"></a>
152
+ <div class="powered-by">Powered By <img src="https://img.icons8.com/clouds/30/000000/gemini.png" alt="Gemini"> Gemini ๐Ÿ’ซ and Streamlit ๐Ÿš€</div>
153
+ </div>
154
+ """
155
+
156
+ # Render Footer
157
+ st.markdown(footer_with_image_light_blue, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv