Atharva-28 commited on
Commit
ee000bc
1 Parent(s): be4f741

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +59 -0
  2. requirements.txt +3 -0
  3. sql.py +40 -0
  4. student.db +0 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Basic Workflow
2
+ #Prompt -> LLM(Gemini Pro) -> Query -> SQL Database -> Response
3
+ from dotenv import load_dotenv
4
+ load_dotenv() ## load all environment variables
5
+ import streamlit as st
6
+ import os
7
+ import sqlite3
8
+ import google.generativeai as genai
9
+
10
+ ##configure api key by calling from .env file
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+ # Function to load Google Gemini Model and provide query as response
14
+ def get_gemini_response(question,prompt):
15
+ model=genai.GenerativeModel('gemini-pro')
16
+ response = model.generate_content([prompt[0],question])
17
+ return response.text
18
+
19
+ ## Function to retrieve query from SQL Database. this will hit the database and get the result
20
+
21
+ def read_sql_query(sql,db):
22
+ conn = sqlite3.connect(db)
23
+ cur = conn.cursor()
24
+ cur.execute(sql)
25
+ rows = cur.fetchall()
26
+ conn.commit()
27
+ conn.close()
28
+ for row in rows:
29
+ print(row)
30
+ return rows
31
+
32
+ prompt = [
33
+ """
34
+ You are an expert in converting English question to SQL query! The SQL databse has the name STUDENT and has following columns - NAME, CLASS, SECTION and MARKS \n\nFor example, \nExample1 - How many entries of records are present?, the sql command will be something like this SELECT COUNT(*) FROM STUDENT;
35
+ \nExample 2 -Tell me all the student studying in Data Science class?, The SQL command will be something like this SELECT * FROM STUDENT where CLASS="Data Science";
36
+ also the sql code should not have ``` in begining or end and sql word in the output.
37
+ """
38
+ ]
39
+
40
+ #Streamlit APP
41
+
42
+ st.set_page_config(page_title="I can Retrieve Any SQL query")
43
+ st.header("Gemini Powered App To Retrieve SQL Data")
44
+
45
+ question = st.text_input("Input: ",key="input")
46
+
47
+ submit = st.button("Ask the question")
48
+
49
+ #get response
50
+ if submit:
51
+ response = get_gemini_response(question,prompt)# will get SQL Query
52
+ print(response)
53
+ data = read_sql_query(response,"student.db")
54
+ st.subheader("The Response is: ")
55
+ for row in data:
56
+ print(row)
57
+ st.header(row)
58
+
59
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ google-generativeai
2
+ streamlit
3
+ python-dotenv
sql.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ ## Connecting to sqlite
4
+ connection = sqlite3.connect("student.db")
5
+
6
+ # Cursor will be responsible to insert record, create table, retrive
7
+ cursor = connection.cursor()
8
+
9
+ ##Creating Table
10
+ table_info="""
11
+ Create table STUDENT(NAME VARCHAR(25), CLASS VARCHAR(25), SECTION VARCHAR(25),MARKS INT);
12
+ """
13
+
14
+ cursor.execute(table_info)
15
+
16
+ ## Inserting some records
17
+
18
+ cursor.execute('''Insert Into STUDENT values('Atharva','AI/ML','A','95')''')
19
+ cursor.execute('''Insert Into STUDENT values('Ashutosh','SWE','B','80')''')
20
+ cursor.execute('''Insert Into STUDENT values('Samarth','Data Science','B','35')''')
21
+ cursor.execute('''Insert Into STUDENT values('Yash','Data Science','A','76')''')
22
+ cursor.execute('''Insert Into STUDENT values('Asnee','AI/ML','B','100')''')
23
+ cursor.execute('''Insert Into STUDENT values('Sam','AI/ML','A','98')''')
24
+ cursor.execute('''Insert Into STUDENT values('Emily','DEVOPS','A','50')''')
25
+ cursor.execute('''Insert Into STUDENT values('Manasi','DEVOPS','B','97')''')
26
+
27
+ ## Display all records
28
+
29
+ data = cursor.execute('''select * From STUDENT''')
30
+
31
+ for row in data:
32
+ print(row)
33
+
34
+ ## Closing the connection
35
+
36
+ connection.commit()
37
+ connection.close()
38
+
39
+
40
+
student.db ADDED
Binary file (8.19 kB). View file