-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
156 lines (136 loc) · 5.69 KB
/
Copy pathapp.py
File metadata and controls
156 lines (136 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# import mysql.connector
import sqlite3
from dotenv import load_dotenv
import os
import streamlit as st
import google.generativeai as genai
import logging
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
# Configure Google API key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Function to load the model and respond with SQL query
def get_gemini_response(question, prompt):
try:
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content([prompt[0], question])
return response.text.strip()
except Exception as e:
logging.error("Error generating response: %s", e)
return None
# # Connect to MySQL & execute query
# def execute_query(sql):
# try:
# # Connect to MySQL
# with mysql.connector.connect(
# host=os.getenv("MYSQL_HOST"),
# user=os.getenv("MYSQL_USER"),
# password=os.getenv("MYSQL_PASSWORD"),
# database=os.getenv("MYSQL_DATABASE")
# ) as connection:
# with connection.cursor() as cursor:
# # Check SQL command
# if sql.strip().lower().startswith("select"):
# cursor.execute(sql)
# results = cursor.fetchall()
# columns = [desc[0] for desc in cursor.description]
# return {"type": "select", "data": results, "columns": columns}
# else:
# cursor.execute(sql)
# connection.commit()
# return {"type": "operation", "message": "Operation successful!"}
# except mysql.connector.Error as e:
# logging.error("Database error: %s", e)
# return {"type": "error", "message": f"An error occurred: {e}"}
# Connect to SQLite & execute query
def execute_query(sql):
try:
# Connect to SQLite
connection = sqlite3.connect('sample.db')
cursor = connection.cursor()
# Check the SQL command
if sql.strip().lower().startswith("select"):
cursor.execute(sql)
results = cursor.fetchall()
columns = [desc[0] for desc in cursor.description]
return {"type": "select", "data": results, "columns": columns}
else:
cursor.execute(sql)
connection.commit()
return {"type": "operation", "message": "Operation successful!"}
except sqlite3.Error as e:
logging.error("Database error: %s", e)
return {"type": "error", "message": f"An error occurred: {e}"}
finally:
cursor.close()
connection.close()
# Streamlit App
st.set_page_config(page_title="Text To SQL")
st.header("Text to SQL")
# User input
question = st.text_input("Input: ", key="input", placeholder="Type your SQL question here...")
submit = st.button("Submit")
# Prompt for the model
prompt = [
"""
You are an expert in converting English questions to SQL queries!
The SQL database has the following tables and columns:
1. **students**: (student_id, name, department_id, batch, section, course_id, marks)
2. **courses**: (course_id, course_name, department_id)
3. **departments**: (department_id, department_name)
4. **enrollments**: (enrollment_id, student_id, course_id, semester)
5. **payments**: (payment_id, student_id, amount, payment_date)
Please note the following guidelines for generating SQL queries:
- Do not include backticks (```) in the output.
- Do not use single quotes (`'`) for string values or aliases.
- Do not use double quotes (`"`) for aliases.
- For aliases, use no delimiters (just the alias name) or backticks (``) only if necessary.
- Do not use the word 'SQL' in your response.
- Generate SQL queries without any additional delimiters.
Here are some examples of how to phrase questions:
\n1. Show me all the students.
\n2. What are the courses available?
\n3. List all students enrolled in the 'Artificial intelligence' course.
\n4. Create a new course called 'Data Science' with 4 credits.
\n5. Insert a new student with name 'John Doe' and department 'CSE'.
\n6. Show me the payment details of Rakibul Islam.
\n7. What is the department of Rakibul Islam?
\n8. Show me the marks of Rakibul Islam.
\n9. Update the department name from 'Computer Science' to 'CSE'.
\n10. Delete the student named 'John Doe'.
Ensure the generated queries are accurate and consider the relationships between tables where necessary.
"""
]
# Validate user input
def get_validated_input():
user_input = question.strip()
if not user_input:
st.error("The input field is empty")
return None
return user_input
if submit:
user_input = get_validated_input()
if user_input:
with st.spinner("Generating SQL query..."):
response = get_gemini_response(user_input, prompt)
if response:
st.subheader("Generated SQL Query:")
st.code(response)
# Execute the query and retrieve data
with st.spinner("Executing query..."):
result = execute_query(response)
# Handle the result based on types
if result['type'] == "error":
st.error(result['message'])
elif result['type'] == "select":
st.subheader("Query Results:")
if result['data']:
st.write(result['columns'])
for row in result['data']:
st.write(row)
else:
st.write("No results found.")
elif result['type'] == "operation":
st.success(result['message'])