arkokundu500 commited on
Commit
0485855
·
verified ·
1 Parent(s): 93aab78

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +70 -0
  2. requirements.txt +4 -0
  3. student.db +0 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() #load all the env variables
3
+ import streamlit as st
4
+ import os
5
+ import sqlite3
6
+
7
+ import google.generativeai as genai
8
+
9
+ # Configure Genai key
10
+
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+ ## Function to load Google Gemini Model and provide queries 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 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
+
34
+ prompt =[
35
+
36
+ """
37
+ You are an expert in converting English questions to SQL query!
38
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS,S​​ECTION
39
+ \n\nFor example,\nExample 1 - How many entries of records are present?,
40
+ the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
41
+ \nExample 2 - Tell me all the students studying in Data Science class?,
42
+ the SQL command will be something like this SELECT * FROM STUDENT
43
+ where CLASS="Data Science";
44
+ also the sql code should not have ``` in beginning or en​​d and sql word in output
45
+
46
+ """
47
+ ]
48
+
49
+ ## Streamlit APP
50
+
51
+ st.set_page_config(page_title="Student Data Retrieval")
52
+ st.header("Provide prompts based on the :red[student data]")
53
+ st.subheader(":blue[to get best output] :lower_left_fountain_pen:", divider=True )
54
+ st.subheader("Try using - 'Tell me the student name with highest rank and provide his marks' ")
55
+
56
+ questions = st.text_input("Input: ",key="input")
57
+
58
+ submit = st.button("Ask the questions")
59
+
60
+
61
+ # if submit is clicked
62
+ if submit:
63
+ response = get_gemini_response(questions,prompt)
64
+ print(response)
65
+ response = read_sql_query(response,"student.db")
66
+ st.subheader("The response is")
67
+ for row in response:
68
+ print(row)
69
+ st.header(row)
70
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+
student.db ADDED
Binary file (8.19 kB). View file