Anand28 commited on
Commit
c4918c5
·
verified ·
1 Parent(s): dbbf33e

Upload 3 files

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