PJ-data-star commited on
Commit
7c793f2
·
verified ·
1 Parent(s): 8411dc5

Upload app.py

Browse files
Files changed (1) hide show
  1. src/app.py +58 -0
src/app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv()
3
+
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+ from google import genai
8
+
9
+ client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
10
+
11
+ # funct to load gemini model and provide queries as response
12
+ def get_gemini_response(question,prompt):
13
+ response = client.models.generate_content(
14
+ model="gemini-3-pro-preview",
15
+ contents=[prompt[0],question]
16
+ )
17
+ return response.text
18
+
19
+ # ffn to retrieve quirery from Db
20
+ def read_sql_query(sql,db):
21
+ conn = sqlite3.connect(db)
22
+ cur = conn.cursor()
23
+ cur.execute(sql)
24
+ rows = cur.fetchall()
25
+ conn.commit()
26
+ conn.close()
27
+ for row in rows:
28
+ print(row)
29
+ return rows
30
+
31
+ ## define prompt
32
+ prompt = [
33
+ '''
34
+ You are an expert in converting english questions to sql code!
35
+ The sql database has name STUDENT and has following columns - NAME,CLASS,SECTION \n\n
36
+ For example, \nExample 1 - How many entries of records are present ?,
37
+ the SQL command will be like this SELECT COUNT(*) FROM STUDENT ;
38
+ \nEaxample 2 - Tell me all students study in Data Science class ?,
39
+ the SQL command will be like this SELECT * FROM STUDENT 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 app
45
+ st.set_page_config(page_title="I can Retreive Any SQL Query")
46
+ st.header("Gemini App to retrieve SQL data ")
47
+
48
+ question = st.text_input("Input: ",key="input")
49
+ submit = st.button("Ask the question")
50
+
51
+ # if submit is clicked
52
+ if submit:
53
+ response = get_gemini_response(question,prompt)
54
+ response = read_sql_query(response,"student.db")
55
+ st.subheader("The Response is ")
56
+ for row in response:
57
+ print(row)
58
+ st.header(row)