harshpatel080503 commited on
Commit
4ed3ea5
·
verified ·
1 Parent(s): 0570204

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +63 -0
  2. requirements.txt +3 -0
  3. student.db +0 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() ## Load all the environment variables
3
+
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+
8
+ import google.generativeai as genai
9
+
10
+ ## Configure our API Key
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEYS"))
12
+
13
+ # Function to Load Google Gemini Model and provide sql query as response
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
+ ## Function to retrieve query from the sql database
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 Your 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 and MARKS \n\nFor example,\nExample 1 - How many entries of records are present?,
38
+ the SQL command will be something like the 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 in sql word in output
43
+ """
44
+ ]
45
+
46
+ ## Streamlit App
47
+
48
+ st.set_page_config(page_title="I can Retrieve Any SQL query")
49
+ st.header("Gemini App to Retrieve SQL Data")
50
+
51
+ question = st.text_input("Input:",key="input")
52
+
53
+ submit = st.button("Ask the question")
54
+
55
+ # if submit is clicked
56
+ if submit:
57
+ response = get_gemini_response(question,prompt)
58
+ print(response)
59
+ data = read_sql_query(response,"student.db")
60
+ st.subheader("The Response is")
61
+ for row in data:
62
+ print(row)
63
+ 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