|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
import streamlit as st |
|
import google.generativeai as genai |
|
import sqlite3 |
|
import os |
|
|
|
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) |
|
model=genai.GenerativeModel('gemini-pro') |
|
|
|
|
|
|
|
|
|
def read_sql_query(sql,db): |
|
conn=sqlite3.connect(db) |
|
cursor=conn.cursor() |
|
cursor.execute(sql) |
|
rows=cursor.fetchall() |
|
conn.commit() |
|
conn.close() |
|
|
|
for row in rows: |
|
print(row) |
|
return rows |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prompt=[ |
|
""" |
|
You are an expert in converting English questions to SQL query! |
|
The SQL database has the name STUDENT and has the following columns - NAME, CLASS, |
|
SECTION \n\nFor example,\nExample 1 - How many entries of records are present?, |
|
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ; |
|
\nExample 2 - Tell me all the students studying in Data Science class?, |
|
the SQL command will be something like this SELECT * FROM STUDENT |
|
where CLASS="Data Science"; |
|
\nExample 3-i marks should be greater 40 and atleast 2 person have score above 40 , retrive that class |
|
the SQL command will be something like this SELECT CLASS FROM STUDENT GROUP BY CLASS HAVING COUNT(*) >= 2 AND AVG(MARKS) > 50; |
|
\nExample 4-Find the names and marks of students in the Science class who have scored more than 60 marks. |
|
SQL Command Example: SELECT NAME, MARKS FROM STUDENT WHERE CLASS='Science' AND MARKS > 60; |
|
\nExample 5-List the classes with the highest average marks. |
|
SQL Command Example: SELECT CLASS FROM STUDENT GROUP BY CLASS HAVING AVG(MARKS) = (SELECT MAX(AVG(MARKS)) FROM STUDENT GROUP BY CLASS); |
|
|
|
also the sql code should not have ``` in beginning or end and sql word in output |
|
|
|
""" |
|
|
|
|
|
] |
|
|
|
|
|
|
|
def gemini_sql_query(prompt,input): |
|
response=model.generate_content([prompt[0],input]) |
|
return response.text |
|
|
|
|
|
st.set_page_config("chat pdf") |
|
st.header("chat with pdf using gemini") |
|
|
|
input=st.text_input("enter the query") |
|
submit=st.button("submit") |
|
|
|
|
|
|
|
|
|
if submit: |
|
query=gemini_sql_query(prompt,input) |
|
response=read_sql_query(query,"student.db") |
|
print(query) |
|
st.header("response") |
|
for row in response: |
|
|
|
values = [str(value) for value in row] |
|
|
|
st.write(*values) |
|
|
|
|
|
|