Jagaadhep commited on
Commit
d3dfb39
1 Parent(s): 3d81dbf

Upload 3 files

Browse files
Files changed (3) hide show
  1. STUDENT.db +0 -0
  2. app.py +55 -0
  3. requirements.txt +3 -0
STUDENT.db ADDED
Binary file (8.19 kB). View file
 
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ import os
3
+ import streamlit as st
4
+ import sqlite3
5
+ import google.generativeai as genai
6
+ import time
7
+
8
+ st.set_page_config(page_title = "Retrieve SQL query using text")
9
+
10
+ os.environ['GOOGLE_API_KEY'] = "AIzaSyCaW7JGGWjiBRvq2DGl9uTIrptH7FzhFh8"
11
+ load_dotenv()
12
+
13
+ genai.configure()
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
+ def read_and_execute_sql_query(sql, db):
21
+ connection = sqlite3.connect(db)
22
+ cursor = connection.cursor()
23
+ cursor.execute(sql)
24
+ rows = cursor.fetchall()
25
+ connection.commit()
26
+ connection.close()
27
+ for row in rows:
28
+ print(row)
29
+ return rows
30
+
31
+ prompt = [
32
+ """
33
+ You are in expert in converting English questions to SQL code!
34
+ The SQL database has the name STUDENT.db and the table name is STUDENT and has the following columns - NAME, CLASS, SECTION \n\n
35
+ For example, \nExample 1 - How many entries of records are present?, the SQL command will be something
36
+ like this SELECT COUNT(*) FROM STUDENT; \nExample 2 - Tell me all the students studying in DS class? the SQL command
37
+ will be something like this SELECT * FROM STUDENT where CLASS = 'DS';
38
+ also the sql code should not have ''' in beginning or end and sql word in output
39
+ """
40
+ ]
41
+
42
+ #Streamlit app
43
+ st.header("Gemini app to retrieve SQL data using text")
44
+
45
+ question = st.text_input("Input : ", key = "input")
46
+
47
+ submit = st.button("Ask the question")
48
+
49
+ if submit:
50
+ response = get_gemini_response(question, prompt)
51
+ response = read_and_execute_sql_query(response, "STUDENT.db")
52
+ st.subheader("Response : ")
53
+ for row in response:
54
+ print(row)
55
+ st.write(row)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ python-dotenv
2
+ google-generativeai
3
+ streamlit