krishnkant0212 commited on
Commit
a352270
·
verified ·
1 Parent(s): ce9dbae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -51
app.py CHANGED
@@ -1,80 +1,66 @@
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)
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import os
3
  import sqlite3
4
+ from google.generativeai import GenerativeModel
5
 
6
+ # Configure API Key
 
 
7
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
8
 
9
+ # Function to Load Google Gemini Model that provide SQL Query as response
 
10
  def get_gemini_response(question, prompt):
11
+ model = GenerativeModel('gemini-pro')
12
  response = model.generate_content([prompt[0], question])
 
13
  return response.text
14
 
15
+ # Function to retrieve query from SQL database
16
+ def read_sql_query(sql, db):
17
  conn = sqlite3.connect(db)
18
  cur = conn.cursor()
19
  cur.execute(sql)
20
  rows = cur.fetchall()
21
  conn.commit()
22
  conn.close()
 
 
 
 
23
  return rows
24
 
25
+ # Defining Prompt
26
  prompt = [
27
  '''
28
  Imagine you are an expert in converting natural language text into SQL queries. You have a table named e_com with the following columns:
29
+ user_id (INTEGER): The unique identifier for each user, automatically incremented.
30
+ username (VARCHAR(50)): The name of the user, required and cannot be null.
31
+ email (VARCHAR(100)): The email address of the user, also required and cannot be null.
32
+ address (VARCHAR(255)): The address of the user.
33
+ order_quantity (VARCHAR(255)): The quantity of orders placed by the user.
34
+ order_date (TIMESTAMP): The date and time of when the order was placed, with a default value set to the current timestamp.
35
+ Your task is to convert the natural language descriptions into SQL queries that operate on the E_COM_DATA table.
36
+ EXAMPLES -
37
+ Tell me the usernames and email addresses of all users whose order quantity is greater than 3.
38
+ Expected Response - SELECT username, email FROM e_com WHERE order_quantity > '3';
39
+ Tell me the usernames and addresses of users who have placed orders after a specific date, say '2023-01-01'.
40
+ Expected Response - SELECT username, address FROM e_com WHERE order_date > '2023-01-01';
41
+ ALSO, the sql response should not have ``` in the beginning or end and sql word in the output.
42
+ '''
 
 
 
 
 
 
 
 
43
  ]
44
 
45
+ # Streamlit App
46
+ st.set_page_config(page_title="Hey! I can retrieve any SQL query")
 
47
  st.header("Gemini App to Retrieve SQL Data")
48
 
49
  question = st.text_input("Input: ", key="input")
50
  submit = st.button("Ask the question")
51
 
52
  if submit:
53
+ try:
54
+ response = get_gemini_response(question, prompt)
55
+ st.subheader("The SQL Query Generated is ")
56
+ st.code(response.strip("`").replace("sql", "").strip(), language="sql")
57
+
58
+ data = read_sql_query(response, "e_com.db")
59
+ st.subheader("The Response is ")
60
+
61
+ if data:
62
+ st.table(data)
63
+ else:
64
+ st.write("No records found matching the criteria.")
65
+ except Exception as e:
66
+ st.error(f"An error occurred: {str(e)}")