riswanahamed commited on
Commit
b334c60
·
verified ·
1 Parent(s): c4ac57a

Upload 4 files

Browse files
Files changed (4) hide show
  1. app1.py +95 -0
  2. requirements.txt +3 -0
  3. sequalite.py +29 -0
  4. student.db +0 -0
app1.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import dotenv_values
2
+
3
+ dotenv_values = dotenv_values('.env')
4
+ GOOGLE_API_KEY = dotenv_values['GOOGLE_API_KEY']
5
+
6
+ import streamlit as st
7
+ import os ,sys
8
+ import sqlite3
9
+ import google.generativeai as gen
10
+
11
+ gen.configure(api_key=str(GOOGLE_API_KEY))
12
+
13
+ # Connect to SQLite database
14
+
15
+ def get_gemini_response(question,prompt):
16
+ model= gen.GenerativeModel("gemini-pro")
17
+
18
+ response= model.generate_content([prompt[0],question])
19
+ return response.text
20
+
21
+
22
+ def execute_sql_query(sql, db_name):
23
+ try:
24
+ # Connect to the SQLite database
25
+ connection = sqlite3.connect(db_name)
26
+ cursor = connection.cursor()
27
+
28
+ # Execute the SQL query
29
+ cursor.execute(sql)
30
+
31
+ # Fetch all rows from the query
32
+ rows = cursor.fetchall()
33
+ connection.commit()
34
+ connection.close()
35
+ # Print the rows
36
+ for row in rows:
37
+ print(row)
38
+
39
+ # Return the rows
40
+ return rows
41
+
42
+ except sqlite3.Error as error:
43
+ print("Error while executing SQL query:", error)
44
+ return "Error: " + str(error)
45
+
46
+ finally:
47
+ # Close the connection to the database
48
+ if connection:
49
+ connection.commit()
50
+ connection.close()
51
+
52
+
53
+ prompt = [
54
+ '''
55
+ I am an SQL expert system. I can convert your English queries into SQL queries and execute them on the database.
56
+ The database contains information about students with attributes: Name, Class, and Section.
57
+
58
+ Please enter your English query. If the query is invalid, I will let you know.
59
+
60
+ Examples:
61
+ - English: How many students are in the database?
62
+ SQL: SELECT COUNT(*) FROM student;
63
+ - English: What are the names of students in Class AIDS?
64
+ SQL: SELECT Name FROM student WHERE Class = 'AIDS';
65
+ - English: What is the section of student named riswan?
66
+ SQL: SELECT Section FROM student WHERE Name = 'riswan';
67
+ - English: How many students are in Section A?
68
+ SQL: SELECT COUNT(*) FROM student WHERE Section = 'A';
69
+ - English: What are the classes of students named ajmal and rayan?
70
+ SQL: SELECT Class FROM student WHERE Name IN ('ajmal', 'rayan');
71
+
72
+ Enter your query:
73
+ '''
74
+ ]
75
+
76
+ st.set_page_config(page_title ="Student DB")
77
+ st.title("SQL Expert System")
78
+ st.header("gemini to sql retriver")
79
+ question = st.text_input("input -->>",key = "input")
80
+
81
+ submit = st.button("Submit")
82
+ if submit:
83
+ response = get_gemini_response(question, prompt)
84
+ print(response)
85
+ result = execute_sql_query(response, 'student.db')
86
+
87
+ if isinstance(result, str) and result.startswith("Error: "):
88
+ st.error(result)
89
+ elif result is not None:
90
+ st.subheader("response")
91
+ for row in result:
92
+ st.write(row)
93
+ st.write("\n")
94
+ else:
95
+ st.error("Unknown error occurred")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ google-generativeai
2
+ streamlit
3
+ pymongo
sequalite.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ connection = sqlite3.connect("test.db")
4
+ cursor = connection.cursor()
5
+ table_info = """
6
+ create table if not exists test(Name varchar(255), class varchar(255),
7
+ section varchar(255));
8
+ """
9
+ cursor.execute(table_info)
10
+
11
+ insert = """insert into test(Name, class, section) values ('riswan', 'AIDS', 'B');"""
12
+ insert1 = """insert into test (Name, class, section) values ('aslam', 'AIDS', 'A');"""
13
+ insert2 = """insert into test (Name,class, section) values ('ajmal', 'ECE', 'A');"""
14
+ insert3 = """insert into test (Name, class, section) values ('rayan', 'ECE', 'B');"""
15
+
16
+
17
+ cursor.execute(insert)
18
+ cursor.execute(insert1)
19
+ cursor.execute(insert2)
20
+ cursor.execute(insert3)
21
+
22
+ print("inserted")
23
+
24
+ data = cursor.execute("SELECT * FROM test")
25
+
26
+ for row in data:
27
+ print(row)
28
+
29
+ connection.close()
student.db ADDED
Binary file (8.19 kB). View file