KenanKhan commited on
Commit
97c8d08
1 Parent(s): 649029e

Upload 3 files

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