mazed commited on
Commit
f0efe10
·
verified ·
1 Parent(s): 9d6e0b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -62
app.py CHANGED
@@ -1,62 +1,62 @@
1
- from dotenv import load_dotenv
2
- load_dotenv()
3
-
4
- import streamlit as st
5
- import os
6
- import sqlite3
7
- import google.generativeai as genai
8
-
9
- # Configure API key
10
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
-
12
- # Function to load Gemini model and provide sql query as response
13
- def get_gemini_response(question,prompt):
14
- model=genai.GenerativeModel('gemini-pro')
15
- response=model.generate_content([prompt[0],question])
16
- return response.text
17
-
18
- # Funciton to retrieve query from the sql database
19
- def read_sql_query(sql,db):
20
- conn=sqlite3.connect(db)
21
- cur=conn.cursor()
22
- cur.execute(sql)
23
- rows=cur.fetchall()
24
- conn.commit()
25
- conn.close()
26
- for row in rows:
27
- print(row)
28
- return rows
29
-
30
- ## Define Your Prompt
31
- prompt=[
32
- """
33
- You are an expert in converting English questions to SQL query!
34
- The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
35
- SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
36
- the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
37
- \nExample 2 - Tell me all the students studying in Data Science class?,
38
- the SQL command will be something like this SELECT * FROM STUDENT
39
- where CLASS="Data Science";
40
- also the sql code should not have ``` in beginning or end and sql word in output
41
-
42
- """
43
- ]
44
-
45
- # Streamlit app
46
- st.set_page_config(page_title="AskSQL")
47
-
48
- # App name
49
- st.markdown("<h4 style='text-align: center;'>AskSQL</h4>", unsafe_allow_html=True)
50
-
51
- # Input and submit
52
- question=st.text_input(" ", key="input", placeholder="Ask question from the database")
53
- submit=st.button("Submit")
54
-
55
- # If submit is clicked
56
- if submit:
57
- response=get_gemini_response(question,prompt)
58
- print(response)
59
- data=read_sql_query(response,"student.db")
60
- for row in data:
61
- formatted_row = ', '.join(map(str, row))
62
- st.header(formatted_row)
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv()
3
+
4
+ import streamlit as st
5
+ import os
6
+ import sqlite3
7
+ import google.generativeai as genai
8
+
9
+ # Configure API key
10
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
+
12
+ # Function to load Gemini model and provide sql query as response
13
+ def get_gemini_response(question,prompt):
14
+ model=genai.GenerativeModel('gemini-pro')
15
+ response=model.generate_content([prompt[0],question])
16
+ return response.text
17
+
18
+ # Funciton to retrieve query from the sql database
19
+ def read_sql_query(sql,db):
20
+ conn=sqlite3.connect(db)
21
+ cur=conn.cursor()
22
+ cur.execute(sql)
23
+ rows=cur.fetchall()
24
+ conn.commit()
25
+ conn.close()
26
+ for row in rows:
27
+ print(row)
28
+ return rows
29
+
30
+ ## Define Prompt
31
+ prompt=[
32
+ """
33
+ You are an expert in converting English questions to SQL query!
34
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
35
+ SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
36
+ the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
37
+ \nExample 2 - Tell me all the students studying in Data Science class?,
38
+ the SQL command will be something like this SELECT * FROM STUDENT
39
+ where CLASS="Data Science";
40
+ also the sql code should not have ``` in beginning or end and sql word in output
41
+
42
+ """
43
+ ]
44
+
45
+ # Streamlit app
46
+ st.set_page_config(page_title="AskSQL")
47
+
48
+ # App name
49
+ st.markdown("<h4 style='text-align: center;'>AskSQL</h4>", unsafe_allow_html=True)
50
+
51
+ # Input and submit
52
+ question=st.text_input(" ", key="input", placeholder="Ask question from the database")
53
+ submit=st.button("Submit")
54
+
55
+ # If submit is clicked
56
+ if submit:
57
+ response=get_gemini_response(question,prompt)
58
+ print(response)
59
+ data=read_sql_query(response,"student.db")
60
+ for row in data:
61
+ formatted_row = ', '.join(map(str, row))
62
+ st.header(formatted_row)