ashishmehra1926 commited on
Commit
bf55b44
·
verified ·
1 Parent(s): d1f1644

Delete sql.py

Browse files
Files changed (1) hide show
  1. sql.py +0 -70
sql.py DELETED
@@ -1,70 +0,0 @@
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
- # Load environment variables
8
- load_dotenv()
9
-
10
- # Configure Gemini API
11
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
-
13
- # Function to load Gemini model and generate SQL query
14
- def get_gemini_response(question, prompt):
15
- model = genai.GenerativeModel('gemini-pro')
16
- full_prompt = prompt + "\n\nUser Query: " + question # Better structuring
17
- response = model.generate_content(full_prompt)
18
- sql_query = response.text.strip() # Clean the response
19
- return sql_query
20
-
21
- # Function to retrieve query results from the database
22
- def read_sql_query(sql, db):
23
- try:
24
- conn = sqlite3.connect(db)
25
- cur = conn.cursor()
26
- cur.execute(sql)
27
- rows = cur.fetchall()
28
- conn.close()
29
- return rows
30
- except Exception as e:
31
- return [("Error:", str(e))] # Return error message if query fails
32
-
33
- # Define prompt
34
- prompt = """
35
- You are an expert in SQL query generation. Your task is to convert natural language questions into valid SQL queries based on the given database schema.
36
-
37
- Instructions:
38
- - The SQL database schema will be provided.
39
- - Generate a syntactically correct SQL query based on the input question.
40
- - The SQL query should be optimized and free from unnecessary clauses.
41
- - Do not include SQL keywords or formatting like triple backticks (```) in the response.
42
- - If the question is ambiguous, generate the most probable SQL query.
43
-
44
- Example:
45
-
46
- Input: "How many students are in the database?"
47
- Output: SELECT COUNT(*) FROM STUDENT_INFO;
48
-
49
- Input: "List all students in CLASS 10 section A."
50
- Output: SELECT * FROM STUDENT_INFO WHERE CLASS = '10' AND SECTION = 'A';
51
-
52
- Input: "Show the names of students in Data Science Section."
53
- Output: SELECT NAME FROM STUDENT_INFO WHERE SECTION = 'Data Science';
54
- """
55
-
56
- # Streamlit App
57
- st.set_page_config(page_title="SQL Query Generator")
58
- st.header("Gemini App To Retrieve SQL Data")
59
-
60
- question = st.text_input("Enter your question:", key="input")
61
- submit = st.button("Generate SQL Query")
62
-
63
- # If submit is clicked
64
- if submit:
65
- sql_query = get_gemini_response(question, prompt)
66
- st.subheader("Generated SQL Query")
67
- st.code(sql_query, language="sql") # Show SQL query
68
-
69
- response = read_sql_query(sql_query, "student.db")
70
-