Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
import sqlite3
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
# Initialize Gemini
|
| 9 |
+
genai.configure(api_key="AIzaSyCnuJMrTkckScv0YxAaaQzH1LDomNUUppA")
|
| 10 |
+
genai_model = genai.GenerativeModel('gemini-pro')
|
| 11 |
+
|
| 12 |
+
class SQLPromptModel:
|
| 13 |
+
def __init__(self, database):
|
| 14 |
+
self.database = database
|
| 15 |
+
self.conn = sqlite3.connect(self.database)
|
| 16 |
+
|
| 17 |
+
def fetch_table_schema(self, table_name):
|
| 18 |
+
cursor = self.conn.cursor()
|
| 19 |
+
cursor.execute(f"PRAGMA table_info({table_name})")
|
| 20 |
+
schema = cursor.fetchall()
|
| 21 |
+
return schema if schema else None
|
| 22 |
+
|
| 23 |
+
def text2sql_gemini(self, schema, user_prompt, inp_prompt=None):
|
| 24 |
+
table_columns = ', '.join([f"{col[1]} {col[2]}" for col in schema])
|
| 25 |
+
|
| 26 |
+
prompt = f"""Below are SQL table schemas paired with instructions that describe a task.
|
| 27 |
+
Using valid SQLite, write a response that appropriately completes the request for the provided tables.
|
| 28 |
+
### Instruction: {user_prompt} ###
|
| 29 |
+
Input: CREATE TABLE sql_pdf({table_columns});
|
| 30 |
+
### Response: (Return only generated query based on user_prompt , nothing extra)"""
|
| 31 |
+
|
| 32 |
+
if inp_prompt is not None:
|
| 33 |
+
prompt = prompt.replace(user_prompt, inp_prompt + " ")
|
| 34 |
+
|
| 35 |
+
completion = genai_model.generate_content(prompt)
|
| 36 |
+
generated_query = completion.text
|
| 37 |
+
|
| 38 |
+
# Extract SQL query
|
| 39 |
+
start_index = generated_query.find("SELECT")
|
| 40 |
+
end_index = generated_query.find(";", start_index) + 1
|
| 41 |
+
|
| 42 |
+
if start_index != -1 and end_index != 0:
|
| 43 |
+
return generated_query[start_index:end_index]
|
| 44 |
+
return generated_query
|
| 45 |
+
|
| 46 |
+
def execute_query(self, query):
|
| 47 |
+
cur = self.conn.cursor()
|
| 48 |
+
cur.execute(query)
|
| 49 |
+
columns = [header[0] for header in cur.description]
|
| 50 |
+
rows = [row for row in cur.fetchall()]
|
| 51 |
+
cur.close()
|
| 52 |
+
self.conn.commit()
|
| 53 |
+
return rows, columns
|
| 54 |
+
|
| 55 |
+
def execute_sql_query(input_prompt):
|
| 56 |
+
database = r"sql_pdf.db"
|
| 57 |
+
sql_model = SQLPromptModel(database)
|
| 58 |
+
|
| 59 |
+
user_prompt = "Give complete details of properties in India"
|
| 60 |
+
|
| 61 |
+
for _ in range(3): # Retry logic
|
| 62 |
+
try:
|
| 63 |
+
table_schema = sql_model.fetch_table_schema("sql_pdf")
|
| 64 |
+
if table_schema:
|
| 65 |
+
if input_prompt.strip():
|
| 66 |
+
query = sql_model.text2sql_gemini(table_schema, user_prompt, input_prompt)
|
| 67 |
+
else:
|
| 68 |
+
query = sql_model.text2sql_gemini(table_schema, user_prompt, user_prompt)
|
| 69 |
+
|
| 70 |
+
rows, columns = sql_model.execute_query(query)
|
| 71 |
+
return {"Query": query, "Results": rows, "Columns": columns}
|
| 72 |
+
else:
|
| 73 |
+
return {"error": "Table schema not found."}
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"An error occurred: {e}")
|
| 76 |
+
time.sleep(1)
|
| 77 |
+
return {"error": "Failed to execute query after 3 retries."}
|
| 78 |
+
|
| 79 |
+
# Load the image
|
| 80 |
+
image = Image.open(os.path.join(os.path.abspath(''), "house_excel_sheet.png"))
|
| 81 |
+
|
| 82 |
+
# Create Gradio interface
|
| 83 |
+
with gr.Blocks(title="House Database Query") as demo:
|
| 84 |
+
gr.Markdown("# House Database Query System")
|
| 85 |
+
|
| 86 |
+
# Display the image
|
| 87 |
+
gr.Image(image)
|
| 88 |
+
|
| 89 |
+
gr.Markdown("""### The database contains information about different properties including their fundamental details.
|
| 90 |
+
You can query this database using natural language.""")
|
| 91 |
+
|
| 92 |
+
# Query input and output
|
| 93 |
+
query_input = gr.Textbox(
|
| 94 |
+
lines=2,
|
| 95 |
+
label="Database Query",
|
| 96 |
+
placeholder="Enter your query or choose from examples below. Default: 'Properties in India'"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
query_output = gr.JSON(label="Query Results")
|
| 100 |
+
|
| 101 |
+
# Example queries
|
| 102 |
+
gr.Examples(
|
| 103 |
+
examples=[
|
| 104 |
+
"Properties in France",
|
| 105 |
+
"Properties greater than an acre",
|
| 106 |
+
"Properties with more than 400 bedrooms"
|
| 107 |
+
],
|
| 108 |
+
inputs=query_input,
|
| 109 |
+
outputs=query_output,
|
| 110 |
+
fn=execute_sql_query
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Submit button
|
| 114 |
+
query_input.submit(fn=execute_sql_query, inputs=query_input, outputs=query_output)
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
demo.launch(share=True)
|