Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
load_dotenv()
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
import google.generativeai as genai
|
6 |
+
import sqlite3
|
7 |
+
import os
|
8 |
+
|
9 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
10 |
+
model=genai.GenerativeModel('gemini-pro')
|
11 |
+
|
12 |
+
|
13 |
+
#dun to retrieve query from the sql database
|
14 |
+
|
15 |
+
def read_sql_query(sql,db):
|
16 |
+
conn=sqlite3.connect(db)
|
17 |
+
cursor=conn.cursor()
|
18 |
+
cursor.execute(sql)
|
19 |
+
rows=cursor.fetchall()
|
20 |
+
conn.commit()
|
21 |
+
conn.close()
|
22 |
+
|
23 |
+
for row in rows:
|
24 |
+
print(row)
|
25 |
+
return rows
|
26 |
+
|
27 |
+
# prompt="""
|
28 |
+
# you are a professinal sql query expert, i will provide input what i want from database
|
29 |
+
# ,you give me the query without quotes are comma or " " double quotes or ```, just give me query
|
30 |
+
# """
|
31 |
+
|
32 |
+
|
33 |
+
prompt=[
|
34 |
+
"""
|
35 |
+
You are an expert in converting English questions to SQL query!
|
36 |
+
The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
|
37 |
+
SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
|
38 |
+
the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
|
39 |
+
\nExample 2 - Tell me all the students studying in Data Science class?,
|
40 |
+
the SQL command will be something like this SELECT * FROM STUDENT
|
41 |
+
where CLASS="Data Science";
|
42 |
+
\nExample 3-i marks should be greater 40 and atleast 2 person have score above 40 , retrive that class
|
43 |
+
the SQL command will be something like this SELECT CLASS FROM STUDENT GROUP BY CLASS HAVING COUNT(*) >= 2 AND AVG(MARKS) > 50;
|
44 |
+
\nExample 4-Find the names and marks of students in the Science class who have scored more than 60 marks.
|
45 |
+
SQL Command Example: SELECT NAME, MARKS FROM STUDENT WHERE CLASS='Science' AND MARKS > 60;
|
46 |
+
\nExample 5-List the classes with the highest average marks.
|
47 |
+
SQL Command Example: SELECT CLASS FROM STUDENT GROUP BY CLASS HAVING AVG(MARKS) = (SELECT MAX(AVG(MARKS)) FROM STUDENT GROUP BY CLASS);
|
48 |
+
|
49 |
+
also the sql code should not have ``` in beginning or end and sql word in output
|
50 |
+
|
51 |
+
"""
|
52 |
+
|
53 |
+
|
54 |
+
]
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
def gemini_sql_query(prompt,input):
|
59 |
+
response=model.generate_content([prompt[0],input])
|
60 |
+
return response.text
|
61 |
+
|
62 |
+
|
63 |
+
st.set_page_config("chat pdf")
|
64 |
+
st.header("chat with pdf using gemini")
|
65 |
+
|
66 |
+
input=st.text_input("enter the query")
|
67 |
+
submit=st.button("submit")
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
if submit:
|
73 |
+
query=gemini_sql_query(prompt,input)
|
74 |
+
response=read_sql_query(query,"student.db")
|
75 |
+
print(query)
|
76 |
+
st.header("response")
|
77 |
+
for row in response:
|
78 |
+
# Convert integers to strings
|
79 |
+
values = [str(value) for value in row]
|
80 |
+
# Write the values to the Streamlit component
|
81 |
+
st.write(*values)
|
82 |
+
|
83 |
+
|