krishnkant0212 commited on
Commit
7cfa86b
·
verified ·
1 Parent(s): 1ee8ce5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from dotenv import load_dotenv
2
+ # load_dotenv() #load all the env variables
3
+
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+
8
+ import google.generativeai as genai
9
+
10
+ #Configure API Key
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+
14
+ #Fuction to Load Google Gemini Model that provide SQL Query as response
15
+ def get_gemini_response(question, prompt):
16
+ model = genai.GenerativeModel('gemini-pro')
17
+ response = model.generate_content([prompt[0], question])
18
+
19
+ return response.text
20
+
21
+ #Function to retrieve query from SQL 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
+
30
+ for row in rows:
31
+ print(row)
32
+
33
+ return rows
34
+
35
+ #Defining Prompt
36
+ prompt = [
37
+ '''
38
+ Imagine you are an expert in converting natural language text into SQL queries. You have a table named e_com with the following columns:
39
+
40
+ user_id (INTEGER): The unique identifier for each user, automatically incremented.
41
+ username (VARCHAR(50)): The name of the user, required and cannot be null.
42
+ email (VARCHAR(100)): The email address of the user, also required and cannot be null.
43
+ address (VARCHAR(255)): The address of the user.
44
+ order_quantity (VARCHAR(255)): The quantity of orders placed by the user.
45
+ order_date (TIMESTAMP): The date and time of when the order was placed, with a default value set to the current timestamp.
46
+ Your task is to convert the natural language descriptions into SQL queries that operate on the E_COM_DATA table.
47
+
48
+ EXAMPLES -
49
+
50
+ Tell me the usernames and email addresses of all users whose order quantity is greater than 3.
51
+ Expected Response - SELECT username, email FROM e_com WHERE order_quantity > '3';
52
+
53
+
54
+ Tell me the usernames and addresses of users who have placed orders after a specific date, say '2023-01-01'.
55
+ Expected Response - SELECT username, address FROM e_com WHERE order_date > '2023-01-01';
56
+
57
+
58
+ ALSO, the sql response should not have ``` in the beginning or end and sql word in the output.
59
+
60
+ '''
61
+ ]
62
+
63
+ #StreamLit App
64
+
65
+ st.set_page_config(page_title = "Hey! I can retriece any SQL query")
66
+ st.header("Gemini App to Retrieve SQL Data")
67
+
68
+ question = st.text_input("Input: ", key="input")
69
+ submit = st.button("Ask the question")
70
+
71
+ if submit:
72
+ response = get_gemini_response(question,prompt)
73
+ print(response)
74
+
75
+ data = read_sql_query(response, "e_com.db")
76
+ st.subheader("The Response is ")
77
+
78
+ for row in data:
79
+ print(row)
80
+ st.header(row)