FUZZZZI commited on
Commit
4da131e
1 Parent(s): c8a7f8d

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +66 -0
  2. requirements.txt +3 -0
  3. student.db +0 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv() # load all the envireonment variables
3
+
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+
8
+ import google.generativeai as genai
9
+
10
+ # Configure genai key
11
+ genai.configure(api_key = os.getenv("GOOGLE_API_KEY"))
12
+
13
+ # Function to load google gemini model
14
+ def get_gemini_response(question, prompt):
15
+ model = genai.GenerativeModel("gemini-pro")
16
+ response = model.generate_content([prompt, question])
17
+ return response.text
18
+
19
+ # Function to retrieve query from database
20
+ def read_sql_query(query, db):
21
+ conn = sqlite3.connect(db)
22
+ cur = conn.cursor()
23
+ cur.execute(query)
24
+ rows = cur.fetchall()
25
+ conn.commit() #Imp
26
+ conn.close()
27
+ for row in rows:
28
+ print(row)
29
+ return rows
30
+
31
+ # Define your prompt
32
+ prompt = [
33
+ """
34
+ You are an expert in converting English questions to SQL query!
35
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS, SECTIOn \n\n
36
+ For Example: \n
37
+ Example1 - How many entires of records are present?, the SQL command will be something like this
38
+ SELECT COUNT(*) FROM STUDENT;
39
+ also all the sql code should not have ''' in the begining or end and sql word in output. \n
40
+ Exmaple2 - tell me all the students study in Data Science class, the SQL command will be something like this
41
+ SELECT * FROM STUDENT where CLASS = "Data Science";
42
+ also all the sql code should not have ''' in the begining or end and sql word in output. \n
43
+ """
44
+ ]
45
+
46
+ # Strealit app
47
+ st.set_page_config(page_title = "I can retreive any SQL query")
48
+ st.header("Gemini App to Retrieve SQL Data")
49
+ question = st.text_input("Input: ", key="input")
50
+ submit = st.button("Ask the question")
51
+
52
+ # if submit is clicked
53
+ if submit:
54
+ resp = get_gemini_response(question=question , prompt=prompt[0])
55
+ print(resp)
56
+ response = read_sql_query(query = resp, db="student.db")
57
+ st.subheader("The Response is")
58
+ for row in response:
59
+ print(row)
60
+ st.header(row)
61
+
62
+
63
+ # Can you provide a list of students categorized as "first class" if their marks are greater than 60 and
64
+ # categorized as "second class" if their marks are lesser than 60
65
+
66
+ # Give me the 2nd last ranker name in terms of marks
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