TalentedSD commited on
Commit
2a905d8
·
verified ·
1 Parent(s): 8380528
Files changed (3) hide show
  1. app.py +61 -0
  2. requirements.txt +3 -0
  3. student.db +0 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv()
3
+ import streamlit as st
4
+ import os
5
+ import sqlite3
6
+ import google.generativeai as genai
7
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
8
+
9
+
10
+ def get_gemini_response(question,prompt):
11
+ model=genai.GenerativeModel('gemini-pro')
12
+ combined_prompt = prompt[0] + "\n" + question
13
+ response = model.generate_content(combined_prompt)
14
+ return response.text
15
+
16
+
17
+ def read_sql_query(sql,db):
18
+ conn = sqlite3.connect(db)
19
+ cur = conn.cursor()
20
+ cur.execute(sql)
21
+ rows = cur.fetchall()
22
+ conn.commit()
23
+ conn.close()
24
+ for row in rows:
25
+ print(row)
26
+ return rows
27
+
28
+ ## Define Your Prompt
29
+
30
+ prompt = [
31
+ """
32
+ You are an expert in converting English questions to SQL code!
33
+ The SQL database has the name STUDENT and has the following columns-
34
+ NAME,CLASS,SECTION,MARKS \n\n
35
+ For example,\n
36
+ Example 1 - How many entries of records are present?,
37
+ the SQL command will be something like select count(*) from student;
38
+
39
+ Example 1 - Tell me all the students from CSE class?,
40
+ the SQL command will be something like select * from student where class="CSE";
41
+
42
+ also the sql code should not have ``` in beginning or end and sql word in output
43
+
44
+
45
+
46
+ """
47
+ ]
48
+
49
+ st.set_page_config(page_title="Retrieve any SQL Query")
50
+ st.header("Gemini App to retrieve SQL data")
51
+ question=st.text_input("Input:", key="input")
52
+ submit=st.button("Ask the question")
53
+
54
+ if submit:
55
+ response = get_gemini_response(question,prompt)
56
+ print(response)
57
+ sql_response = read_sql_query(response,"student.db")
58
+ st.subheader("The response is:")
59
+ for row in sql_response:
60
+ print(row)
61
+ 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