Haodan commited on
Commit
011f2dc
·
verified ·
1 Parent(s): 0a0381d

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +64 -0
  2. requirements.txt +3 -0
  3. sqlite.py +31 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv()
3
+
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+ import google.generativeai as genai
8
+
9
+ ## Configure Genai key
10
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
+
12
+ ## Function to load Google Gemini model
13
+
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 the database
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
+
29
+ for row in rows:
30
+ print(row)
31
+ return rows
32
+
33
+ ## Define Your prompt
34
+ prompt=[
35
+ """
36
+ You are an expert in converting English questions to SQL query.
37
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS, SECTION \n\n
38
+ For example, \nExample1 - How many entries of records are present?,
39
+ The SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
40
+ \nExample 2 - Tell me all the students studying in Data Science class?,
41
+ the SQL command will be something like this SELECT * FROM STUDENT 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
+ ## Streamlit app
47
+
48
+ st.set_page_config(page_title="I can Retreive Any SQL query")
49
+ st.header("Text to SQL query App")
50
+
51
+ question=st.text_input("Input: ", key="input")
52
+ submit=st.button("Ask the question")
53
+
54
+ # if submit is clicked
55
+ if submit:
56
+ query=get_gemini_response(question,prompt)
57
+ st.subheader("The SQL query is")
58
+ st.text(query)
59
+
60
+ response=read_sql_query(query, 'student.db')
61
+ st.subheader("The response is")
62
+ for row in response:
63
+ print(row)
64
+ st.text(row)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
sqlite.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ ## Connect to SQlite
4
+ connection=sqlite3.connect('student.db')
5
+
6
+ ## Create a cursor object to insert record, create table
7
+ cursor=connection.cursor()
8
+
9
+ ## Create the 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
+ ## Insert some records
17
+ cursor.execute('''Insert Into STUDENT values('Chris','Data Science', 'A', 91)''')
18
+ cursor.execute('''Insert Into STUDENT values('Ashley','Data Science', 'B', 86)''')
19
+ cursor.execute('''Insert Into STUDENT values('Alex','Data Science', 'A', 99)''')
20
+ cursor.execute('''Insert Into STUDENT values('Doris','Software Development', 'A', 71)''')
21
+ cursor.execute('''Insert Into STUDENT values('Wong','Software Development', 'B', 52)''')
22
+
23
+ ## Display all the records
24
+ print("The inserted records are:")
25
+ data=cursor.execute('''Select * from STUDENT''')
26
+ for row in data:
27
+ print(row)
28
+
29
+ ## Commit the changes in the database
30
+ connection.commit()
31
+ connection.close()