Samyak29 commited on
Commit
348d4d8
1 Parent(s): ed9c3a0

Upload 3 files

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