Jaichandran1984 commited on
Commit
cc77ed1
1 Parent(s): d7714e0

Upload 4 files

Browse files
Files changed (4) hide show
  1. COMPANY.db +0 -0
  2. app.py +78 -0
  3. requirements.txt +9 -0
  4. sqlite.py +37 -0
COMPANY.db ADDED
Binary file (8.19 kB). View file
 
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() ## load all the environemnt 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
+ ## Fucntion 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
+ #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 COMPANY and has the following columns - FIRST_NAME, LAST_NAME,DEPT,
38
+ SALARY,AGE \n\nFor example,\nExample 1 - How many entries of records are present?,
39
+ the SQL command will be something like this SELECT COUNT(*) FROM COMPANY ;
40
+ \nExample 2 - Tell me all the students studying in Data Science class?,
41
+ the SQL command will be something like this SELECT * FROM company
42
+ where CLASS="Data Science";
43
+ also the sql code should not have ``` in beginning or end and sql word in output
44
+
45
+ """
46
+
47
+
48
+ ]
49
+
50
+ ## Streamlit App
51
+
52
+ st.set_page_config(page_title="Retrieve Any SQL query")
53
+ st.header("App To Generate SQL & Data")
54
+
55
+ question=st.text_input("Input: ",key="input")
56
+
57
+ submit=st.button("Ask the question")
58
+
59
+ # if submit is clicked
60
+ if submit:
61
+ response=get_gemini_response(question,prompt)
62
+ #print(response)
63
+ st.subheader("Generated SQL Query")
64
+ st.code(response, language='sql')
65
+ data=read_sql_query(response,"company.db")
66
+ st.subheader("The Response is")
67
+ for row in data:
68
+ #print(row)
69
+ st.write(row)
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
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
sqlite.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ #Connect to SQlite
4
+ connection=sqlite3.connect("COMPANY.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 COMPANY(FIRST_NAME VARCHAR(25),LAST_NAME VARCHAR(25),DEPT VARCHAR(25),
13
+ SALARY VARCHAR(25),AGE INT);
14
+
15
+ """
16
+ cursor.execute(table_info)
17
+
18
+ ## Insert Some more records
19
+
20
+ cursor.execute('''Insert Into COMPANY values('Jaya', 'Chandran','Data Science','$1000',35)''')
21
+ cursor.execute('''Insert Into COMPANY values('Karthy','R','DEVOPS','$900',33)''')
22
+ cursor.execute('''Insert Into COMPANY values('Anbu', 'Alagan','AEM','$800',33)''')
23
+ cursor.execute('''Insert Into COMPANY values('Sivam','Sivam','DEVOPS','$1000',30)''')
24
+ cursor.execute('''Insert Into COMPANY values('Mani','Ratam','Data Science','$500',20)''')
25
+ cursor.execute('''Insert Into COMPANY values('Kasi','Nathan','Security','$400',23)''')
26
+ cursor.execute('''Insert Into COMPANY values('Ravi','Kumar','.Net','$150',25)''')
27
+
28
+ ## Disspaly All the records
29
+
30
+ print("The inserted records are")
31
+ data=cursor.execute('''Select * from COMPANY''')
32
+ for row in data:
33
+ print(row)
34
+
35
+ ## Commit your changes int he databse
36
+ connection.commit()
37
+ connection.close()