laxminarasimha6 commited on
Commit
6e5a30a
1 Parent(s): 8a6f366

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ import streamlit as st
3
+ import os
4
+ import sqlite3
5
+ import google.generativeai as genai
6
+
7
+ # Configure API key
8
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
9
+
10
+ def get_gemini_response(question, prompt):
11
+ model = genai.GenerativeModel('gemini-pro')
12
+ response = model.generate_content([prompt[0], question])
13
+ return response.text.strip()
14
+
15
+ def read_sql_query(sql, db):
16
+ conn = sqlite3.connect(db)
17
+ cur = conn.cursor()
18
+ cur.execute(sql)
19
+ rows = cur.fetchall()
20
+ conn.commit()
21
+ conn.close()
22
+ return rows
23
+
24
+ # Prompt for the application
25
+ prompt = [
26
+ """
27
+ You are an expert in converting English questions to SQL query!
28
+ The SQL database has the name STUDENT and has the following columns - NAME, CLASS,
29
+ SECTION \n\nFor example,\nExample 1 - How many entries of records are present?,
30
+ the SQL command will be something like this SELECT COUNT(*) FROM STUDENT ;
31
+ \nExample 2 - Tell me all the students studying in Data Science class?,
32
+ the SQL command will be something like this SELECT * FROM STUDENT
33
+ where CLASS="Data Science";
34
+ """
35
+ ]
36
+
37
+ # Streamlit app
38
+ st.set_page_config(page_title="Text to SQL Query Converter")
39
+ st.title("Text to SQL Query Converter")
40
+
41
+ # User input
42
+ question = st.text_input("Enter your question:", key="input")
43
+
44
+ # Submit button
45
+ if st.button("Convert to SQL Query"):
46
+ if not question:
47
+ st.error("Please enter a question.")
48
+ else:
49
+ # Generate SQL query from the question
50
+ sql_query = get_gemini_response(question, prompt)
51
+ st.write("SQL Query:")
52
+ st.code(sql_query)
53
+
54
+ # Execute the SQL query and display results
55
+ try:
56
+ results = read_sql_query(sql_query, "student.db")
57
+ if results:
58
+ st.success("Query executed successfully. Results:")
59
+ st.table(results)
60
+ else:
61
+ st.warning("No results found.")
62
+ except Exception as e:
63
+ st.error(f"An error occurred: {e}")