laxminarasimha6 commited on
Commit
ffec9cc
1 Parent(s): 2cb4244

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -58
app.py CHANGED
@@ -1,63 +1,100 @@
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
- # Configure API key
8
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
9
-
10
- def get_gemini_response(question, prompt):
11
- model = genai.GenerativeModel('gemini-pro')
12
- response = model.generate_content([prompt[0], question])
13
- return response.text.strip()
14
-
15
- def read_sql_query(sql, db):
16
- conn = sqlite3.connect(db)
17
- cur = conn.cursor()
18
- cur.execute(sql)
19
- rows = cur.fetchall()
20
- conn.commit()
21
- conn.close()
22
- return rows
23
-
24
- # Prompt for the application
25
- prompt = [
 
26
  """
27
- You are an expert in converting English questions to SQL query!
28
- The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
29
- SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
30
- the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
31
- \nExample 2 - Tell me all the students studying in Data Science class?,
32
- the SQL command will be something like this SELECT * FROM STUDENT
33
- where CLASS="Data Science";
 
 
 
 
 
 
 
 
34
  """
35
- ]
36
-
37
- # Streamlit app
38
- st.set_page_config(page_title="Text to SQL Query Converter")
39
- st.title("Text to SQL Query Converter")
40
-
41
- # User input
42
- question = st.text_input("Enter your question:", key="input")
43
-
44
- # Submit button
45
- if st.button("Convert to SQL Query"):
46
- if not question:
47
- st.error("Please enter a question.")
48
- else:
49
- # Generate SQL query from the question
50
- sql_query = get_gemini_response(question, prompt)
51
- st.write("SQL Query:")
52
- st.code(sql_query)
53
-
54
- # Execute the SQL query and display results
55
- try:
56
- results = read_sql_query(sql_query, "student.db")
57
- if results:
58
- st.success("Query executed successfully. Results:")
59
- st.table(results)
60
- else:
61
- st.warning("No results found.")
62
- except Exception as e:
63
- st.error(f"An error occurred: {e}")
 
 
1
  import streamlit as st
 
 
2
  import google.generativeai as genai
3
+ from dotenv import load_dotenv
4
+ import os
5
+
6
+ # Load environment variables
7
+ load_dotenv()
8
+
9
+ # Configure GenerativeAI with API key
10
+ genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
11
+
12
+ def main():
13
+ # Function to generate SQL query based on prompt input
14
+ def gemini_ans(prompt_input):
15
+ model = genai.GenerativeModel('gemini-pro')
16
+ response = model.generate_content([prompt_input])
17
+ return response.text
18
+
19
+ # Set page configuration
20
+ st.set_page_config(page_title='SQL Query Generator', page_icon=':robot:')
21
+
22
+ # Header section
23
+ st.markdown(
24
+ """
25
+ <style>
26
+ .header {
27
+ padding: 20px;
28
+ background-color: #f0f0f0;
29
+ border-radius: 10px;
30
+ margin-bottom: 20px;
31
+ color: black;
32
+ }
33
+ .header h1, .header h3 {
34
+ margin: 0;
35
+ color: black;
36
+ }
37
+ </style>
38
+ """
39
+ , unsafe_allow_html=True)
40
 
41
+ st.markdown(
42
+ """
43
+ <div class="header">
44
+ <h1 style="text-align: center;">SQL Query Generator 🤖</h1>
45
+ <h3 style="text-align: center;">Generate SQL queries with ease!</h3>
46
+ <p style="text-align: center;">This tool allows you to generate SQL queries based on your prompt.</p>
47
+ </div>
48
+ """
49
+ , unsafe_allow_html=True)
50
+
51
+ # Text area for input
52
+ input_text = st.text_area('Enter your query...')
53
+
54
+ # Generate SQL Query button
55
+ submit = st.button('Generate SQL Query', key='generate_button')
56
+
57
+ # Prompts for model responses
58
+ prompt = """
59
+ You are an English to SQL language translator. Using the given text here {en},
60
+ write a SQL query only without making any mistakes.
61
  """
62
+
63
+ prompt1 = """
64
+ What would be the expected response of this query snippet:
65
+ ```
66
+ {query}
67
+ ```
68
+ Provide a sample tabular response with no explanation.
69
+ """
70
+
71
+ prompt2 = """
72
+ Explain the SQL query:
73
+ ```
74
+ {query}
75
+ ```
76
+ Please provide the simplest explanation.
77
  """
78
+
79
+ # Handle button click event
80
+ if submit:
81
+ with st.spinner('Generating SQL Query...'):
82
+ # Generate SQL query
83
+ sql_query = gemini_ans(prompt.format(en=input_text))
84
+ st.header('Model Response')
85
+ st.success("Generated SQL Query Successfully!")
86
+ st.write(sql_query)
87
+
88
+ # Generate sample expected output
89
+ sql_table = gemini_ans(prompt1.format(query=sql_query))
90
+ st.success('Sample Expected Output')
91
+ st.markdown(sql_table)
92
+
93
+ # Generate explanation of the given query
94
+ sql_explanation = gemini_ans(prompt2.format(query=sql_query))
95
+ st.success('Explanation of the Given Query')
96
+ st.markdown(sql_explanation)
97
+
98
+ # Execute main function
99
+ if __name__ == "__main__":
100
+ main()