Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,52 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
-
load_dotenv()
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import os
|
| 5 |
-
import google.generativeai as genai
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import sqlite3
|
| 8 |
-
|
| 9 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 10 |
-
# load gemini model
|
| 11 |
-
def get_gemini_response(question,prompt):
|
| 12 |
-
model=genai.GenerativeModel("gemini-pro")
|
| 13 |
-
response=model.generate_content([prompt[0],question])
|
| 14 |
-
return response.text
|
| 15 |
-
|
| 16 |
-
def read_sql_query(sql,db):
|
| 17 |
-
connection=sqlite3.connect('student.db')
|
| 18 |
-
cursor=connection.cursor()
|
| 19 |
-
cursor.execute(sql)
|
| 20 |
-
rows=cursor.fetchall()
|
| 21 |
-
connection.commit()
|
| 22 |
-
connection.close()
|
| 23 |
-
for row in rows:
|
| 24 |
-
print(row)
|
| 25 |
-
return rows
|
| 26 |
-
|
| 27 |
-
prompt=[
|
| 28 |
-
"""
|
| 29 |
-
You are an expert in converting English questions to SQL query!
|
| 30 |
-
The SQL database has the name student and has the following columns - name, roll,
|
| 31 |
-
section,marks \n\nFor example,\nExample 1 - How many entries of records are present?,
|
| 32 |
-
the SQL command will be something like this SELECT COUNT(*) FROM student ;
|
| 33 |
-
\nExample 2 - Tell me all the students studying in A section?,
|
| 34 |
-
the SQL command will be something like this SELECT * FROM student
|
| 35 |
-
where section="A";
|
| 36 |
-
also the sql code should not have ``` in beginning or end and sql word in output
|
| 37 |
-
"""
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
st.set_page_config(page_title="SQL Query Generator",page_icon="📊")
|
| 42 |
-
st.title("SQL Query Generator")
|
| 43 |
-
st.write("This app generates SQL queries based on the questions asked by the user.")
|
| 44 |
-
question=st.text_input("Enter your question here:",key="input")
|
| 45 |
-
submit=st.button("Generate SQL Query")
|
| 46 |
-
|
| 47 |
-
if submit:
|
| 48 |
-
response=get_gemini_response(question,prompt)
|
| 49 |
-
st.write(response)
|
| 50 |
-
rows=read_sql_query(response,"student.db")
|
| 51 |
-
for row in rows:
|
| 52 |
st.write(row)
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv()
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import sqlite3
|
| 8 |
+
|
| 9 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 10 |
+
# load gemini model
|
| 11 |
+
def get_gemini_response(question,prompt):
|
| 12 |
+
model=genai.GenerativeModel("gemini-pro")
|
| 13 |
+
response=model.generate_content([prompt[0],question])
|
| 14 |
+
return response.text
|
| 15 |
+
|
| 16 |
+
def read_sql_query(sql,db):
|
| 17 |
+
connection=sqlite3.connect('student.db')
|
| 18 |
+
cursor=connection.cursor()
|
| 19 |
+
cursor.execute(sql)
|
| 20 |
+
rows=cursor.fetchall()
|
| 21 |
+
connection.commit()
|
| 22 |
+
connection.close()
|
| 23 |
+
for row in rows:
|
| 24 |
+
print(row)
|
| 25 |
+
return rows
|
| 26 |
+
|
| 27 |
+
prompt=[
|
| 28 |
+
"""
|
| 29 |
+
You are an expert in converting English questions to SQL query!
|
| 30 |
+
The SQL database has the name student and has the following columns - name, roll,
|
| 31 |
+
section,marks \n\nFor example,\nExample 1 - How many entries of records are present?,
|
| 32 |
+
the SQL command will be something like this SELECT COUNT(*) FROM student ;
|
| 33 |
+
\nExample 2 - Tell me all the students studying in A section?,
|
| 34 |
+
the SQL command will be something like this SELECT * FROM student
|
| 35 |
+
where section="A";
|
| 36 |
+
also the sql code should not have ``` in beginning or end and sql word in output
|
| 37 |
+
"""
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
st.set_page_config(page_title="SQL Query Generator",page_icon="📊")
|
| 42 |
+
st.title("SQL Query Generator")
|
| 43 |
+
st.write("This app generates SQL queries based on the questions asked by the user.\n NOTE: You need to add your own database first. Here i have only added 1 database i.e. student it will execute query for that only")
|
| 44 |
+
question=st.text_input("Enter your question here:",key="input")
|
| 45 |
+
submit=st.button("Generate SQL Query")
|
| 46 |
+
|
| 47 |
+
if submit:
|
| 48 |
+
response=get_gemini_response(question,prompt)
|
| 49 |
+
st.write(response)
|
| 50 |
+
rows=read_sql_query(response,"student.db")
|
| 51 |
+
for row in rows:
|
| 52 |
st.write(row)
|