darshvaidya26 commited on
Commit
d44af81
·
verified ·
1 Parent(s): c696a0b

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +61 -0
  2. requirements.txt +3 -0
  3. student.db +0 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 API key
10
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
+
12
+ #Load 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
+ def read_sql_query(query,db):
20
+ conn = sqlite3.connect(db)
21
+ cur = conn.cursor()
22
+ cur.execute(query)
23
+ rows = cur.fetchall()
24
+ conn.commit()
25
+ conn.close()
26
+ for row in rows:
27
+ print(row)
28
+ return rows
29
+
30
+ #Prompt definition
31
+ prompt = [
32
+ """
33
+ You are an expert in converting English questions to SQL code!
34
+ The SQL database has the name STUDENT and the following columns - NAME , CLASS , SECTION\n\n
35
+ For example,\n Example 1 - How many entries of records are present?,
36
+ the SQL command will be something like this SELECT COUNT(* FROM STUDENT;
37
+ \n Example 2 - Tell me all the students studying in AI class?,
38
+ the SQL command will be something like this SELECT * FROM STUDENT WHERE CLASS = "AI";
39
+ also the sql code should not have ``` in the beginning or end and sql word in output
40
+
41
+ """
42
+ ]
43
+
44
+ #Streamlit app
45
+
46
+ st.set_page_config(page_title="I can retreive any SQL query")
47
+ st.header("Gemini App to retrieve any SQL Data")
48
+
49
+ question = st.text_input("Input:" , key="input" )
50
+ submit = st.button("Ask the question")
51
+
52
+ #If submit is clicked
53
+
54
+ if submit:
55
+ response = get_gemini_response(question,prompt)
56
+ print(response)
57
+ response = read_sql_query(response,"student.db")
58
+ st.subheader("The response is")
59
+ for row in response:
60
+ print(row)
61
+ st.header(row)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
student.db ADDED
Binary file (8.19 kB). View file