nasirkhan786 commited on
Commit
151368f
·
0 Parent(s):

text-to-SQL

Browse files
Files changed (8) hide show
  1. .gitignore +1 -0
  2. README.md +10 -0
  3. app.py +71 -0
  4. poetry.lock +0 -0
  5. pyproject.toml +17 -0
  6. requirements.txt +3 -0
  7. sql.py +59 -0
  8. student.db +0 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Text To SQL LLM App
3
+ emoji: 👁
4
+ colorFrom: indigo
5
+ colorTo: pink
6
+ sdk: streamlit
7
+ sdk_version: 1.39.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai #type:ignore
2
+ import streamlit as st
3
+ # importing for env variables loader
4
+ from dotenv import load_dotenv
5
+ import sqlite3
6
+ import os
7
+
8
+ load_dotenv()
9
+
10
+ # configure our api key
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+
14
+ # function to load Model and provide sql query as response
15
+ def get_gemini_response(prompt, question):
16
+ model = genai.GenerativeModel("gemini-1.5-flash")
17
+ response = model.generate_content([prompt[0], question])
18
+ return response.text
19
+
20
+
21
+ # function to retrieve query from the sql data base
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
+
34
+ # definning your prompt
35
+
36
+ prompt = [
37
+ """
38
+ You are an expert in converting English questions to SQL queries!
39
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
40
+ SECTION, and MARKS.
41
+
42
+ For Example:
43
+ - Example 1: How many entries of records are present?
44
+ The SQL command will be: SELECT COUNT(*) FROM STUDENT;
45
+ - Example 2: Tell me all the students studying in Data Science class?
46
+ The SQL command will be: SELECT * FROM STUDENT WHERE CLASS="Data Science";
47
+
48
+ Provide the clean query that can be executed on the database without using any backticks or additional characters.
49
+ The SQL command should not include any formatting characters or the word "SQL".
50
+ """
51
+ ]
52
+
53
+ # also the SQL code should not have ''' in biggining or end and sql word in output.
54
+
55
+ # streamlit app
56
+ st.set_page_config(page_title="retrieve sql queryF")
57
+ st.header(" Gemini App to Retrieve SQL Data")
58
+
59
+ user_question = st.text_input("input:", key="input")
60
+
61
+ sumbit = st.button("ask the question")
62
+
63
+ if sumbit:
64
+ sql_query = get_gemini_response(prompt,user_question)
65
+ print(sql_query)
66
+ data = read_sql_query(sql_query,"student.db")
67
+ st.subheader("The resplonse is:")
68
+ for row in data:
69
+ print(row)
70
+ st.write(row)
71
+
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "text to sql llm app"
3
+ version = "0.1.0"
4
+ description = ""
5
+ authors = ["Nasir Abbas <akhtarabbas.islamicreaction@gmail.com>"]
6
+ readme = "README.md"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.12"
10
+ streamlit = "^1.39.0"
11
+ google-generativeai = "^0.8.3"
12
+ python-dotenv = "^1.0.1"
13
+
14
+
15
+ [build-system]
16
+ requires = ["poetry-core"]
17
+ build-backend = "poetry.core.masonry.api"
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
sql.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ ## conect the sql
4
+ connection = sqlite3.connect("student.db")
5
+
6
+
7
+ # Create a cursor object to insert record, craete table, retreeve
8
+ cursor =connection.cursor()
9
+
10
+ ## create the table
11
+
12
+ table_info = """
13
+ Create table STUDENT(NAME VARCHAR(25), CLASS VARCHAR(25),
14
+ SECTION VARCHAR(25), MARKS INT);
15
+
16
+ """
17
+
18
+
19
+ cursor.execute(table_info)
20
+
21
+
22
+ # inser some more recordes
23
+
24
+ cursor.execute('''Insert into STUDENT values('Nasir','data Science', 'A', 90)''')
25
+ cursor.execute('''Insert into STUDENT values('Akhtar','Generative AI', 'B', 87)''')
26
+ cursor.execute('''Insert into STUDENT values('Asad','DEVOPS', 'A', 83)''')
27
+ cursor.execute('''Insert into STUDENT values('Munawer','data Science', 'C', 93)''')
28
+ cursor.execute('''Insert into STUDENT values('Muzafar','Generative AI', 'A', 83)''')
29
+ cursor.execute('''Insert into STUDENT values('Ali','DEVOPS', 'C', 82)''')
30
+ cursor.execute('''Insert into STUDENT values('Kashif','data Science', 'A', 79)''')
31
+ cursor.execute('''Insert into STUDENT values('Aneela','DEVOPS', 'B', 81)''')
32
+ cursor.execute('''Insert into STUDENT values('Danish','data Science', 'A', 78)''')
33
+ cursor.execute('''Insert into STUDENT values('Rabia','DEVOPS', 'C', 82)''')
34
+ cursor.execute('''Insert into STUDENT values('Khizar','Generative AI', 'A', 76)''')
35
+ cursor.execute('''Insert into STUDENT values('Nazia','data Science', 'A', 73)''')#
36
+ cursor.execute('''Insert into STUDENT values('Bilal','Generative AI', 'B', 72)''')
37
+ cursor.execute('''Insert into STUDENT values('sajid','DEVOPS', 'A', 87)''')
38
+ cursor.execute('''Insert into STUDENT values('Asif','data Science', 'C', 63)''')
39
+ cursor.execute('''Insert into STUDENT values('Husnain','Generative AI', 'A', 98)''')
40
+ cursor.execute('''Insert into STUDENT values('Hamza','DEVOPS', 'C', 84)''')
41
+ cursor.execute('''Insert into STUDENT values('Ikram','data Science', 'A', 65)''')
42
+ cursor.execute('''Insert into STUDENT values('Ramzan','DEVOPS', 'B', 78)''')
43
+ cursor.execute('''Insert into STUDENT values('Abid','data Science', 'A', 72)''')
44
+ cursor.execute('''Insert into STUDENT values('Jafir','DEVOPS', 'C', 81)''')
45
+ cursor.execute('''Insert into STUDENT values('Sadiq','Generative AI', 'A', 92)''')
46
+
47
+
48
+ ## Display all the recards
49
+ print("The inserted recards are")
50
+
51
+ data = cursor.execute('''Select * From STUDENT''')
52
+
53
+ for row in data:
54
+ print(row)
55
+
56
+
57
+ # close the connection
58
+ connection.commit()
59
+ connection.close()
student.db ADDED
Binary file (8.19 kB). View file