ashishmehra1926 commited on
Commit
5f43ffe
·
verified ·
1 Parent(s): 2513d29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -70
app.py CHANGED
@@ -1,70 +1,72 @@
1
- from dotenv import load_dotenv
2
- import streamlit as st
3
- import os
4
- import sqlite3
5
- import google.generativeai as genai
6
-
7
- # Load environment variables
8
- load_dotenv()
9
-
10
- # Configure Gemini API
11
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
-
13
- # Function to load Gemini model and generate SQL query
14
- def get_gemini_response(question, prompt):
15
- model = genai.GenerativeModel('gemini-pro')
16
- full_prompt = prompt + "\n\nUser Query: " + question # Better structuring
17
- response = model.generate_content(full_prompt)
18
- sql_query = response.text.strip() # Clean the response
19
- return sql_query
20
-
21
- # Function to retrieve query results from the database
22
- def read_sql_query(sql, db):
23
- try:
24
- conn = sqlite3.connect(db)
25
- cur = conn.cursor()
26
- cur.execute(sql)
27
- rows = cur.fetchall()
28
- conn.close()
29
- return rows
30
- except Exception as e:
31
- return [("Error:", str(e))] # Return error message if query fails
32
-
33
- # Define prompt
34
- prompt = """
35
- You are an expert in SQL query generation. Your task is to convert natural language questions into valid SQL queries based on the given database schema.
36
-
37
- Instructions:
38
- - The SQL database schema will be provided.
39
- - Generate a syntactically correct SQL query based on the input question.
40
- - The SQL query should be optimized and free from unnecessary clauses.
41
- - Do not include SQL keywords or formatting like triple backticks (```) in the response.
42
- - If the question is ambiguous, generate the most probable SQL query.
43
-
44
- Example:
45
-
46
- Input: "How many students are in the database?"
47
- Output: SELECT COUNT(*) FROM STUDENT_INFO;
48
-
49
- Input: "List all students in CLASS 10 section A."
50
- Output: SELECT * FROM STUDENT_INFO WHERE CLASS = '10' AND SECTION = 'A';
51
-
52
- Input: "Show the names of students in Data Science Section."
53
- Output: SELECT NAME FROM STUDENT_INFO WHERE SECTION = 'Data Science';
54
- """
55
-
56
- # Streamlit App
57
- st.set_page_config(page_title="SQL Query Generator")
58
- st.header("Gemini App To Retrieve SQL Data")
59
-
60
- question = st.text_input("Enter your question:", key="input")
61
- submit = st.button("Generate SQL Query")
62
-
63
- # If submit is clicked
64
- if submit:
65
- sql_query = get_gemini_response(question, prompt)
66
- st.subheader("Generated SQL Query")
67
- st.code(sql_query, language="sql") # Show SQL query
68
-
69
- response = read_sql_query(sql_query, "student.db")
70
-
 
 
 
1
+ from dotenv import load_dotenv
2
+ import streamlit as st
3
+ import os
4
+ import sqlite3
5
+ import google.generativeai as genai
6
+
7
+ # # Load environment variables
8
+ # load_dotenv()
9
+
10
+ # # Configure Gemini API
11
+ # genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+ api_key = st.secrets["GOOGLE_API_KEY"]
14
+
15
+ # Function to load Gemini model and generate SQL query
16
+ def get_gemini_response(question, prompt):
17
+ model = genai.GenerativeModel('gemini-pro')
18
+ full_prompt = prompt + "\n\nUser Query: " + question # Better structuring
19
+ response = model.generate_content(full_prompt)
20
+ sql_query = response.text.strip() # Clean the response
21
+ return sql_query
22
+
23
+ # Function to retrieve query results from the database
24
+ def read_sql_query(sql, db):
25
+ try:
26
+ conn = sqlite3.connect(db)
27
+ cur = conn.cursor()
28
+ cur.execute(sql)
29
+ rows = cur.fetchall()
30
+ conn.close()
31
+ return rows
32
+ except Exception as e:
33
+ return [("Error:", str(e))] # Return error message if query fails
34
+
35
+ # Define prompt
36
+ prompt = """
37
+ You are an expert in SQL query generation. Your task is to convert natural language questions into valid SQL queries based on the given database schema.
38
+
39
+ Instructions:
40
+ - The SQL database schema will be provided.
41
+ - Generate a syntactically correct SQL query based on the input question.
42
+ - The SQL query should be optimized and free from unnecessary clauses.
43
+ - Do not include SQL keywords or formatting like triple backticks (```) in the response.
44
+ - If the question is ambiguous, generate the most probable SQL query.
45
+
46
+ Example:
47
+
48
+ Input: "How many students are in the database?"
49
+ Output: SELECT COUNT(*) FROM STUDENT_INFO;
50
+
51
+ Input: "List all students in CLASS 10 section A."
52
+ Output: SELECT * FROM STUDENT_INFO WHERE CLASS = '10' AND SECTION = 'A';
53
+
54
+ Input: "Show the names of students in Data Science Section."
55
+ Output: SELECT NAME FROM STUDENT_INFO WHERE SECTION = 'Data Science';
56
+ """
57
+
58
+ # Streamlit App
59
+ st.set_page_config(page_title="SQL Query Generator")
60
+ st.header("Gemini App To Retrieve SQL Data")
61
+
62
+ question = st.text_input("Enter your question:", key="input")
63
+ submit = st.button("Generate SQL Query")
64
+
65
+ # If submit is clicked
66
+ if submit:
67
+ sql_query = get_gemini_response(question, prompt)
68
+ st.subheader("Generated SQL Query")
69
+ st.code(sql_query, language="sql") # Show SQL query
70
+
71
+ response = read_sql_query(sql_query, "student.db")
72
+