niladridutta commited on
Commit
744b59b
·
verified ·
1 Parent(s): 0423772
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ from pathlib import Path
4
+ import sqlite3
5
+ import pandas as pd
6
+
7
+ st.set_page_config(page_title='SQL GENERATOR')
8
+ st.title('SQL GENERATED WITH GENAI')
9
+
10
+ secretKey = "AIzaSyAA_R5VXv1qjJ5jDMObkluREA8BxJO67RU"
11
+ #from google.colab import userdata
12
+ genai.configure(api_key = secretKey)
13
+ # Set up the model
14
+ generation_config = {
15
+ "temperature": 0.4,
16
+ "top_p": 1,
17
+ "top_k": 32,
18
+ "max_output_tokens": 4096,
19
+ }
20
+
21
+ safety_settings = [
22
+ {
23
+ "category": "HARM_CATEGORY_HARASSMENT",
24
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
25
+ },
26
+ {
27
+ "category": "HARM_CATEGORY_HATE_SPEECH",
28
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
29
+ },
30
+ {
31
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
32
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
33
+ },
34
+ {
35
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
36
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
37
+ }
38
+ ]
39
+ @st.cache_resource
40
+ def load_model(model1,config1,safety1):
41
+ return genai.GenerativeModel(model_name = model1,
42
+ generation_config = config1,
43
+ safety_settings = safety1)
44
+ model = load_model("gemini-pro",generation_config,safety_settings)
45
+
46
+
47
+ prompt_parts_1 = [
48
+ "You are an expert in converting English questions to SQL code! The SQL database has the name classicmodels and has the following tables - productlines, products, offices, employees, customers, payments, orders and orderdetails.\n\nFor example,\nExample 1 - How many Classic Cars are present?, the SQL command will be something like this\n SELECT COUNT(*) FROM products WHERE productLine = 'Classic Cars';\n\n\nExample 2 - What are the names of the cars having turnable front wheels?\n\nSELECT productName FROM products WHERE productDescription LIKE '%turnable front wheels%';\n\n\n Example 3 - What are the top 5 high performing products in terms of revenue?, the SQL command will be SELECT productName, SUM(quantityOrdered * priceEach) AS totalRevenue FROM orderdetails JOIN products ON products.productCode = orderdetails.productCode GROUP BY productName ORDER BY totalRevenue DESC LIMIT 5;\n\n\n Example 4 - What are the top 5 employees in terms of sales?, the SQL command will be SELECT e.employeeNumber, e.firstName || ' ' || e.lastName AS employeeName, SUM(od.quantityOrdered * od.priceEach) AS totalSales FROM employees e JOIN customers c ON e.employeeNumber = c.salesRepEmployeeNumber JOIN orders o ON c.customerNumber = o.customerNumber JOIN orderdetails od ON o.orderNumber = od.orderNumber GROUP BY e.employeeNumber, employeeName ORDER BY totalSales DESC LIMIT 5; \n\n\nExample 5 - \n\nSELECT productName FROM products WHERE quantityInStock = (SELECT MAX(quantityInStock) FROM products);\n\n\nExample 4 - \n\nSELECT productName FROM products WHERE quantityInStock = (SELECT MAX(quantityInStock) FROM products);\n\n\nDont include ``` and \\n in the output",
49
+ ]
50
+
51
+
52
+ st.subheader('SHOW TABLE')
53
+ input1=st.text_input("Enter table name")
54
+ submit1=st.button("Show")
55
+ if input1 is not None and submit1:
56
+ conn = sqlite3.connect('data.sqlite')
57
+ cur = conn.cursor()
58
+ query = f"select * from {input1} limit 5"
59
+ cur.execute(query)
60
+ records = cur.fetchall()
61
+ df1 = pd.read_sql_query(query, con=conn)
62
+ conn.close()
63
+ st.dataframe(df1)
64
+
65
+ st.subheader("GENERATE SQL RESULT")
66
+ question=st.text_input("Enter question related to the database")
67
+ submit2=st.button("Run")
68
+ if question is not None and submit2:
69
+ prompt_parts = [prompt_parts_1[0], question]
70
+ response = model.generate_content(prompt_parts)
71
+ query1 = response.text
72
+ conn1 = sqlite3.connect('data.sqlite')
73
+ cur1 = conn1.cursor()
74
+ cur1.execute(query1)
75
+ records = cur1.fetchall()
76
+ df2 = pd.read_sql_query(query1, con=conn1)
77
+ conn1.close()
78
+ st.dataframe(df2)